[Python-Dev] Merging PEP 310 and PEP 340-redux?

Steven Bethard steven.bethard at gmail.com
Wed May 11 23:44:40 CEST 2005


On 5/11/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
> The gist is that the alternative is to require an __exit__() method to raise
> TerminateBlock in order to suppress an exception.

So I didn't see any examples that really needed TerminateBlock to
suppress an exception.  If the semantics of a do-statement was simply:

    stmt = EXPR1
    try:
        stmt_enter = stmt.__enter__
        stmt_exit = stmt.__exit__
    except AttributeError:
        raise TypeError("User defined statement template required")
    
    VAR1 = stmt_enter() # Omit 'VAR1 =' if no 'as' clause
    exc = ()
    try:
        try:
            BLOCK1
        except:
            exc = sys.exc_info()
    finally:
        stmt_exit(*exc)

would this make any of the examples impossible to write?  All you have
to do to suppress an exception is to not reraise it in __exit__. 
These semantics would make a normally completed BLOCK1 look like a
BLOCK1 exited through return, break or continue, but do we have any
use cases that actually need this distinction?  I couldn't see any,
but I've been reading *way* too many PEP 310/340 posts so probably my
eyes are just tired. ;-)

If you want the default to be that the exception gets re-raised
(instead of being suppressed as it is above), I think you could just
change the finally block to something like:

    finally:
        if stmt_exit(*exc):
           raise exc[0], exc[1], exc[2] 

That would mean that if any nonzero object was returned from __exit__,
the exception would be reraised.

But, like I say, I've been reading this thread way too much, so I'm
probably just missing the TerminateBlock use cases. =)

STeVe
-- 
You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list