Added ToPyObject and IntoPy<PyObject> impls for PyBackedStr.#4205
Added ToPyObject and IntoPy<PyObject> impls for PyBackedStr.#4205davidhewitt merged 12 commits intoPyO3:mainfrom
ToPyObject and IntoPy<PyObject> impls for PyBackedStr.#4205Conversation
CodSpeed Performance ReportMerging #4205 will not alter performanceComparing Summary
|
davidhewitt
left a comment
There was a problem hiding this comment.
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.
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.
davidhewitt
left a comment
There was a problem hiding this comment.
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.
|
I think one of the runners crashed and the checks need to be restarted |
I have a
PyBackedStrthat I extracted from aPyObject, and would like to pass it back to Python through another method call. However,PyBackedStrcurrently does not implement the conversion traits necessary to do so without cloning. This PR simply adds these conversion methods, which is trivial since thePyBackedStris already storing a pointer to the Python string as aPyObject.