Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to halt python program after pdb.set_trace()

Tags:

python

pdb

ipdb

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!

like image 463
nonagon Avatar asked Sep 30 '16 14:09

nonagon


People also ask

How do I get out of pdb in Python?

from pdb, just type disable N, where N is the breakpoint number you are stuck on. If you don't know the number of your troubling breakpoint, enter tbreak. This will list your breakpoints by number under column "Num" and show whether (yes) or not (no) they are enabled or disabled under the column headed with "Enb."

How do I exit a pdb loop?

Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.

How do I exit pdb and continue?

Post-mortem debugging From here, you are dropped into a (Pdb) prompt. To start execution, you use the continue or c command. If the program executes successfully, you will be taken back to the (Pdb) prompt where you can restart the execution again. At this point, you can use quit / q or Ctrl+D to exit the debugger.

How do I exit PuDB?

Press Ctrl-d at any time to exit the shell and return to the debugger. To configure the shell used by PuDB, open the settings ( Ctrl-p ) and select the shell.


1 Answers

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.

like image 103
nonagon Avatar answered Oct 10 '22 03:10

nonagon