python - How do I ignore a line when using the pdb? -
for quick python debugging i'll throw in import pdb;pdb.set_trace()
line drop me debugger. handy. however, if want debug loop, may run many, many, many times, loses effectiveness somewhat. mash on c
ontinue many, many, many times, there way remove/ignore hard-coded breakpoint can let finish?
i set global flag , run conditionally, i'd lose 'standalone-ness' of one-line breakpoint, requiring flag each pdb.set_trace()
.
my other answer sort of quick hack, , not perfect (will disable all calls set_trace
). here's better solution.
we define module wrapping pdb
. in it, set_trace
callable object, maintaining list of disabled callers (identified filename/line-number).
# mypdb.py import inspect import sys try: import ipdb pdb except importerror: import pdb pdb class settracewrapper(object): def __init__(self): self.callers_disabled = set() self.cur_caller = none def __call__(self): self.cur_caller = self.get_caller_id() if self.cur_caller in self.callers_disabled: # disabled caller #print 'set_trace skipped %s' % ( self.cur_caller, ) return #print 'set_trace breaking %s' % ( self.cur_caller, ) try: pdb.set_trace(sys._getframe().f_back) except typeerror: pdb.set_trace() def disable_current(self): #print 'set_trace disabling %s' % ( self.cur_caller, ) self.callers_disabled.add(self.cur_caller) def get_caller_id(self, levels_up = 1): f = inspect.stack()[levels_up + 1] return ( f[1], f[2] ) # filename , line number set_trace = settracewrapper()
in code, make sure use wrapper:
import mypdb pdb; pdb.set_trace()
when want disable current set_trace
-calling-line, do:
pdb.set_trace.disable_current()
notes:
i prefer
ipdb
onpdb
when using
pdb
, since function callingpdb.set_trace
in wrapper, current frame when breaking in it.up
command gets frame want. not happen if usingipdb
(the implementation in wrapper makes sure break in right place).when using
ipdb
, find report caller-frame line-numbers inconsistently. means first timepdb.set_trace.disable_current()
, might not hold. again if breaks next time -- second time holds.in general, having own
pdb
wrapper useful other things well. have ownset_trace
wrapper avoids breaking ifnot sys.stdout.isatty
(you never want break if process not connected terminal, nor when redirect stdout file/pipe). say, having ownpdb
wrapper , callingset_trace
instead ofpdb
's practice.
Comments
Post a Comment