Why can't pdb access a variable containing an exception? Why can't pdb access a variable containing an exception? python-3.x python-3.x

Why can't pdb access a variable containing an exception?


In Python 3, the target of an except .. as target statement is cleared when the suite exits. From the try statement documentation:

When an exception has been assigned using as target, it is cleared at the end of the except clause. This is as if

except E as N:    foo

was translated to

except E as N:    try:        foo    finally:        del N

This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.

Calling pdb.set_trace() effectively exits the block, so the implicit finally suite above is executed.

Bind the exception to a different name:

try:    foo()except Exception as e:   exception = e   import pdb; pdb.set_trace()

Demo:

>>> try:...     foo()... except Exception as e:...    exception = e...    import pdb; pdb.set_trace()...--Return--> <stdin>(5)<module>()->None(Pdb) e*** NameError: name 'e' is not defined(Pdb) exceptionNameError("name 'foo' is not defined",)