python: in pdb is it possible to enable a breakpoint only after n hit counts?

Question:

In eclipse (and several other IDE’s as well) there is an option to turn on the breakpoint only after a certain number of hits. In Python’s pdb there is a hit count for breakpoints and there is the condition command. How do I connect them?

Asked By: zenpoy

||

Answers:

Conditional Breakpoints can be set in 2 ways –

FIRST: specify the condition when the breakpoint is set using break

python -m pdb pdb_break.py
> .../pdb_break.py(7)<module>()
-> def calc(i, n):
(Pdb) break 9, j>0
Breakpoint 1 at .../pdb_break.py:9

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at .../pdb_break.py:9
        stop only if j>0

(Pdb) continue
i = 0
j = 0
i = 1
> .../pdb_break.py(9)calc()
-> print 'j =', j

(Pdb)

SECOND: condition can also be applied to an existing breakpoint using the condition command. The arguments are the breakpoint ID and the expression.

$ python -m pdb pdb_break.py
> .../pdb_break.py(7)<module>()
-> def calc(i, n):
(Pdb) break 9
Breakpoint 1 at .../pdb_break.py:9

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at .../pdb_break.py:9

(Pdb) condition 1 j>0

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at .../pdb_break.py:9
        stop only if j>0

(Pdb)

source

UPDATE: I wrote a simpler code

import pdb; pdb.set_trace()
for i in range(100):
    print i

debugging on terminal –

$ python 1.py 
> /code/python/1.py(3)<module>()
-> for i in range(100):
(Pdb) l
  1     
  2     import pdb; pdb.set_trace()
  3  -> for i in range(100):
  4         print i
[EOF]
(Pdb) break 4, i==3
Breakpoint 1 at /code/python/1.py:4
(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /code/python/1.py:4
    stop only if i==3
(Pdb) c
0
1
2
> /Users/srikar/code/python/1.py(4)<module>()
-> print i
(Pdb) p i
3
Answered By: Srikar Appalaraju

I found the answer. It’s pretty easy actually, there’s a command called ignore let’s say you want to break at breakpoint in line 9 after 1000 hits:

b 9

Output: Breakpoint 2 at ...

ignore 1 1000

Output: Will ignore next 1000 crossings of breakpoint 1.

 c
Answered By: zenpoy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.