[Python-ideas] Yield-From: Finalization guarantees

Nick Coghlan ncoghlan at gmail.com
Wed Apr 1 16:22:51 CEST 2009


Jacob Holm wrote:
> Explicitly catching GeneratorExit and then returning is a valid use
> today that I wouldn't consider suspect.  Catching GeneratorExit and then
> exiting the except block by other means than a raise or return is
> suspect, but has valid uses.

What are these valid uses? The PEP 342 definition made some sense
originally when GeneratorExit was a subclass of Exception and hence easy
to suppress accidentally, but I have serious doubts about the validity
of trapping it and turning it into StopIteration now that it has been
moved out to inherit directly from BaseException.

Regardless, unless Greg goes out of his way to change the meaning of
close() in the PEP, GeneratorReturn will escape from close() (since that
only traps StopIteration). That means you'll be able to catch that
exception directly if you really want to, and if you don't it will
bubble up out of the original close() call that was made on the
outermost generator.

Cheers,
Nick.

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



More information about the Python-ideas mailing list