[Python-Dev] PEP 344: Exception Chaining and Embedded Tracebacks

Guido van Rossum gvanrossum at gmail.com
Tue May 17 16:41:42 CEST 2005


I figured out the semantics that I'd like to see intuitively for
setting the context. I'm not saying this is all that reasonable, but
I'd like throw it out anyway to see what responses it gets.

Consider

    try:
        BLOCK
    except EXCEPTION, VAR:
        HANDLER

I'd like to see this translated into

    try:
        BLOCK
    except EXCEPTION, VAR:
        __context = VAR
        try:
            HANDLER
        except Exception, __error:
            __error.__context__ = __context
            raise

i.e. the context is set only upon *leaving* the handler. (The
translation for finally is hairier but you get the idea at this
point.)

My intuition prefers this over Ping's solution because HANDLER could
easily invoke code that (after many stack levels deep) raises and
catches many exceptions, and I'd hate to see all those be bothered by
the context (far down on the stack) that is irrelevant.

BTW, please study how the traceback is built up. I believe that if we
store the traceback in the exception instance, we have to update the
__traceback__ attribute each time we pop a stack level. IIRC that's
how the traceback chain is built up. (The alternative, building the
whole chain  when the exception is raised, would be too expensive for
exceptions raised *and* caught somewhere deep.)

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


More information about the Python-Dev mailing list