[Python-ideas] Yield-From: Finalization guarantees

Nick Coghlan ncoghlan at gmail.com
Wed Apr 1 13:28:28 CEST 2009

Jacob Holm wrote:
> Greg Ewing wrote:
>> Jacob Holm wrote:
>>> will also remove some behavior that could have been useful, such as
>>> the ability to suppress the GeneratorExit if you know what you are
>>> doing.
>> I'm not convinced there are any use cases for suppressing
>> GeneratorExit in the first place. Can you provide an
>> example that couldn't be easily done some other way?
> I don't have any real use cases, just a few examples of things you can
> do in #2 that become a bit uglier in #3 or #4.

You appear to be thinking of GeneratorExit as a way to ask a generator
to finish normally such that it still makes sense to try to return a
value after a GeneratorExit has been thrown in to the current frame, but
that really isn't its role.

Instead, it's more of an "Abandon Ship! Abandon Ship! All hands to the
lifeboats!" indication that gives the generator a chance to release any
resources it might be holding and bail out. The reason that close()
accepts a StopIteration as well as a GeneratorExit is that the former
still indicates that the generator has finalised itself, so the
objective of calling close() has been achieved and there is no need to
report an error.

Any code that catches GeneratorExit without reraising it is highly
suspect, just like code that suppresses SystemExit and KeyboardInterrupt.


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

More information about the Python-ideas mailing list