Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdb step into a function when already in pdb mode

Tags:

python

pdb

When in pdb mode I often want to step into a function. Here is a situation that illustrates what I might do. Given the code:

def f(x):
    print('doing important stuff..')
    result = g(x) + 2
    return result

def g(x):
    print('some cool stuff going on here, as well')
    0 / 0  # oops! a bug
    return x + 5

Now, assume I set a breakpoint between the print('doing important stuff...') and result = g(x) + 2. So now, f(x) looks like this:

def f(x):
    print('doing important stuff..')
    __import__('pdb').set_trace()  # or something similar..
    result = g(x) + 2
    return result

And then I call the function f(x) with x=5, expecting to get a result 12. When called, I end up in an interactive pdb session on the second line in f. Hitting n will give me the error (in this case a ZeroDivisionError). Now, I want to step into the g(x) function interactively to see what the error might be. Is it somehow possible to do that while not "moving" the breakpoint in g(x) and re-running everything? I simply want to enter the function g on the first line while still being in pdb mode.

I've searched for previous SO questions and answers + looked up the documentation and still haven't found anything that addresses this particular situation.

like image 235
morkel Avatar asked Oct 08 '19 08:10

morkel


People also ask

How do you step over in pdb?

The difference between n (next) and s (step) is where pdb stops. Use n (next) to continue execution until the next line and stay within the current function, i.e. not stop in a foreign function if one is called. Think of next as “staying local” or “step over”.

How do you step into a function in Python?

Stepping Into a FunctionSet a breakpoint at the function call line. Step through your code line by line until you reach the function call. While the program is paused at the function call line, click the Step Into button. on the Debug toolbar or select Debug:Step Into.

How do you set a breakpoint in 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 you get out of a loop in pdb?

Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.


1 Answers

You're probably looking for the s command: it s-teps into the next function.

While in debugging mode, you can see all available commands using h (help). See also the docs.

like image 85
natka_m Avatar answered Sep 18 '22 01:09

natka_m