[Python-Dev] PEP 343 - Abstract Block Redux

Nick Coghlan ncoghlan at gmail.com
Mon May 16 13:53:01 CEST 2005


Guido van Rossum wrote:
> [Nick Coghlan]
> 
>>>http://members.iinet.net.au/~ncoghlan/public/pep-3XX.html

> I have the same question for Nick. Interestingly, assuming Nick meant
> that "raise" to be there, PEP 3XX and PEP 343 now have the same
> translation. In rev 1.10 I moved the __enter__ call out of the
> try-block again. Having it inside was insane: when __enter__ fails, it
> should do its own cleanup rather than expecting __exit__ to clean up
> after a partial __enter__.

Are you sure? The copy I see on python.org still has it inside the try/finally.

But yes, the differences between PEP 343 and PEP 3XX [1] are not huge, 
particularly if __enter__ is called outside the try/finally block.

The key difference is whether or not exceptions are injected into the generators 
internal frame so that templates can be written using the style from PEP 340.

> But some of the claims from PEP 3XX seem to be incorrect now: Nick
> claims that a with-statement can abstract an except clause, but that's
> not the case; an except clause causes the control flow to go forward
> (continue after the whole try statement) but the with-statement (with
> the "raise" added) always acts like a finally-clause, which implicitly
> re-raises the exception.

Steven's correct - there's a raise statement missing. The point I'm trying to 
make in the PEP is that, even without the ability to suppress exceptions, the 
__exit__() statement can still react to them. Then the only code that needs to 
be repeated at the calling site is the actual suppression of the exception.

Whether doing such a thing makes sense is going to be application dependent, of 
course.

> Anyway, I think we may be really close at this point, if we can agree
> on an API for passing exceptions into generators and finalizing them,
> so that the generator can be written using a try/finally around the
> yield statement.

My PEP punts on providing a general API for passing exceptions into generators 
by making it an internal operation.

The version I submitted to the PEP editors uses __enter__() and __exit__() to 
handle finalisation, though.

Cheers,
Nick.

[1] I finally submitted it to the PEP editors, so it'll be up on python.org as 
soon as they find the time to check it in.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com


More information about the Python-Dev mailing list