Sub topic 7
Setting, listing, disabling, and clearing breakpoints
This chapter assumes you can start a pdb session with breakpoint() or python -m pdb. It also assumes you know the n, s, and c commands from earlier chapters. Here you will manage breakpoints, the marks that tell pdb where to stop, so you don't have to step through code line by line.
Setting a breakpoint
The b command (short for break) takes a location. It accepts three forms:
b 12sets a breakpoint at line 12 of the current file.b mymodule.py:30sets one at line 30 of another file.b process_itemsets one on the first executable line of a function.
pdb prints a number for each breakpoint, starting at 1. You use that number in every later command. A line must contain executable code. If you point at a blank line or a comment, pdb refuses and prints an error.
Use tbreak for a temporary breakpoint. It takes the same arguments as b, but pdb deletes it the first time it is hit. This suits the case where you want to run to a spot once and then carry on.
A worked example
Save this as inventory.py:
def process_item(item):
price = item["price"]
return price * item["qty"]
def main():
items = [{"price": 2, "qty": 3}, {"price": 5, "qty": 1}, {"price": 4, "qty": 2}]
total = 0
for item in items:
total += process_item(item)
print(total)
main()Start it with python -m pdb inventory.py, then set two breakpoints at the prompt:
(Pdb) b 10
(Pdb) tbreak process_item
(Pdb) bThe bare b lists every breakpoint in a table:
Num Type Disp Enb Where
1 breakpoint keep yes at inventory.py:10
2 breakpoint del yes at inventory.py:2The Disp column shows what happens after a hit: keep stays, del is removed (the tbreak). The Enb column shows whether the breakpoint is active. pdb prints absolute paths, and a hit count appears under any breakpoint that has been hit.
Disabling, enabling, and clearing
Disabling keeps a breakpoint but stops it from firing. Clearing removes it.
| Command | Effect |
|---|---|
| `disable 1` | Breakpoint 1 stays in the list but is skipped |
| `enable 1` | Breakpoint 1 stops again |
| `cl 2` | Deletes breakpoint 2 (`clear 2` is the same) |
| `cl` | Deletes all breakpoints after a confirmation prompt |
Prefer disable when you may want the breakpoint again. A disabled breakpoint also keeps its condition and attached commands.
How the pieces fit
Common errors
- "is not a valid breakpoint number": you gave
disableorcla number that was already cleared. pdb never reuses numbers within a session, so runbto see the current ones. - A breakpoint on a line that never runs: lines inside an
ifbranch that isn't taken never trigger. Check the condition before assuming pdb is broken. - Typing a bare
bto set a breakpoint: this lists them. Always give a location. - Relative filenames:
b mymodule.py:30needs a file pdb can find onsys.path. If it can't, use a full path.
Breakpoints live only for the current session. When you want the same ones every time, put the b commands in a .pdbrc file, which a later chapter covers.
Exercise
In a session on inventory.py you ran b 10, then tbreak process_item, then c, and execution stopped inside process_item. You now type b. Which breakpoint is gone from the list, and what command would stop breakpoint 1 from firing without deleting it?
Show answer
Breakpoint 2 (the tbreak on process_item) is gone, because a temporary breakpoint is deleted on its first hit. disable 1 keeps breakpoint 1 in the list but stops it from firing, and enable 1 turns it back on.