29

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.

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
morkel
  • 434
  • 1
  • 4
  • 7

1 Answers1

55

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.

natka_m
  • 1,297
  • 17
  • 22
  • 2
    Wow, that works! Guess that's me not being good enough at looking at the documentation. Cheers! – morkel Oct 08 '19 at 09:09
  • 2
    Guess it happens to everyone sometimes :p – natka_m Oct 08 '19 at 09:11
  • 1
    The help informatin built in pdb is kind of confusing. [Python document site](https://docs.python.org/3/library/pdb.html?highlight=pdb#pdbcommand-step) says "... (either in a function that is called or **on the next line** in the current function)", while the pdb help information says "either in a function that is called or in the current function". – ebk Dec 26 '20 at 04:57