PDB, Exceptions, Tracebacks
sys.exc_info()
- the exception info that's been currently handled. Note that the exception could contain inner exceptions via __context__
, or __cause__
. And this could be nested for multiple levels.
pdb.post_mortem(traceback)
- once found the exception you want to post-mortem debug, use:
1 | pdb.post_mortem(exception.__traceback__) |
About exception chaining (both implicit and explicit), see PEP-3134.
Explicit chaining is via:
1 | try: |
In above case, the SomeErr
instance contains __cause__
which is e
.
Implicit chaining:
1 | try: |
In implicit chaining, interpreter automatically sets __context__
on the new exception instance SomeErr
. TBH, I feel the differentiation of implicit and explicit chaining via __cause__
and __context__
is unnecessary. And it causes extra complexity to handle them.
PDB's handling of inner exceptions
...there is none. PDB's post mortem pdb.pm()
doesn't debug the inner-most exception of sys.last_value
. In fact, sys.last_value
isn't always present. So pm()
is not a reliable way of post-mortem debugging.
To debug the inner-most exception:
1 | import sys, pdb |
I created a .pdbrc
alias for this:
1 | _LOCAL = dict() |