diff --git a/newsfragments/5655.fixed.md b/newsfragments/5655.fixed.md new file mode 100644 index 00000000000..e5b811d511d --- /dev/null +++ b/newsfragments/5655.fixed.md @@ -0,0 +1 @@ +Correct `IntoPyObject` output type of `PyBackedStr` to be `PyString`, not `PyAny`. diff --git a/src/pybacked.rs b/src/pybacked.rs index ab4fe7351c0..d8b6db30c2a 100644 --- a/src/pybacked.rs +++ b/src/pybacked.rs @@ -16,7 +16,10 @@ use std::{convert::Infallible, ops::Deref, ptr::NonNull, sync::Arc}; /// This type gives access to the underlying data via a `Deref` implementation. #[cfg_attr(feature = "py-clone", derive(Clone))] pub struct PyBackedStr { - storage: Py, + #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] + storage: Py, + #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] + storage: Py, data: NonNull, } @@ -73,7 +76,7 @@ impl TryFrom> for PyBackedStr { let s = py_string.to_str()?; let data = NonNull::from(s); Ok(Self { - storage: py_string.into_any().unbind(), + storage: py_string.unbind(), data, }) } @@ -83,7 +86,7 @@ impl TryFrom> for PyBackedStr { let s = unsafe { std::str::from_utf8_unchecked(bytes.as_bytes()) }; let data = NonNull::from(s); Ok(Self { - storage: bytes.into_any().unbind(), + storage: bytes.unbind(), data, }) } @@ -103,7 +106,7 @@ impl FromPyObject<'_, '_> for PyBackedStr { } impl<'py> IntoPyObject<'py> for PyBackedStr { - type Target = PyAny; + type Target = PyString; type Output = Bound<'py, Self::Target>; type Error = Infallible; @@ -117,12 +120,12 @@ impl<'py> IntoPyObject<'py> for PyBackedStr { #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, &self).into_any()) + Ok(PyString::new(py, &self)) } } impl<'py> IntoPyObject<'py> for &PyBackedStr { - type Target = PyAny; + type Target = PyString; type Output = Bound<'py, Self::Target>; type Error = Infallible; @@ -136,7 +139,7 @@ impl<'py> IntoPyObject<'py> for &PyBackedStr { #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, self).into_any()) + Ok(PyString::new(py, self)) } }