Open mattip opened 1 year ago
(This one sounds like deja vu to me -- I wonder if we've done a similar inventory in the past.)
Another problem with PyIter_Next
is that its NULL
return is ambiguous -- it can mean an actual error occurred, or the end was reached. This particular API seems to be poorly designed, and we ought to replace it with something else that looks more complex but avoids the issues.
Note also that setting a StopIteration
in tp_nextitem
is optional. To signal the end of iteration it should either set a StopIteration
and return NULL, or return NULL without setting any error.
Consider this code:
There are two ways to convert
next(an)
to C: eitherPyIter_Next(an)
oran.tp_iternext()
. There is a subtle difference:PyIter_Next
will return NULL, signaling that the iteration is complete, and clear theStopIteration
exception, erasing theex.args
. Callingtp_iternext
will not clear the exception, so theyield
value will be available through theex.args
. This came up in cython since it tends not to use the slot functions on non-CPython (in this case PyPy). As far as I can tell, there is noPyIter_*
function to only calltp_iternext
without clearing the exception.