[Python-Dev] Termination of two-arg iter()

Oren Tirosh oren-py-d@hishome.net
Sun, 14 Jul 2002 09:33:24 -0400


On Sun, Jul 14, 2002 at 12:31:13AM -0400, Oren Tirosh wrote:
> On Sat, Jul 13, 2002 at 07:07:36PM -0400, Guido van Rossum wrote:
> > Actually, not.  Under "Resolved Issues" the PEP has this:
> > 
> >     - Once a particular iterator object has raised StopIteration, will
> >       it also raise StopIteration on all subsequent next() calls?
> >       Some say that it would be useful to require this, others say
> >       that it is useful to leave this open to individual iterators.
> 
> At the time this was discussed on the list has anyone considered the
> possibility of raising an exception?  Something like 'IteratorExhausted'?

An alternative approach would be to raise an exception when calling iter() on 
an exhausted iterator. This is orthogonal to whatever .next() does on such
and iterator.

Suggested implementation: when an iterator raises StopIteration it will
immediately clean up and decref any referenced objects and then alter its 
ob_type field to a special closed iterator type. This is similar to the
way closed files are handled - any attempt to perform I/O on them raises an
IOError. The behavior of this type's tp_iter and tp_iternext is open for 
discussion. This will "fix" the behavior of list iterators, for example, 
that can be revived by extending the list.  It's a matter of interpretation 
whether this is a bug or a feature, though.

	Oren