Merge pull request #3389 from alex/less-as-py-pointer

Migrate `PyIterator::from_object` and `PyByteArray::from` from `AsPyPointer` to `&PyAny`
This commit is contained in:
David Hewitt 2023-08-15 23:12:28 +00:00 committed by GitHub
commit 82b1e55e2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 16 deletions

View File

@ -0,0 +1 @@
`PyIterator::from_object` and `PyByteArray::from` now take a single argument of type `&PyAny`, rather than two arguments, `Python` and `AsPyPointer`

View File

@ -818,7 +818,7 @@ impl PyAny {
/// This is typically a new iterator but if the argument is an iterator, /// This is typically a new iterator but if the argument is an iterator,
/// this returns itself. /// this returns itself.
pub fn iter(&self) -> PyResult<&PyIterator> { pub fn iter(&self) -> PyResult<&PyIterator> {
PyIterator::from_object(self.py(), self) PyIterator::from_object(self)
} }
/// Returns the Python type object for this object's type. /// Returns the Python type object for this object's type.

View File

@ -64,11 +64,11 @@ impl PyByteArray {
/// Creates a new Python `bytearray` object from another Python object that /// Creates a new Python `bytearray` object from another Python object that
/// implements the buffer protocol. /// implements the buffer protocol.
pub fn from<'p, I>(py: Python<'p>, src: &'p I) -> PyResult<&'p PyByteArray> pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
where unsafe {
I: AsPyPointer, src.py()
{ .from_owned_ptr_or_err(ffi::PyByteArray_FromObject(src.as_ptr()))
unsafe { py.from_owned_ptr_or_err(ffi::PyByteArray_FromObject(src.as_ptr())) } }
} }
/// Gets the length of the bytearray. /// Gets the length of the bytearray.
@ -305,7 +305,7 @@ mod tests {
let bytearray = PyByteArray::new(py, src); let bytearray = PyByteArray::new(py, src);
let ba: PyObject = bytearray.into(); let ba: PyObject = bytearray.into();
let bytearray = PyByteArray::from(py, &ba).unwrap(); let bytearray = PyByteArray::from(ba.as_ref(py)).unwrap();
assert_eq!(src, unsafe { bytearray.as_bytes() }); assert_eq!(src, unsafe { bytearray.as_bytes() });
}); });
@ -314,7 +314,7 @@ mod tests {
#[test] #[test]
fn test_from_err() { fn test_from_err() {
Python::with_gil(|py| { Python::with_gil(|py| {
if let Err(err) = PyByteArray::from(py, &py.None()) { if let Err(err) = PyByteArray::from(py.None().as_ref(py)) {
assert!(err.is_instance_of::<exceptions::PyTypeError>(py)); assert!(err.is_instance_of::<exceptions::PyTypeError>(py));
} else { } else {
panic!("error"); panic!("error");

View File

@ -124,7 +124,7 @@ mod impl_ {
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
PyFrozenSetIterator { PyFrozenSetIterator {
it: PyIterator::from_object(self.py(), self).unwrap(), it: PyIterator::from_object(self).unwrap(),
} }
} }
} }

View File

@ -30,11 +30,11 @@ impl PyIterator {
/// Constructs a `PyIterator` from a Python iterable object. /// Constructs a `PyIterator` from a Python iterable object.
/// ///
/// Equivalent to Python's built-in `iter` function. /// Equivalent to Python's built-in `iter` function.
pub fn from_object<'p, T>(py: Python<'p>, obj: &T) -> PyResult<&'p PyIterator> pub fn from_object(obj: &PyAny) -> PyResult<&PyIterator> {
where unsafe {
T: AsPyPointer, obj.py()
{ .from_owned_ptr_or_err(ffi::PyObject_GetIter(obj.as_ptr()))
unsafe { py.from_owned_ptr_or_err(ffi::PyObject_GetIter(obj.as_ptr())) } }
} }
} }
@ -209,7 +209,7 @@ def fibonacci(target):
fn int_not_iterable() { fn int_not_iterable() {
Python::with_gil(|py| { Python::with_gil(|py| {
let x = 5.to_object(py); let x = 5.to_object(py);
let err = PyIterator::from_object(py, &x).unwrap_err(); let err = PyIterator::from_object(x.as_ref(py)).unwrap_err();
assert!(err.is_instance_of::<PyTypeError>(py)); assert!(err.is_instance_of::<PyTypeError>(py));
}); });

View File

@ -149,7 +149,7 @@ mod impl_ {
/// If PyO3 detects that the set is mutated during iteration, it will panic. /// If PyO3 detects that the set is mutated during iteration, it will panic.
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
PySetIterator { PySetIterator {
it: PyIterator::from_object(self.py(), self).unwrap(), it: PyIterator::from_object(self).unwrap(),
} }
} }
} }