Fixes for PyIterator

This commit is contained in:
Alex Gaynor 2020-10-18 11:06:21 -04:00
parent ba6f0ecdfa
commit 265db337c2
4 changed files with 18 additions and 6 deletions

View file

@ -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)

View file

@ -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;

View file

@ -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<V: Into<&'v PyAny>>(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();

View file

@ -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)]