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.
There's a quick dirty way like this:
It works and don't have to busy your mind with any other commands :)
I think you were looking for a more direct solution that did not involve adding lines to the code, and just involved debugger commands.
Your original example of
doesn't work, because you are setting a breakpoint at the place in your code you inserted the ipdb.set_trace() command. By adding the statement 'b xx, i == 10' in the debugger, you actually have 2 break points (1 conditional and 1 unconditional) defined at the same location (assuming xx is the line were the set_trace() command is).
Alternatively, once you have defined breakpoints in your code using the 'b' command, which apparently works for you. You can add a condition to the breakpoint by
for example
Note: the bpnumber is the number assigned to the breakpoint, not the line in your code. To see a list of breakpoints, just type 'b' with no arguments.
Also, if you want to enter debug mode without using ipdb.set_trace(), you simply run your code with the pdb/ipbd module enabled
alright, 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 setup breakpointsso, to realize what i want for conditional debugging, i should add as follows
then after i enter ipdb, i can input
b xx, i == 10
, and thenc
orr
to run the code, then the code will stop when the condition is met. when I inputl
, the bpnumber is shown for the line as :i have to say, the document and all other explanations are so confusing, i wish my answer here clarifies the difference between the "debug entry point" and "debug breakpoint"