[Python-ideas] Revised**7 PEP on Yield-From

Jacob Holm jh at improva.dk
Fri Mar 20 10:33:58 CET 2009


Greg Ewing wrote:
> Jacob Holm wrote:
>
>> Of course.   Here is a demonstration/test...
>>
>> However, when I run this using your patch, the first 3 "Close" 
>> messages, and the 3 "GeneratorExit" messages are missing.
>
> I don't understand why you expect to get the output
> you present. Can you explain your reasoning with
> reference to the relevant sections of the relevant
> PEPs that you mention?
>
Starting with "Close".  The only reason I excpect *any* "Close" message 
is that the expansion in your PEP explicitly calls close in the finally 
clause. It makes no distinction between different ways of exiting the 
block, so I'd expect one call for each time it is exited.


The "GeneratorExit", I expect due to the description of close in PEP 342:

def close(self):
    try:
        self.throw(GeneratorExit)
    except (GeneratorExit, StopIteration):
        pass
    else:
        raise RuntimeError("generator ignored GeneratorExit")

When the generator is closed (due to the del g lines in the example), 
this says to throw a GeneratorExit and handle the result.  If we do this 
manually, the throw will be delegated to the iterator, which will print 
the "Throw: (<type 'exceptions.GeneratorExit'>,)" message. 


Do I make sense yet?

- Jacob




More information about the Python-ideas mailing list