Burdui
Burdui

Reputation: 1302

pdb postmortem debug: jump to raise of exception after exiting in exception handler

I'm just curious if there exists a command for the ipdb-debugger to jump back to the source of a raised exception.

Consider the following little programm.

import sys
import random

def somefun():
    someval = random.random()
    raise Exception()

def run():
    try:
        somefun()
    except Exception:
        sys.exit(10)

When running the run function from the commandline (using ipython-console) it exits with 10 as it should. Is there a way to start the post mortem debugger (pdb.pm()) and get the value of someval by jumping back?

Upvotes: 3

Views: 2041

Answers (1)

J_H
J_H

Reputation: 20425

except Exception:
    sys.exit(10)

Is there a way to ... get the value of someval by jumping back?

No. It is gone. Your error handler swallowed the exception and did something that python considers "normal", though the parent process will interpret the non-zero exit status as an error.

Raise a fatal python error when you're in a debugging context. Here is one way:

except Exception:
    if debug:
        raise
    else:
        sys.exit(10)

Then invoking as $ python -m pdb some_script.py will let you examine backtrace and value with:

(Pdb) bt

and

(Pdb) p someval

Upvotes: 5

Related Questions