This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients behzad.nouri, rhettinger
Date 2015-05-10.22:50:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1431298211.46.0.806715938742.issue24161@psf.upfronthosting.co.za>
In-reply-to
Content
The PyIter_Check() macro in Include/abstract.h does a quick test to see whether the tp_iternext slot is null or marked as not implemented.  That works for builtin types but not for user defined classes (heap types).

Old-style instances, see Objects/classobject.c::instance_iternext(), all define iternext with code that attempts lookup and call to the next() method, and if not it is not found, raises the TypeError you are seeing.

The conflict is that PyIter_Check() aims to be a fast check of a static slot entry while instance_iternext() aims for a dynamic call-it-and-see-if-it-works check much like PyObject_HasAttr() does.

Since this code is very old (back to Python 2.2) and has been mostly harmless (as far as we know), one resolution would be to just document this as a known limitation of PyIter_Check().  Rather than using PyIter_Check(), extensions should just call next() on object and see whether it succeeds.
History
Date User Action Args
2015-05-10 22:50:11rhettingersetrecipients: + rhettinger, behzad.nouri
2015-05-10 22:50:11rhettingersetmessageid: <1431298211.46.0.806715938742.issue24161@psf.upfronthosting.co.za>
2015-05-10 22:50:11rhettingerlinkissue24161 messages
2015-05-10 22:50:11rhettingercreate