Skip to content

Commit 8b58f5d

Browse files
authored
use PyString as output type of PyBackedStr (#5655)
1 parent feb6db1 commit 8b58f5d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

newsfragments/5655.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Correct `IntoPyObject` output type of `PyBackedStr` to be `PyString`, not `PyAny`.

src/pybacked.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use std::{convert::Infallible, ops::Deref, ptr::NonNull, sync::Arc};
1616
/// This type gives access to the underlying data via a `Deref` implementation.
1717
#[cfg_attr(feature = "py-clone", derive(Clone))]
1818
pub struct PyBackedStr {
19-
storage: Py<PyAny>,
19+
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
20+
storage: Py<PyString>,
21+
#[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))]
22+
storage: Py<PyBytes>,
2023
data: NonNull<str>,
2124
}
2225

@@ -73,7 +76,7 @@ impl TryFrom<Bound<'_, PyString>> for PyBackedStr {
7376
let s = py_string.to_str()?;
7477
let data = NonNull::from(s);
7578
Ok(Self {
76-
storage: py_string.into_any().unbind(),
79+
storage: py_string.unbind(),
7780
data,
7881
})
7982
}
@@ -83,7 +86,7 @@ impl TryFrom<Bound<'_, PyString>> for PyBackedStr {
8386
let s = unsafe { std::str::from_utf8_unchecked(bytes.as_bytes()) };
8487
let data = NonNull::from(s);
8588
Ok(Self {
86-
storage: bytes.into_any().unbind(),
89+
storage: bytes.unbind(),
8790
data,
8891
})
8992
}
@@ -103,7 +106,7 @@ impl FromPyObject<'_, '_> for PyBackedStr {
103106
}
104107

105108
impl<'py> IntoPyObject<'py> for PyBackedStr {
106-
type Target = PyAny;
109+
type Target = PyString;
107110
type Output = Bound<'py, Self::Target>;
108111
type Error = Infallible;
109112

@@ -117,12 +120,12 @@ impl<'py> IntoPyObject<'py> for PyBackedStr {
117120

118121
#[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))]
119122
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
120-
Ok(PyString::new(py, &self).into_any())
123+
Ok(PyString::new(py, &self))
121124
}
122125
}
123126

124127
impl<'py> IntoPyObject<'py> for &PyBackedStr {
125-
type Target = PyAny;
128+
type Target = PyString;
126129
type Output = Bound<'py, Self::Target>;
127130
type Error = Infallible;
128131

@@ -136,7 +139,7 @@ impl<'py> IntoPyObject<'py> for &PyBackedStr {
136139

137140
#[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))]
138141
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
139-
Ok(PyString::new(py, self).into_any())
142+
Ok(PyString::new(py, self))
140143
}
141144
}
142145

0 commit comments

Comments
 (0)