From 1a904a7429a9a90629608291be4f7aea77782822 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 15 Aug 2023 17:44:54 -0400 Subject: [PATCH] Migrate `PyIterator::from_object` and `PyByteArray::from` from `AsPyPointer` to `&PyAny` --- newsfragments/3389.changed.md | 1 + src/types/any.rs | 2 +- src/types/bytearray.rs | 14 +++++++------- src/types/frozenset.rs | 2 +- src/types/iterator.rs | 12 ++++++------ src/types/set.rs | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 newsfragments/3389.changed.md diff --git a/newsfragments/3389.changed.md b/newsfragments/3389.changed.md new file mode 100644 index 00000000..2f695c7f --- /dev/null +++ b/newsfragments/3389.changed.md @@ -0,0 +1 @@ +`PyIterator::from_object` and `PyByteArray::from` now take a single argument of type `&PyAny`, rather than two arguments, `Python` and `AsPyPointer` diff --git a/src/types/any.rs b/src/types/any.rs index 0192bfa3..2ff65275 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -818,7 +818,7 @@ impl PyAny { /// This is typically a new iterator but if the argument is an iterator, /// this returns itself. 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. diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index 9c03ddec..1ec244a4 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -64,11 +64,11 @@ impl PyByteArray { /// Creates a new Python `bytearray` object from another Python object that /// implements the buffer protocol. - pub fn from<'p, I>(py: Python<'p>, src: &'p I) -> PyResult<&'p PyByteArray> - where - I: AsPyPointer, - { - unsafe { py.from_owned_ptr_or_err(ffi::PyByteArray_FromObject(src.as_ptr())) } + pub fn from(src: &PyAny) -> PyResult<&PyByteArray> { + unsafe { + src.py() + .from_owned_ptr_or_err(ffi::PyByteArray_FromObject(src.as_ptr())) + } } /// Gets the length of the bytearray. @@ -305,7 +305,7 @@ mod tests { let bytearray = PyByteArray::new(py, src); 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() }); }); @@ -314,7 +314,7 @@ mod tests { #[test] fn test_from_err() { 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::(py)); } else { panic!("error"); diff --git a/src/types/frozenset.rs b/src/types/frozenset.rs index d27e87ef..bbcd7102 100644 --- a/src/types/frozenset.rs +++ b/src/types/frozenset.rs @@ -124,7 +124,7 @@ mod impl_ { fn into_iter(self) -> Self::IntoIter { PyFrozenSetIterator { - it: PyIterator::from_object(self.py(), self).unwrap(), + it: PyIterator::from_object(self).unwrap(), } } } diff --git a/src/types/iterator.rs b/src/types/iterator.rs index 6bacb438..c70c08f6 100644 --- a/src/types/iterator.rs +++ b/src/types/iterator.rs @@ -30,11 +30,11 @@ impl PyIterator { /// Constructs a `PyIterator` from a Python iterable object. /// /// Equivalent to Python's built-in `iter` function. - pub fn from_object<'p, T>(py: Python<'p>, obj: &T) -> PyResult<&'p PyIterator> - where - T: AsPyPointer, - { - unsafe { py.from_owned_ptr_or_err(ffi::PyObject_GetIter(obj.as_ptr())) } + pub fn from_object(obj: &PyAny) -> PyResult<&PyIterator> { + unsafe { + obj.py() + .from_owned_ptr_or_err(ffi::PyObject_GetIter(obj.as_ptr())) + } } } @@ -209,7 +209,7 @@ def fibonacci(target): fn int_not_iterable() { Python::with_gil(|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::(py)); }); diff --git a/src/types/set.rs b/src/types/set.rs index e10979c2..68b89d65 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -149,7 +149,7 @@ mod impl_ { /// If PyO3 detects that the set is mutated during iteration, it will panic. fn into_iter(self) -> Self::IntoIter { PySetIterator { - it: PyIterator::from_object(self.py(), self).unwrap(), + it: PyIterator::from_object(self).unwrap(), } } }