dropping into pdb on an exception

J

Jon Perez

How do you set up pdb such that you will automatically
get dropped into its prompt if an unanticipated exception
occurs in a script you are using?

ASPN Python cookbook gives you the following method
which you can add to your script and hook into sys.excepthook.
But is there a way to do it without adding stuff to your
script? (It's okay if this means having to invoke the script
from within pdb, but #1, I don't know how to get it stay inside
pdb in the case of an /unanticipated/ exception. And #2, I
don't know how to pass [the equivalent of] command-line
arguments to a script invoked from within pdb.)



def info(type, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
pdb.pm()

sys.excepthook = info
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top