delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2022/04/02/15:08:37

X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DAD843858C52
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1648926514;
bh=c0Z24lcZs1M8QhiLlm6O9HHl5PpfK67Nx5hXPzXfQ3c=;
h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post:
List-Help:List-Subscribe:From:Reply-To:From;
b=QDSdJUzyigbPZVv1fvx1NJPMnyn4pLCPJ3WeHTL6Bx1qKd9BG88fdegF91b9OBhr1
cMVBqy5I9Js+iBmfWu/QE5PQqGjS/WyOUziyJJSw38phakyM7t0qf9ofTKUeOGBTQT
1VqjC/BuPv4uFo059EUpz7zooG+sfNCVX4EOSV7Q=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5A09F3858C52
X-Injected-Via-Gmane: http://gmane.org/
To: cygwin AT cygwin DOT com
Subject: Weird results from PyIter_Check
Date: Sat, 02 Apr 2022 15:07:14 -0400
Message-ID: <vriusfqvnw2l.fsf@mail.aol.com>
Mime-Version: 1.0
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (cygwin)
Cancel-Lock: sha1:4noxd25nKuzjmpVNv+XsFxVVHIc=
X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,
FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,
KAM_DMARC_STATUS, KAM_GOODAOL, SPF_HELO_NONE, SPF_PASS, TXREP,
T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4
X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on
server2.sourceware.org
X-Content-Filtered-By: Mailman/MimeDel 2.1.29
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.29
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: airplanemath via Cygwin <cygwin AT cygwin DOT com>
Reply-To: airplanemath <airplanemath AT aol DOT com>
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com>

--=-=-=
Content-Type: text/plain

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?


--=-=-=
Content-Type: text/x-python3
Content-Transfer-Encoding: quoted-printable



--=-=-=
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

--=-=-=--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019