Sub topic 6
Moving through the call stack: where, up, down
This chapter assumes you can run a Python script from a terminal, read a traceback, and use breakpoint() with the basic n, s, c and p commands from earlier chapters.
When pdb stops, it stops inside one function. The bug is often in a caller, because the caller passed a bad value. The stack commands let you look at each caller without leaving the debugger.
The call stack and the current frame
The call stack is the chain of function calls that led to the current line. Each call has a frame, which holds that call's local variables and the line it is executing.
pdb always has a current frame. Commands such as p, pp, args and list work on the current frame. At first, the current frame is the one where execution stopped.
Seeing the stack with where
The w command (long form where) prints the stack. The oldest call is at the top, and the newest call is at the bottom. An arrow > marks the current frame.
def parse(text):
number = int(text)
breakpoint()
return number * 2
def load(rows):
results = []
for row in rows:
results.append(parse(row))
return results
def main():
rows = ["4", "7", "x9"]
print(load(rows))
main()The first two rows are fine. On the third row, int("x9") raises ValueError before reaching breakpoint(). To stop at the right place, run the script and use the breakpoint on the first row, then ask for the stack.
(Pdb) w
script.py(18)<module>()
-> main()
script.py(15)main()
-> print(load(rows))
script.py(10)load()
-> results.append(parse(row))
> script.py(4)parse()
-> return number * 2Read it from the bottom up. parse is the current function, load called it, main called load, and the module called main.
Moving with up and down
The u command (long form up) moves the current frame one step toward the older callers. The d command (long form down) moves back toward the newest call.
(Pdb) u
> script.py(10)load()
-> results.append(parse(row))
(Pdb) p row
'4'
(Pdb) p results
[]
(Pdb) d
> script.py(4)parse()
-> return number * 2Moving up does not run any code. Execution is still paused in parse. You are only choosing which frame the inspection commands read. You can also pass a count, as in u 2, to move several frames at once.
A typical use
Suppose parse receives a value it cannot handle. Inside parse you see only text. Use u to go to load, then check row and rows to find which input caused it. You can also run an expression in the caller's frame, for example p len(rows).
If a name does not exist in the frame you are viewing, pdb reports a NameError. That error usually means you are in a different frame than you thought. Run w and check where the arrow is.
Stepping resets the view
When you run n, s, c or r, pdb returns to the newest frame as execution continues. A frame you selected with u is forgotten. Move up again if you still need it.
Summary of commands
| Command | Long form | Effect |
|---|---|---|
| `w` | `where` | Print the stack, with `>` on the current frame |
| `u [n]` | `up` | Move the current frame toward older callers |
| `d [n]` | `down` | Move the current frame toward the newest call |
Exercise
You stop in parse() and see text = "x9", but you need to know which index of rows it came from. The index is not a local variable in parse(). Which commands do you use, and why does this work?
Show answer
Run w to confirm that load() is one frame above parse(), then u to make load() the current frame. Now p row and p rows show the caller's variables, and p rows.index(row) gives the position. This works because inspection commands read the current frame, and u changes that frame without running any code.