[Python-3000] sys.exc_info()

Antoine Pitrou solipsis at pitrou.net
Sat May 31 19:41:46 CEST 2008


Adam Olsen <rhamph <at> gmail.com> writes:
> > 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().
> 
> What exception stacking?  I thought we'd be using a simple per-thread
> exception.  I'd expect the yield statement to clear it, giving us
> (None, None, None).

There is a per-thread exception for the current exception state but we
must also save and restore the previous state when we enter and leave
an exception handler, respectively, so that re-raising and sys.exc_info()
work properly in situations with lexically nested exception handlers.

Also, "yield" cannot blindingly clear the exception state, because the frame 
calling the generator may except the exception state to be non-None.
Consequently, we might have to keep the f_exc_* members solely for the
generator case.




More information about the Python-3000 mailing list