[Python-3000] sys.exc_info()

Antoine Pitrou solipsis at pitrou.net
Sat May 31 15:59:24 CEST 2008


Mark Hammond <mhammond <at> skippinet.com.au> writes:
> In both Python 2.x and 3 (a few months old build of Py3k though), the
> traceback isn't the same.  For Python 2.0 you could write it like:
> 
> def handle_exception():
> ...
>     raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
> 
> Its not clear how that would be spelt in py3k though (and from what I can
> see, sys.exc_info() itself has an uncertain future in py3k).

sys.exc_info() will remain, it's just that the returned value will be (None,
None, None) if we are not in an except block in any of the currently active
frames in the thread. In the case above it would return the current exception
(the one caught in one of the enclosing frames).

By the way, another interesting sys.exc_info() case:

def except_yield():
    try:
        raise TypeError
    except:
        yield 1

def f():
    for i in except_yield():
        return sys.exc_info()

Right now, running f() returns (None, None, None). But with rewritten exception
stacking, it may return the 3-tuple for the TypeError raised in except_yield().

Regards

Antoine.




More information about the Python-3000 mailing list