2

I have recently come across a VERY cool Python module called pdb. For those that are not familiar with it, it is super easy to use and gives you access to pretty much anything within scope at the time. All you have to do to use it is import pdb and put this line in your program where you want to set the breakpoint:

pdb.set_trace()

It works very much like gdb, and I wouldnt be surprised if it was built on top to some extent. Anyway, what I would like to know:

Say I have stopped at my first breakpoint, evaluated some things, and now I want to finish my program. How can I tell the debugger to finish the program, WITHOUT stopping at any more breakpoints? There are some commands, like continue, step, and next, but none of these seem to run the rest of the program uninterrupted. Anyone have some experience with this or am I asking for something that doesnt exist? Thanks!

Houdini
  • 3,442
  • 9
  • 31
  • 47
  • 1
    You know what's cooler than `pdb`? [`ipdb`](https://pypi.python.org/pypi/ipdb) - it gives you a lot of `IPython`'s niceness (tab completion, syntax highlighting, better tracebacks, better introspection...) that is lacking in `pdb`, and has an identical interface. – ali_m Jun 14 '13 at 21:08
  • Thanks a lot ali I like iPython also so I am definitely going to check that out! – Houdini Jun 14 '13 at 23:30

2 Answers2

3

I would just override pdb.set_trace function, delete all breakpoints and continue

pdb.set_trace = lambda : 0

The good thing is that you can do monkey patching in the debugger.

vikasdhi@redpanda:~$ cat ~/tmp/test.py
for i in range(1000):
    import pdb
    pdb.set_trace()
vikasdhi@redpanda:~$ python ~/tmp/test.py
> /home/vikasdhi/tmp/test.py(1)<module>()
-> for i in range(1000):

it stopped for the first time

(Pdb) c
> /home/vikasdhi/tmp/test.py(1)<module>()
-> for i in range(1000):
(Pdb) c
> /home/vikasdhi/tmp/test.py(1)<module>()
-> for i in range(1000):

when i want to skip everything i just replace the function

(Pdb) pdb.set_trace = lambda : 0
(Pdb) c
vikasdhi@redpanda:~$ 
Vikas
  • 2,220
  • 1
  • 15
  • 12
  • Hmm, I know how to use `lambda` functions, but what exactly is that doing here? – Houdini Jun 14 '13 at 20:52
  • pdb.set_trace function is not the function it was anymore. I have replaced it with a dummy function that does nothing but returns 0 all the time. So pdb.set_trace won't stop your program anymore, it will just keep returning 0 (in this case) – Vikas Jun 14 '13 at 20:54
  • 1
    May be this will help: http://stackoverflow.com/questions/5626193/what-is-monkey-patching . – Vikas Jun 14 '13 at 21:01
  • That is helpful but I still want it to enter `pdb` the first time, just not every other time after that. i.e. If I have more breakpoints, I want it to skip over them. – Houdini Jun 14 '13 at 21:02
  • 1
    One caveat: when you monkey-patch like this, `pdb.set_trace()` won't work again until you `reload(pdb)` – ali_m Jun 14 '13 at 21:10
  • Ah, I see what you mean after you updated your post. Very neat way of working around that, I will have to look more into monkey-patching. – Houdini Jun 14 '13 at 23:32
1

the command is cl or clear.

cl(ear) [filename:lineno | bpnumber [bpnumber ...]]

With a filename:lineno argument, clear all the breakpoints at this line. With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation).

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696