Search code examples
pythondebuggingpdb

How do I step into a particular callable in PDB, when there are several invoked on the same line?


Often when I step through code in PDB, I get to lines like this:

foo(bar(), qux())

In this case, I'm interested in stepping into foo(), but not into bar() or qux().

How do you do this in PDB?

If I just issue a step command at the prompt, PDB will trace into bar(), then qux(), and only then into foo() - which is a huge inconvenience when bar() and qux() are long functions.


Solution

  • I guess this is the answer and not just a comment.

    When you are about to run the line calling:

    foo(bar(), qux())
    

    Add temporary breakpoint on foo() using:

    tbreak foo
    

    And then just:

    c
    

    or continue. This will run bar and qux and stop once reaching foo code block.

    You could also just use a regular b(reak).

    Alternatively, you can s(tep) into bar and qux but use:

    r
    

    or return. To just run them up to returning from them. With "only" two functions as parameters, that is likely still relatively bearable inconvenience.

    You can also expand on the breakpoint idea by making it conditional, e.g. if you know you only want to debug foo after x has has been assigned a value of one:

    b foo, x == 1
    

    This way you could run (or n(ext)) through your code and have the breakpoint only trigger when the condition is met.