pdb/ipdb for python break on editable condition

shelper picture shelper · Jun 24, 2013 · Viewed 7.6k times · Source

Say I have code the following code:

for i in range(100):
    print i

In general I can add one line to the code as:

for i in range(100):
    import ipdb;ipdb.set_trace()
    print i

However, now I want to debug it at condition of i == 10, and I don't want to bother by typing c for 10 times in ipdb, how should I do?

In the documentation I found condition bpnumber [condition], but how could I know the bpnumber if there is no list of bpnumber index. The documentation also says b(reak) ([file:]lineno | function) [, condition]. For example, assume the line number of print i is xx. I entered the following in ipdb shell: b xx, i == 10 but nothing as expected happened.

Answer

shelper picture shelper · Jun 24, 2013

I did some exploration myself, here is my new understanding of pdb.

When you input import ipdb;ipdb.set_trace() you actually add an entry point of ipdb to the line, not really a breakpoint.

After you enter ipdb, you can then set up breakpoints.

So, to realize what I want for conditional debugging, I should do this:

import ipdb;ipdb.set_trace()
for i in range(100):
    print i

then after I enter ipdb, I can input b xx, i == 10, and then c or r to run the code. The code will stop when the condition is met.

When I input l, the bpnumber is shown for the line as :

          xx-1                  for i in range(100): 
bpnumber> xx                        print i
          xx+1                      ...

I have to say, the documentation and all other explanations are very confusing, I hope my answer here clarifies the difference between the "debug entry point" and "debug breakpoint"