[Python-ideas] Yield-From: Finalization guarantees

Nick Coghlan ncoghlan at gmail.com
Sun Mar 29 00:55:10 CET 2009

> However, with the current expansion they are different.  Only the
> version not using "yield from" will print "CLOSED" in this case:
> g = outer()
> g.next()   # prints 1
> g.close()  # should print "CLOSED", but doesn't because the
> GeneratorExit is reraised by yield-from
> I currently don't think that a special case for GeneratorExit is
> needed.  Can you give me an example showing that it is?

Take your example, replace the "print val" with a "yield val" and you
get a broken generator that will yield again when close() is called.

Generators that catch and do anything with GeneratorExit other than turn
it into StopIteration are almost always going to be broken - the new
expression needs to avoid making it easy to do that accidentally.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list