Reputation: 7317
In IPython, with %pdb
enabled, I can hit Ctrl-C and be dropped to a Python debugger console at the exact point I stopped the program.
However, at this point the program is entirely stopped, and I can't resume he execution even if I run continue
in the debugger.
Is IPython able to do that, then let me resume the execution of the program when I'm done?
Note: I know about pdb.set_trace()
, but this is not what I'm looking for. I'm looking for a way to quickly, temporarily stop IPython for quick harmless checks instead of having to manually add set_trace
in my code, if possible.
Upvotes: 3
Views: 987
Reputation: 2364
There are a lot of corner cases with this type of thing. However adding a signal handler to your usercustomize.py gets you quite far:
cat >> $(python -m site --user-site)/usercustomize.py <<EOF
import signal, pdb
signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
EOF
Now when you launch python, you will get that signal handler installed.
For example, you can now interrupt the SimpleHTTPServer:
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
^C--Return--
> [...].local/lib/python2.7/site-packages/usercustomize.py(3)<lambda>()>None
-> signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
(Pdb) locals()
{'__return__': None, 'args': (2, <frame object at 0x10245b238>), 'os': <module 'os' from '[...]/lib/python2.7/os.pyc'>}
Upvotes: 1
Reputation: 27843
I can hit Ctrl-C and be dropped to a Python debugger console at the exact
point I stopped the program.
Ctrl-C is interrupting your program, which raise a KeyboardInterrupt
, when you resume with continue
you continue ...and interrupt your program as asked by the Ctrl-C you sent. Ctrl-C is not to "pause" the program in debug mode at one certain point.
What you are looking for is to set a breakpoint a one particular line, like %run -d
allow you to do. See %run?
.
Upvotes: 0