[Python-Dev] A cute new way to get an infinite loop

Tim Peters tim.peters at gmail.com
Mon Sep 27 00:06:00 CEST 2004


[Raymond Hettinger]
> ...
> One situation did look suspect.  _PySequence_IterSearch() remembers an
> index/count across calls to PyIter_Next() -- it looks like the worst
> that could happen is the index or count would be wrong, but no crashers.

If the operation is PY_ITERSEARCH_INDEX, n is the 0-based count of the
number of times the iterator got poked before the object was found. 
That's always correct, by definition (given that there's no guarantee
the iterator can be rewound and restarted, or even that it would yield
the same objects if it could be restarted, what else could "the index
of the first occurrence" mean?).

If the operation is PY_ITERSEARCH_COUNT, then n is the number of times
poking the iterator returned the object in question.  That's also
correct by defintion of what PySequence_Count() means, although
there's again no guarantee that the user passes a sensible iterable
object (== one that would produce the same objects if crawled over a
second time).

So those are fine.

Thanks for checking the others, and for checking in a test and the fix!


More information about the Python-Dev mailing list