Skip to content

Commit

Permalink
use simplified PyIter_Check on CPython 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 3, 2023
1 parent f495322 commit 9094210
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
1 change: 1 addition & 0 deletions newsfragments/2914.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FFI definition `PyIter_Check` on CPython 3.7 now does the equivalent for `hasattr(type(obj), "__next__")`, which works correctly on all platforms and adds support for `abi3`.
15 changes: 5 additions & 10 deletions pyo3-ffi/src/abstract_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,14 @@ extern "C" {
}

// Before 3.8 PyIter_Check was defined in CPython as a macro,
// which uses Py_TYPE so cannot work on the limited ABI.
// but the implementation of that in PyO3 did not work, see
// https://github.com/PyO3/pyo3/pull/2914
//
// From 3.10 onwards CPython removed the macro completely,
// so PyO3 only uses this on 3.7 unlimited API.
#[cfg(not(any(Py_3_8, Py_LIMITED_API, PyPy)))]
// This is a slow implementation which should function equivalently.
#[cfg(not(any(Py_3_8, PyPy)))]
#[inline]
pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int {
(match (*crate::Py_TYPE(o)).tp_iternext {
Some(tp_iternext) => {
tp_iternext as *const std::os::raw::c_void != crate::_PyObject_NextNotImplemented as _
}
None => false,
}) as c_int
crate::PyObject_HasAttrString(crate::Py_TYPE(o), "__next__\0".as_ptr())
}

extern "C" {
Expand Down
7 changes: 1 addition & 6 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython

use crate::{ffi, AsPyPointer, IntoPyPointer, Py, PyAny, PyErr, PyNativeType, PyResult, Python};
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
use crate::{PyDowncastError, PyTryFrom};

/// A Python iterator object.
Expand All @@ -29,7 +28,6 @@ use crate::{PyDowncastError, PyTryFrom};
#[repr(transparent)]
pub struct PyIterator(PyAny);
pyobject_native_type_named!(PyIterator);
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
pyobject_native_type_extract!(PyIterator);

impl PyIterator {
Expand Down Expand Up @@ -64,7 +62,6 @@ impl<'p> Iterator for &'p PyIterator {
}

// PyIter_Check does not exist in the limited API until 3.8
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
impl<'v> PyTryFrom<'v> for PyIterator {
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v PyIterator, PyDowncastError<'v>> {
let value = value.into();
Expand Down Expand Up @@ -218,7 +215,7 @@ def fibonacci(target):
}

#[test]
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]

fn iterator_try_from() {
Python::with_gil(|py| {
let obj: Py<PyAny> = vec![10, 20].to_object(py).as_ref(py).iter().unwrap().into();
Expand Down Expand Up @@ -250,7 +247,6 @@ def fibonacci(target):
}

#[test]
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
#[cfg(feature = "macros")]
fn python_class_not_iterator() {
use crate::PyErr;
Expand Down Expand Up @@ -298,7 +294,6 @@ def fibonacci(target):
}

#[test]
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
#[cfg(feature = "macros")]
fn python_class_iterator() {
#[crate::pyfunction(crate = "crate")]
Expand Down

0 comments on commit 9094210

Please sign in to comment.