-
Notifications
You must be signed in to change notification settings - Fork 763
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
Added ToPyObject
and IntoPy<PyObject>
impls for PyBackedStr
.
#4205
Conversation
CodSpeed Performance ReportMerging #4205 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think this implementation makes complete sense and probably should have been added when I first added the type 🤦
I think PyBackedBytes
could use the same treatment, perhaps?
Also, CI is failing.
…ls to the case where `cfg(any(Py_3_10, not(Py_LIMITED_API)))`. When this cfg does not apply, the conversion is less trivial since the `storage` is actually `PyBytes`, not `PyString`.
src/pybacked.rs
Outdated
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] | ||
impl ToPyObject for PyBackedStr { | ||
fn to_object(&self, py: Python<'_>) -> Py<PyAny> { | ||
self.storage.clone_ref(py) | ||
} | ||
} | ||
|
||
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cfgs are unfortunate; I think it should still be possible to write the implementations even if it's a bit more work.
You are right to call out the complexity though; we should add some tests for these methods :)
I actually just noticed that the conversion is not as trivial when
Regarding the CI, do you have any tips on why it would be failing and what I can do to fix that? The change seems too small to actually effect anything |
https://github.com/PyO3/pyo3/actions/runs/9238604511/job/25416822459?pr=4205#step:7:23 It looks like |
Something like |
…`cfg(not(any(Py_3_10, not(Py_LIMITED_API))))` case by converting the `PyBytes` back to `PyString`.
I guess I was overthinking it because I was expecting to need some ffi wizardry like I updated the impls and will look into adding some tests. |
I went ahead and added What do you think @davidhewitt? I also added a couple tests, but let me know if you have any suggestions to improve them. |
…oduce `PyBytes` regardless of the backing variant. Updated tests to demonstrate this.
I changed my mind and updated the |
…BackedStr` instead of `&str` since the latter is not supported under some `cfg`'s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks great to me!
Overall I agree that getting bytes
out in all cases is most user-friendly. They can always create a bytearray
manually when needed.
Just one bit which I think needs fixing, then let's merge 👍
… both storage types as intended. Updated test to properly catch the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super, LGTM! Thanks 👍
I think one of the runners crashed and the checks need to be restarted |
I have a
PyBackedStr
that I extracted from aPyObject
, and would like to pass it back to Python through another method call. However,PyBackedStr
currently does not implement the conversion traits necessary to do so without cloning. This PR simply adds these conversion methods, which is trivial since thePyBackedStr
is already storing a pointer to the Python string as aPyObject
.