delorie.com/archives/browse.cgi | search |
X-Recipient: | archive-cygwin AT delorie DOT com |
DKIM-Filter: | OpenDKIM Filter v2.11.0 sourceware.org 9D0283838221 |
DKIM-Signature: | v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; |
s=default; t=1653686833; | |
bh=vEHccCB9A8iL+SrbprO4HgRTTtUtk1hYB7w4FttU700=; | |
h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: | |
List-Help:List-Subscribe:From:Reply-To:From; | |
b=UjcYSCfNKg7MQQWRariMnjNE2y4PeO6OST9sUxKe+UaSGt+7RHbQDb8ooZIVgpB88 | |
84m4EUII6yEc8lJtKYLaP/kbOKQkUFTpTJZ3710DyP2xcVSswwCsGPh/+6fMjcxvRK | |
QdfPFywBQHBfHjt3G/8xkZ3WZilY5UcNzEF3d63U= | |
X-Original-To: | cygwin AT cygwin DOT com |
Delivered-To: | cygwin AT cygwin DOT com |
DMARC-Filter: | OpenDMARC Filter v1.4.1 sourceware.org 0DF1E386F82B |
X-Injected-Via-Gmane: | http://gmane.org/ |
To: | cygwin AT cygwin DOT com |
Subject: | PyIter_Check false positives |
Date: | Fri, 27 May 2022 17:26:30 -0400 |
Message-ID: | <vriur14ehdkp.fsf@mail.aol.com> |
Mime-Version: | 1.0 |
User-Agent: | Gnus/5.13 (Gnus v5.13) Emacs/28.1 (cygwin) |
Cancel-Lock: | sha1:DvRLrvcLAD+wPLidBUC98SwkWz0= |
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.6 | |
X-Spam-Checker-Version: | SpamAssassin 3.4.6 (2021-04-09) on |
server2.sourceware.org | |
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 I found this problem running the tests for pandas, but can reproduce it more simply. Compile the following cython file to create a thin wrapper around the C-API function: > from cpython.iterator cimport PyIter_Check > > def is_iterator(obj: object) -> bool: > return PyIter_Check(obj) with `cythonize --build --inplace test_iterator.pyx` (probably requires python39-cython), changing the name if needed, then try the following in Python: >>> from collections.abc import Iterator >>> from fractions import Fraction >>> from os import environ >>> from test_iterator import is_iterator >>> isinstance(environ, Iterator) False >>> is_iterator(environ) True >>> next(environ) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '_Environ' object is not an iterator >>> isinstance(Fraction(0, 1), Iterator) False >>> is_iterator(Fraction(0, 1)) True >>> next(Fraction(0, 1)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Fraction' object is not an iterator From the Python documentation [1], it appears that `PyIter_Check` is intended to return True only if the `PyIter_Next` will work, and `PyIter_Next` is the C equivalent of the python `next` function. That is, `isinstance(thing, Iterator)` and `is_iterator(thing)` should agree with each other, and should return True only if `next(thing)` works (produces an element or says there aren't any). On Linux, both checks agree with each other, returning False, and the attempts to advance the iterator with `next` still fail. As seen above, on (my) Cygwin, PyIter_Check disagrees with `collections.abc.Iterator` and succeeds in cases where `next` produces a TypeError. I can reproduce this with python3.9 and with python2.7 on Cygwin. Am I missing something in the documentation? Is this a side-effect of working on top of Windows? In case it's relevant, I'm using an unpatched Cython with the distribution Python packages. [1] https://docs.python.org/3/c-api/iter.html#c.PyIter_Check --=-=-= Content-Type: text/x-python3 Content-Disposition: inline; filename=test_iterator.pyx Content-Description: PyIter_Next Cython wrapper module source 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) --=-=-= 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 --=-=-=--
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |