[Python-Dev] PEP 343 - Abstract Block Redux

Guido van Rossum gvanrossum at gmail.com
Mon May 16 02:50:55 CEST 2005


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

[Steven Bethard]
> there I see the semantics:
> 
> VAR1 = stmt_enter() # Omit 'VAR1 =' if no 'as' clause
> exc = (None, None, None)
> try:
>     try:
>         BLOCK1
>     except:
>         exc = sys.exc_info()
> finally:
>     stmt_exit(*exc)
> 
> Don't you want a "raise" after the "exc = sys.exc_info()"?

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__.

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. So, in particular, in this example:

with EXPR1:
    1/0
print "Boo"

the print statement is unreachable and there's nothing clever you can
put in an __exit__ method to make it reachable. Just like in this
case:

try:
    1/0
finally:
    BLOCK1
print "Boo"

there's nothing that BLOCK1 can to to cause the print statement to be reached.

This claim in PEP 3XX may be a remnant from a previous version; or it
may be that Nick misunderstands how 'finally' works.

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.

Of course, it's also possible that Nick did *not* mean for the missing
"raise" to be there. But in that case other claims from his PEP become
false, so I'm assuming with Steven here. Nick?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list