Sub topic 9
Automating breakpoints with commands
This chapter assumes you can start a pdb session, set a breakpoint with b, and use p, n, and c. Here you will attach commands to a breakpoint so pdb runs them for you each time it is hit.
Why attach commands
Sometimes you need to know what a variable holds on every pass through a loop. Stopping and typing p x and c each time is slow. A breakpoint can carry a list of pdb commands that run automatically when it is hit. This works like adding a print() call to your code, except that you add it from inside the debugger and you never edit the source file.
The commands command
The commands N command starts a block of commands for breakpoint number N. The prompt changes from (Pdb) to (com). Each line you type is stored instead of run. The word end closes the block.
Here is a script to try it on:
def scale(items, factor):
results = []
for x in items:
y = x * factor
results.append(y)
return results
print(scale([1, 2, 3], 10))Run it with python -m pdb script.py, then enter this:
(Pdb) b 4
(Pdb) commands 1
(com) silent
(com) p x
(com) continue
(com) end
(Pdb) cBreakpoint 1 is on line 4. Each time the loop reaches it, pdb prints x and resumes. You see 1, 2, and 3 printed, then the program output.
What each line does
silentstops pdb from printing the usual breakpoint banner and source line. Without it, every hit prints extra text.p xprints a value. You can list several commands, such asp x, factor.continueresumes the program. It must be the last command in the block. Any command that resumes execution (c,n,s) ends the list, and lines after it are ignored.endcloses the block.
If you leave out continue, pdb stops at the breakpoint after running your commands. That is useful when you want an automatic print and then a normal prompt.
Combining with conditions
Commands run only when the breakpoint actually triggers. If the breakpoint has a condition, the commands run only when the condition is true. This lets you log only the interesting cases:
(Pdb) b 4, x > 1
(Pdb) commands 1
(com) silent
(com) p f"x={x} y={y}"
(com) continue
(com) endOnly the passes where x is greater than 1 print anything. Note that line 4 runs before y is assigned on that pass, so y still holds its value from the previous pass, or raises a NameError on the first. Choose the line after the assignment (line 5) when you want to see the new y.
Common mistakes
- Forgetting
end. The(com)prompt stays open and your next command is stored instead of run. - Putting commands after
continue. They never run. - Expecting the commands to persist after you quit. They last only for the session. To reuse them, put the setup in a
.pdbrcfile, which a later chapter covers. - Using
commandson a breakpoint number that does not exist. Runbwith no arguments first to see the numbers.
Checking what you set
Run b with no arguments to list breakpoints. The listing shows hit counts, conditions, and the attached commands. Use it to confirm the block was stored as you intended.
Exercise
You set breakpoint 2 on a line inside a loop and want it to print total on every pass without stopping. Write the lines you type after b to do this, and say what happens if you leave out silent.
Show answer
Type commands 2, then silent, p total, continue, and end. Without silent, pdb still prints the breakpoint banner and the current source line on every hit before it runs p total, so the output is cluttered, but the program still resumes because of continue.