From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 5A09F3858C52 for ; Sat, 2 Apr 2022 19:07:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5A09F3858C52 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1naj5n-0002W4-Uk for cygwin@cygwin.com; Sat, 02 Apr 2022 21:07:23 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: cygwin@cygwin.com From: airplanemath Subject: Weird results from PyIter_Check Date: Sat, 02 Apr 2022 15:07:14 -0400 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" 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@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Apr 2022 19:07:28 -0000 --=-=-= 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 "", line 1, in 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 "", line 1, in 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: binary --=-=-=--