Weird results from PyIter_Check

airplanemath airplanemath@aol.com
Sat Apr 2 19:07:14 GMT 2022


PyIter_Check appears to be the C-level equivalent of
isinstance(..., collections.abc.Iterator), testing whether the python
next() or the C PyIter_Next will succeed.  However, PyIter_Check
disagrees with collections.abc.Iterator and next about whether a given
object is an iterator.  

$ cat test_iterator.pyx
from cpython.iterator cimport PyIter_Check

def is_iterator(obj: object) -> bool:
    """Check whether obj is an iterator.

    Should agree with isinstance(obj, collections.abc.Iterator).

    Parameters
    ----------
    obj : object

    Returns
    -------
    bool
    """
    return PyIter_Check(obj)

$ cythonize --build --inplace test_iterator.pyx
running build_ext

$ python
Python 3.9.10 (main, Jan 20 2022, 21:37:52)
[GCC 11.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test_iterator, os, fractions, collections.abc
>>> test_iterator.is_iterator(os.environ)
True
>>> isinstance(os.environ, collections.abc.Iterator)
False
>>> next(os.environ)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '_Environ' object is not an iterator
>>> test_iterator.is_iterator(fractions.Fraction(0, 1))
True
>>> isinstance(fractions.Fraction(0, 1), collections.abc.Iterator)
False
>>> next(fractions.Fraction(0, 1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Fraction' object is not an iterator

On Linux, the test function using PyIter_Check agrees with
collections.abc.Iterator and next.  The test case that led me to this
behaviour works the same in Windows as on Linux.  Is this expected behavior?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/x-python3
Size: 1 bytes
Desc: not available
URL: <https://cygwin.com/pipermail/cygwin/attachments/20220402/96bc6cea/attachment.bin>


More information about the Cygwin mailing list