Raymond Hettinger added the comment:

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.

----------
nosy: +rhettinger

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24161>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to