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.
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”.
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.
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.
Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With