Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5655.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct `IntoPyObject` output type of `PyBackedStr` to be `PyString`, not `PyAny`.
17 changes: 10 additions & 7 deletions src/pybacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PyAny>,
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
storage: Py<PyString>,
#[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))]
storage: Py<PyBytes>,
data: NonNull<str>,
}

Expand Down Expand Up @@ -73,7 +76,7 @@ impl TryFrom<Bound<'_, PyString>> 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,
})
}
Expand All @@ -83,7 +86,7 @@ impl TryFrom<Bound<'_, PyString>> 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,
})
}
Expand All @@ -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;

Expand All @@ -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<Self::Output, Self::Error> {
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;

Expand All @@ -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<Self::Output, Self::Error> {
Ok(PyString::new(py, self).into_any())
Ok(PyString::new(py, self))
}
}

Expand Down
Loading