Skip to content

Convert PathBuf & Path into python pathlib.Path instead of PyString#4925

Merged
Icxolu merged 3 commits intoPyO3:mainfrom
bschoenmaeckers:pathbuf-pathlib
Feb 26, 2025
Merged

Convert PathBuf & Path into python pathlib.Path instead of PyString#4925
Icxolu merged 3 commits intoPyO3:mainfrom
bschoenmaeckers:pathbuf-pathlib

Conversation

@bschoenmaeckers
Copy link
Member

@bschoenmaeckers bschoenmaeckers commented Feb 19, 2025

closes #4907

@bschoenmaeckers bschoenmaeckers force-pushed the pathbuf-pathlib branch 3 times, most recently from 7a4cd73 to 5adfc6c Compare February 20, 2025 09:46
@LilyFirefly
Copy link
Contributor

How do we handle paths that are not utf-8? I believe both Rust and Python have ways to represent these, but I think especially on the Rust side, this is considered an implementation detail and I'm not sure we can safely convert between them.

@Icxolu
Copy link
Member

Icxolu commented Feb 21, 2025

Not a full review (yet), but I think we need to keep the current behavior for the deprecated IntoPy and ToPyObject impls.

@bschoenmaeckers
Copy link
Member Author

How do we handle paths that are not utf-8? I believe both Rust and Python have ways to represent these, but I think especially on the Rust side, this is considered an implementation detail and I'm not sure we can safely convert between them.

We are using the &OsStr conversions defined in the ostr.rs file. I did not change that in this MR. Do you have an example of a non-utf8 string that may cause problems? So I can add it as a test case.

@LilyFirefly
Copy link
Contributor

How do we handle paths that are not utf-8? I believe both Rust and Python have ways to represent these, but I think especially on the Rust side, this is considered an implementation detail and I'm not sure we can safely convert between them.

We are using the &OsStr conversions defined in the ostr.rs file. I did not change that in this MR. Do you have an example of a non-utf8 string that may cause problems? So I can add it as a test case.

Oh, I didn't know we had that code, that's really cool! I think my concern is moot.

Copy link
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A side from keeping old conversion for IntoPy and ToPyObject I only have a small suggestion around the tests.

Comment on lines 187 to 194
let pystring = unsafe {
ffi::PyOS_FSPath(pyobject.as_ptr())
.assume_owned_or_err(py)
.unwrap()
.downcast_into::<PyString>()
.unwrap()
};
assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether it still makes sense to compare these as strings, now that we don't have a string representation on either side. Maybe we can just get rid of this intermediate check.

Instead I think we should add a new test that a PathBuf can be extracted from both a str and pathlib.Path, which was removed from this test.

Copy link
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you very much!

@Icxolu Icxolu added this pull request to the merge queue Feb 26, 2025
Merged via the queue into PyO3:main with commit ffd7276 Feb 26, 2025
48 checks passed
FirelightFlagboy added a commit to Scille/parsec-cloud that referenced this pull request Jul 30, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925
FirelightFlagboy added a commit to Scille/parsec-cloud that referenced this pull request Jul 30, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
FirelightFlagboy added a commit to Scille/parsec-cloud that referenced this pull request Jul 30, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
FirelightFlagboy added a commit to Scille/parsec-cloud that referenced this pull request Aug 5, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
github-merge-queue bot pushed a commit to Scille/parsec-cloud that referenced this pull request Aug 5, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
github-merge-queue bot pushed a commit to Scille/parsec-cloud that referenced this pull request Aug 5, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
github-merge-queue bot pushed a commit to Scille/parsec-cloud that referenced this pull request Aug 5, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
github-merge-queue bot pushed a commit to Scille/parsec-cloud that referenced this pull request Aug 5, 2025
Remove `PathWrapper` as it was not used and `pyo3` now directly serialize `Path` & `PathBuf` to `pathlib.Path` following PyO3/pyo3#4925

Closes #10848
tpoliaw added a commit to tpoliaw/pyo3 that referenced this pull request Nov 4, 2025
Path::into_pyobject now returns a pathlib.Path object which doesn't work
as expected when added to sys.path so revert to string (see PyO3#4925).

`impl FromPyObject for PyRefMut` now returns a custom error type (that
doesn't impl `std::error::Error`) so map the error back the PyErr to
return it (see PyO3#5413).
tpoliaw added a commit to tpoliaw/pyo3 that referenced this pull request Nov 4, 2025
Path::into_pyobject now returns a pathlib.Path object which doesn't work
as expected when added to sys.path so revert to string (see PyO3#4925).

`impl FromPyObject for PyRefMut` now returns a custom error type (that
doesn't impl `std::error::Error`) so map the error back the PyErr to
return it (see PyO3#5413).
github-merge-queue bot pushed a commit that referenced this pull request Nov 8, 2025
Path::into_pyobject now returns a pathlib.Path object which doesn't work
as expected when added to sys.path so revert to string (see #4925).

`impl FromPyObject for PyRefMut` now returns a custom error type (that
doesn't impl `std::error::Error`) so map the error back the PyErr to
return it (see #5413).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support converting std::path::{Path, PathBuf} to pathlib.Path

3 participants