[Python-ideas] Yield-From: Finalization guarantees

Jacob Holm jh at improva.dk
Tue Mar 31 03:30:47 CEST 2009


Greg Ewing wrote:
> Jacob Holm wrote:
>
>> Even without changing throw and close, I still think we should 
>> forward GeneratorExit like any other exception, and not do anything 
>> special to reraise it or call close on the subiterator.
>
> But that allows you to inadvertently create a broken
> generator by calling another generator that, according to
> the rules you've just acknowledged we can't change, is
> behaving correctly.

According to the rules for generator finalization it might behave 
correctly.  However, in most cases this will be code that is breaking 
the rule about not catching KeyboardInterrupt and SystemExit.  This is 
broken code IMNSHO, and I don't think we should complicate the 
yield-from expression to cater for it.   Yes there might be existing 
code that is not broken even by that standard and that still converts 
GeneratorExit to StopIteration.  I don't think that is common enough 
that we have to care.  If you use such a generator in a yield-from 
expression, you will get a RuntimeError('generator ignored 
GeneratorExit') on close, telling you that something is wrong.

>
> Asking users not to call such generators would require
> them to have knowledge about the implementation of every
> generator they call, which I don't think is acceptable.
>

I think that getting a RuntimeError on close is sufficient indication 
that such a generator should not be used in yield-from.

That said, I don't really care much either way.  Both versions are 
acceptable to me, and it is your PEP.

- Jacob





More information about the Python-ideas mailing list