Sub topic 8
Conditional breakpoints and ignore counts
A breakpoint inside a loop stops on every pass. If the bug appears on iteration 700, you don't want to type c 699 times. pdb has two tools for this: a condition, which stops only when an expression is true, and an ignore count, which skips a fixed number of crossings.
This chapter assumes you can start a session with breakpoint() or python -m pdb, and that you know b, c, and p.
A program that fails late
Save this as orders.py:
def scale(items):
results = []
for index, item in enumerate(items):
value = item * 2
results.append(100 // (value - 14))
return results
print(scale(range(10)))The call raises ZeroDivisionError when item is 7, because value - 14 is then 0. A plain b 5 would stop eight times before the failing pass.
Conditions
Put the condition after the line number, separated by a comma. It can be any Python expression that uses variables visible at that line.
$ python -m pdb orders.py
(Pdb) b 5, value == 14
Breakpoint 1 at orders.py:5
(Pdb) c
> orders.py(5)scale()
-> results.append(100 // (value - 14))
(Pdb) p index
7pdb evaluates the condition each time execution reaches line 5. It stops only when the result is true. You can also add or change a condition on an existing breakpoint with condition:
(Pdb) condition 1 index > 5
(Pdb) condition 1
Breakpoint 1 is now unconditional.condition 1 with no expression removes the condition. The first argument is the breakpoint number, not a line number.
Ignore counts
An ignore count tells pdb to skip the next N crossings and stop on crossing N+1. It suits cases where you know how many passes to skip but can't easily write an expression.
(Pdb) b 5
Breakpoint 1 at orders.py:5
(Pdb) ignore 1 7
Will ignore next 7 crossings of breakpoint 1.
(Pdb) c
> orders.py(5)scale()
-> results.append(100 // (value - 14))
(Pdb) p index
7The b command with no arguments lists each breakpoint with its condition, its remaining ignore count, and how many times it was hit. Use it to check your setup:
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at orders.py:5
stop only if value == 14
breakpoint already hit 8 timesHow the checks combine
When both are set, the condition is checked first. The ignore count is used up only by crossings where the condition is true.
Common errors
- A typo in the condition. pdb compiles the expression when you set it. If it has a syntax error, you get an error message and no condition is set. A name that doesn't exist is different, because pdb can't know that until the line runs.
- A condition that raises an exception. pdb treats this as true and stops. If you stop where you didn't expect to, run
pon the condition's variables. ANameErrororTypeErrorin the condition is often the cause. - Side effects in conditions. An expression such as
items.pop() == 3changes your program every time it runs. Keep conditions free of side effects. - Confusing the numbers.
b 5, ...takes a line number.condition 1 ...andignore 1 ...take a breakpoint number.
Exercise
You have breakpoint 3 on a line inside a loop and want it to stop only when name == "retry", and only the second time that happens. Which two commands do you type?
Show answer
condition 3 name == "retry" followed by ignore 3 1. The ignore count is spent only on crossings where the condition is true, so pdb skips the first matching pass and stops on the second.