dropping into pdb on an exception

Jon Perez jbperez808 at yahoo.com
Wed Jun 9 01:04:07 EDT 2004


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




More information about the Python-list mailing list