Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IntoPy<PyObject> for &PathBuf and &OsString #1712

Merged
merged 1 commit into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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());
})
}
}