Merge pull request #1712 from messense/pathbuf-into-py

Implement `IntoPy<PyObject>` for `&PathBuf` and `&OsString`
This commit is contained in:
messense 2021-07-04 16:45:16 +08:00 committed by GitHub
commit 9cdb6a0d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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};
@ -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());
})
}
}

View File

@ -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};
@ -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());
})
}
}