How to ignore string when using pdb?

For some quick Python debugging, I sometimes enter a line import pdb;pdb.set_trace()that will put me in the debugger. Very comfortably. However, if I want to debug a cycle that can run many, many, many times, it loses its effectiveness somewhat. I could crush continue many, many, many times, but is there a way to remove / ignore this hard-coded breakpoint so I can finish it?

I can set the global flag and run it conditionally, but then I will lose the "autonomy" of the single-line breakpoint, also requiring one more flag for each pdb.set_trace().

+5
source share
5 answers

- , ( set_trace). .

pdb. set_trace - , ( / ).

# mypdb.py
import inspect
import sys
try:
    import ipdb as PDB
except ImportError:
    import pdb as 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:
            # already disabled for caller
            #print 'set_trace SKIPPED for %s' % ( self.cur_caller, )
            return
        #print 'set_trace BREAKING for %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 and line number

set_trace = SetTraceWrapper()

, :

import mypdb as pdb; pdb.set_trace()

set_trace -calling-line, :

pdb.set_trace.disable_current()

:

  • ipdb pdb

  • pdb, , pdb.set_trace, , . up . , ipdb ( ).

  • ipdb , . , pdb.set_trace.disable_current() . , - .

  • , pdb . set_trace, , not sys.stdout.isatty ( , , stdout /). , pdb set_trace pdb .

+2

hack set_trace (.. ).

noop_pdb :

# noop_pdb.py
def set_trace(*args, **kwargs):
    pass

, pdb.set_trace, set_trace, :

sys.modules['pdb'] = __import__('noop_pdb')

:

import pdb;pdb.set_trace()

pdb, drop-in.

EDIT: , noop_pdb, set_trace noop pdb: pdb.set_trace = lambda: None

+1

condition bpnumber? , . break tbreak, . .

0

'return' pdb pdb.set_trace() .

0

All Articles