Is it possible to automatically break into the debugger when a exception is thrown?

Question:

If ones catches an exception outside of the function it is originally thrown, ones loses access to the local stack. As a result one cannot inspect the values of the variables that might have caused the exception.

Is there a way to automatically start break into the debugger (import pdb; pdb.set_trace()) whenever a exception is thrown to inspect the local stack?

Asked By: Framester

||

Answers:

You don’t want to break on every exception; idiomatic Python code uses exceptions heavily (EAFP) so you’d be continually breaking in unrelated code.

Instead, use pdb post-mortem: import pdb; pdb.pm(). This uses sys.last_traceback to inspect the stack including the locals at the throw point.

Answered By: ecatmur

ipython supports this (http://ipython.org). from inside ipython, do

%pdb on

and from then on, it will automatically drop you inside the debugger whenever you get an exception.

note that you’ll (probably) quickly tire of this in general use… every time you mistype something and get a syntax error, you’ll have to exit the debugger. but it’s sometimes useful.

Answered By: Corley Brigman

I found what I was looking for in an answer to What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?

Wrap it with that:

def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except exceptions:
                pdb.post_mortem(sys.exc_info()[2])
        return wrapper
    return decorator

Example:

@debug_on(TypeError)
def buggy_function()
    ....
    raise TypeError
Answered By: Framester

If you want to wrap only some inner part of a function, or would need to decorate multiple functions otherwise, a context manager could be used as an alternative to the accepted answer. I am now using this simple version that catches all exceptions. I recommend using pudb, but this will work pdb or any other debugger too

from contextlib import contextmanager

@contextmanager
def postmortem_pudb():
    try:
        yield
    except Exception as exc:
        import pudb
        pudb.post_mortem()

Use like this

with postmortem_pudb():
    function_that_might_throw_some()
    ...
    another_function_that_might_throw_some()
    ...
    yet_another_function_that_might_throw_some()

Answered By: smido

I know this is old and an answer has already been accepted, but I found this useful (for IPython):
start IPython with the –pdb option

ipython --pdb <whatever command>
Answered By: geogeo

For python 3 (today at 3.8), this can be done with

python3 -m pdb myscript.py

From the docs:

When invoked as a script, pdb will automatically enter post-mortem
debugging if the program being debugged exits abnormally. After
post-mortem debugging (or after normal exit of the program), pdb will
restart the program. Automatic restarting preserves pdb’s state (such
as breakpoints) and in most cases is more useful than quitting the
debugger upon program’s exit.

Note that at launch, python will directly enter pdb mode, and you need to type c then enter to start running the script

Answered By: Shadi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.