From 265db337c2c5047c06eb0dd9b7a4ef55a1950028 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 18 Oct 2020 11:06:21 -0400 Subject: [PATCH] Fixes for PyIterator --- CHANGELOG.md | 1 + src/ffi/objectabstract.rs | 5 +++++ src/types/iterator.rs | 14 ++++++++++---- src/types/set.rs | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dd5203f..0c4f4d7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added - Add support for building for CPython limited API. This required a few minor changes to runtime behaviour of of pyo3 `#[pyclass]` types. See the migration guide for full details. [#1152](https://github.com/PyO3/pyo3/pull/1152) - Add argument names to `TypeError` messages generated by pymethod wrappers. [#1212](https://github.com/PyO3/pyo3/pull/1212) diff --git a/src/ffi/objectabstract.rs b/src/ffi/objectabstract.rs index 76154c33..bead5ebf 100644 --- a/src/ffi/objectabstract.rs +++ b/src/ffi/objectabstract.rs @@ -143,6 +143,11 @@ pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int { }) as c_int } +#[cfg(all(Py_LIMITED_API, Py_3_8))] +extern "C" { + pub fn PyIter_Check(obj: *mut PyObject) -> c_int; +} + extern "C" { #[cfg_attr(PyPy, link_name = "PyPyIter_Next")] pub fn PyIter_Next(arg1: *mut PyObject) -> *mut PyObject; diff --git a/src/types/iterator.rs b/src/types/iterator.rs index cc798ebf..ea0f2dc9 100644 --- a/src/types/iterator.rs +++ b/src/types/iterator.rs @@ -2,9 +2,9 @@ // // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython -use crate::{ - ffi, AsPyPointer, PyAny, PyDowncastError, PyErr, PyNativeType, PyResult, PyTryFrom, Python, -}; +use crate::{ffi, AsPyPointer, PyAny, PyErr, PyNativeType, PyResult, Python}; +#[cfg(any(not(Py_LIMITED_API), Py_3_8))] +use crate::{PyDowncastError, PyTryFrom}; /// A Python iterator object. /// @@ -28,6 +28,7 @@ use crate::{ #[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 { @@ -67,6 +68,8 @@ 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>(value: V) -> Result<&'v PyIterator, PyDowncastError<'v>> { let value = value.into(); @@ -96,7 +99,9 @@ mod tests { use crate::exceptions::PyTypeError; use crate::gil::GILPool; use crate::types::{PyDict, PyList}; - use crate::{Py, PyAny, PyTryFrom, Python, ToPyObject}; + #[cfg(any(not(Py_LIMITED_API), Py_3_8))] + use crate::{Py, PyAny, PyTryFrom}; + use crate::{Python, ToPyObject}; use indoc::indoc; #[test] @@ -200,6 +205,7 @@ mod tests { } #[test] + #[cfg(any(not(Py_LIMITED_API), Py_3_8))] fn iterator_try_from() { let gil_guard = Python::acquire_gil(); let py = gil_guard.python(); diff --git a/src/types/set.rs b/src/types/set.rs index d631c080..5593490a 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -118,8 +118,8 @@ impl PySet { } #[cfg(Py_LIMITED_API)] -pub struct PySetIterator<'py> { - it: PyIterator<'py>, +pub struct PySetIterator<'p> { + it: &'p PyIterator, } #[cfg(Py_LIMITED_API)]