Skip to content

Commit

Permalink
Implement IntoPy<PyObject> for &PathBuf and &OsString
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jul 4, 2021
1 parent 4b88dd1 commit eaf5dca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,12 @@ PyO3 versions, please see the [migration guide](https://pyo3.rs/main/migration.h
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Implement `IntoPy<PyObject>` for `&PathBuf` and `&OsString`. [#1721](https://github.com/PyO3/pyo3/pull/1712)

## [0.14.0] - 2021-07-03

### Packaging
Expand Down
7 changes: 7 additions & 0 deletions src/conversions/osstr.rs
Expand Up @@ -145,6 +145,12 @@ impl IntoPy<PyObject> for OsString {
}
}

impl<'a> IntoPy<PyObject> for &'a OsString {
fn into_py(self, py: Python) -> PyObject {
self.to_object(py)
}
}

#[cfg(test)]
mod test {
use crate::{types::PyString, IntoPy, PyObject, Python, ToPyObject};
Expand Down Expand Up @@ -205,6 +211,7 @@ mod test {
let os_str = OsStr::new("Hello\0\n🐍");
test_roundtrip::<&OsStr>(py, os_str);
test_roundtrip::<OsString>(py, os_str.to_os_string());
test_roundtrip::<&OsString>(py, &os_str.to_os_string());
})
}
}
9 changes: 8 additions & 1 deletion src/conversions/path.rs
Expand Up @@ -66,6 +66,12 @@ impl IntoPy<PyObject> for PathBuf {
}
}

impl<'a> IntoPy<PyObject> for &'a PathBuf {
fn into_py(self, py: Python) -> PyObject {
self.as_os_str().to_object(py)
}
}

#[cfg(test)]
mod test {
use crate::{types::PyString, IntoPy, PyObject, Python, ToPyObject};
Expand Down Expand Up @@ -120,11 +126,12 @@ mod test {
let pystring: &PyString = pyobject.extract(py).unwrap();
assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy());
let roundtripped_obj: PathBuf = pystring.extract().unwrap();
assert!(obj.as_ref() == roundtripped_obj.as_path());
assert_eq!(obj.as_ref(), roundtripped_obj.as_path());
}
let path = Path::new("Hello\0\n🐍");
test_roundtrip::<&Path>(py, path);
test_roundtrip::<PathBuf>(py, path.to_path_buf());
test_roundtrip::<&PathBuf>(py, &path.to_path_buf());
})
}
}

0 comments on commit eaf5dca

Please sign in to comment.