Is there a way to examine the last exception when in pdb/before entering pdb? (Using python 2.7.5).
Immediately (yes, I enter no other commands at all) afte
You can use sys.last_value:
>>> no_such_var
Traceback (most recent call last):
File "", line 1, in
NameError: name 'no_such_var' is not defined
>>> import sys
>>> sys.last_value
NameError("name 'no_such_var' is not defined",)
>>> sys.last_value.args
("name 'no_such_var' is not defined",)
>>> no_such_var
Traceback (most recent call last):
File "", line 1, in
NameError: name 'no_such_var' is not defined
>>> import pdb, sys
>>> pdb.set_trace()
--Return--
> (1)()->None
(Pdb) sys.last_value
NameError("name 'no_such_var' is not defined",)
NOTE: This solution is not perfect. The value is set when an exception is not handled and the interpreter prints an error message and a stack traceback. For example, if the exception is caught using try .. except .., sys.last_value is not set.