Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdb/ipdb for python break on editable condition

Tags:

python

pdb

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.

like image 861
shelper Avatar asked Jun 24 '13 16:06

shelper


People also ask

How do you set a breakpoint in Python pdb?

It's easy to set a breakpoint in Python code to i.e. inspect the contents of variables at a given line. Add import pdb; pdb. set_trace() at the corresponding line in the Python code and execute it. The execution will stop at the breakpoint.

How do I break out of pdb?

Whenever you want to leave the pdb console, type the command quit or exit . If you would like to explicitly restart a program at any place within the program, you can do so with the command run .

What is the difference between IPDB and pdb?

ipdb is an enhanced version of pdb (built-in). It supports all pdb commands and is simply easier to use like tab for auto-complete. ipdb command cheatsheet.

How do I skip a line in IPDB?

In general, to "step out" of the current function, use return . r(eturn) Continue execution until the current function returns.


3 Answers

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"

like image 92
shelper Avatar answered Oct 17 '22 19:10

shelper


There's a quick dirty way like this:

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

It works and don't have to busy your mind with any other commands :)

like image 36
Paulo Bu Avatar answered Oct 17 '22 20:10

Paulo Bu


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

b xx, i == 10 

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

condition bpnumber boolean-expression

for example

condition 1 i == 10

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

python -m pdb foo.py
like image 27
Jim Parker Avatar answered Oct 17 '22 20:10

Jim Parker