handling unexpected exceptions in pdb

R. Bernstein rocky at panix.com
Thu Jul 10 14:52:42 EDT 2008

Simon Bierbaum <bierbaum at vierfalt.com> writes:

> Hi all,
> I'm in an interactive session in pdb, debugging my code using
> pdb.runcall. Somewhere, an exception is raised and lands uncaught on
> stdout. Is there any way of picking up this exception and at least
> read the full message, or even accessing its stack trace to determine
> where exactly within the one line I just executed it was raised? This
> is where I'm stuck:
>> /usr/local/apache2/bin/Model/Database.py(287)disconnect()
> (Pdb) n
> FlushError: FlushErr...perly.",)
>> /usr/local/apache2/bin/Model/Database.py(287)disconnect()
> (Pdb) import sys
> (Pdb) sys.last_traceback
> *** AttributeError: 'module' object has no attribute 'last_traceback'
> Thanks, Simon

I think basically you want runcall to be wrapped in a try block. So in pdb.py
instead of:

def runcall(*args, **kwds):
    return Pdb().runcall(*args, **kwds)

Change with:

def runcall(*args, **kwds):
      return p.runcall(*args, **kwds)
      print "Uncaught exception. Entering post mortem debugging"
      t = sys.exc_info()[2]
      print "Post mortem debugger finished."
      return None

Code like this appears near the bottom of the pdb.py file. If that
works, you may want to file a bug Python report to change pdb. Also
note that one may want to do the same thing on run() and runeval()

But also if this does what you want, please file a feature request to pydb:

and I'll probably make it happen in the next release.

This is the first time I've heard of anyone using runcall.

More information about the Python-list mailing list