nonagon
nonagon

Reputation: 3463

how to halt python program after pdb.set_trace()

When debugging scripts in Python (2.7, running on Linux) I occasionally inject pdb.set_trace() (note that I'm actually using ipdb), e.g.:

import ipdb as pdb
try:
    do_something()
    # I'd like to look at some local variables before running do_something_dangerous()
    pdb.set_trace()
except:
    pass
do_something_dangerous()

I typically run my script from the shell, e.g.

python my_script.py

Sometimes during my debugging session I realize that I don't want to run do_something_dangerous(). What's the easiest way to halt program execution so that do_something_dangerous() is not run and I can quit back to the shell?

As I understand it pressing ctrl-d (or issuing the debugger's quit command) will simply exit ipdb and the program will continue running (in my example above). Pressing ctrl-c seems to raise a KeyboardInterrupt but I've never understood the context in which it was raised.

I'm hoping for something like ctrl-q to simply take down the entire process, but I haven't been able to find anything.

I understand that my example is highly contrived, but my question is about how to abort execution from pdb when the code being debugged is set up to catch exceptions. It's not about how to restructure the above code so it works!

Upvotes: 5

Views: 1428

Answers (2)

nonagon
nonagon

Reputation: 3463

I found that ctrl-z to suspend the python/ipdb process, followed by 'kill %1' to terminate the process works well and is reasonably quick for me to type (with a bash alias k='kill %1'). I'm not sure if there's anything cleaner/simpler though.

Upvotes: 3

tzaman
tzaman

Reputation: 47770

From the module docs:

q(uit) Quit from the debugger. The program being executed is aborted.

Specifically, this will cause the next debugger function that gets called to raise a BdbQuit exception.

Upvotes: 2

Related Questions