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 continue 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:

  1. i prefer ipdb on pdb

  2. when using pdb, since function calling pdb.set_trace in wrapper, current frame when breaking in it. up command gets frame want. not happen if using ipdb (the implementation in wrapper makes sure break in right place).

  3. when using ipdb, find report caller-frame line-numbers inconsistently. means first time pdb.set_trace.disable_current(), might not hold. again if breaks next time -- second time holds.

  4. in general, having own pdb wrapper useful other things well. have own set_trace wrapper avoids breaking if not sys.stdout.isatty (you never want break if process not connected terminal, nor when redirect stdout file/pipe). say, having own pdb wrapper , calling set_trace instead of pdb's practice.


Comments

Popular posts from this blog

asp.net mvc 3 - Using mvc3, I need to add a username/password to the sql connection string at runtime -

kineticjs - draw multiple lines and delete individual line -

thumbnails - jQuery image rotate on hover -