I had a bit of confusion that there's not StopIteration traceback printed, and neither is "after that". Apparently an uncaught StopIteration call just exits silently? (I'm using 2.3a0 here, in the case that it matters---cvs from 2-3 weeks ago)
Is this about the example below?
from __future__ import generators
class HaltingIterator: def __init__(self, *args, **kw): self._stop = 0 self._generator = self.generator(*args, **kw)
def stop(self, arg=1): self._stop = arg self.cleanup(arg)
def next(self): if self._stop: raise StopIteration return self._generator.next()
def cleanup(self, arg): pass
class ExampleHaltingIterator(HaltingIterator): def generator(self): a, b = 1, 1 while 1: ret = a ret, a, b = a, b, a+b yield ret
def cleanup(self, arg): print "halted with", arg
x = ExampleHaltingIterator()
for i in range(10): print x.next() x.stop("76 trombones") print x.next() print "after that"
With current CVS this prints: 1 1 2 3 5 8 13 21 34 55 halted with 76 trombones Traceback (most recent call last): File "/tmp/x.py", line 35, in ? print x.next() File "/tmp/x.py", line 14, in next raise StopIteration StopIteration --Guido van Rossum (home page: http://www.python.org/~guido/)