3

I find that in large multi-tier pieces of software it is typically easier to debug python code by putting the following within the code.

import pdb; pdb.set_trace() this sets a breakpoint at the LOC where I put the statement, and I can continue within the code using pdb in an interactive way and inspect the code execution etc.

I would like to know if its possible to add more than one breakpoints for such python debugging such that I can do c in the interactive python debugger and hit the next breakpoint?

i.e. for instance

<python code>
import pdb; pdb.set_trace();
... interactive debugging....
...press c here....
<more python code>
...
....
<breakpoint>// How can we insert such a breakpoint? 

2 Answers 2

5

I once did this for ipdb by heavily modifying it here. However, I recently reinvented the wheel, in a way I think is simpler will work with any Bdb debugger. Tested on pdb and and ipdb

#!/usr/bin/env python3

import pdb
import sys
import ipdb.__main__

def foo(x):
  return x+1

class RunningTrace():
  def set_running_trace(self):
    frame = sys._getframe().f_back
    self.botframe = None
    self.setup(frame, None)
    while frame:
      frame.f_trace = self.trace_dispatch
      self.botframe = frame
      frame = frame.f_back
    self.set_continue()
    self.quitting = False
    sys.settrace(self.trace_dispatch)

class ProgrammaticPdb(pdb.Pdb, RunningTrace):
  pass

class ProgrammaticIpdb(ipdb.__main__.debugger_cls, RunningTrace):
  pass

p = ProgrammaticPdb()
# p = ProgrammaticIpdb(ipdb.__main__.def_colors)

p.onecmd('b bar.py:38') # Works before set_running_trace
p.set_running_trace()
p.onecmd('b foo')       # only works after calling set_running_trace
p.onecmd('l')

x=-1
x=2
x=0
x=foo(x)

print(x)

You can set breakpoints by file/line number before calling set_running_trace, however you can only set breakpoints that depend on scope after calling set_running_trace, such as the function name foo in this example.

Sign up to request clarification or add additional context in comments.

Comments

0

Following the @andy way, in more recent ipdb versions you can use:

debugger_cls = ipdb.__main__._get_debugger_cls()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.