12

I suspect that I have issue in one of my loops, so I setup a break points with pdb.set_trace()

import pdb
for i in range(100):
    print("a")
    pdb.set_trace()
    print("b")

after check variable in this loop for a few times, I decide continue this programming without further breaks. So I try to get the break number with b command, no breaks listed. I guess this line of code don't setup a break point. but How Do I get ride of this "break points" without stopping the program and change the code?

scott huang
  • 2,478
  • 4
  • 21
  • 36
  • why does `r(eturn)` not work for you? – abcd Sep 12 '17 at 03:01
  • r don't work , function of r command if return from a function call. If this function get called again, the program still get halt. – scott huang Sep 12 '17 at 07:44
  • I think this is maybe a duplicate of https://stackoverflow.com/questions/17820618/how-to-exit-pdb-and-allow-program-to-continue/17820890 although that question is vague because it doesn't state whether a breakpoint or `set_trace` was used. – Adam Spiers Mar 15 '18 at 20:48

5 Answers5

38

to my knowledge, you could not bypass set_trace, but you could neutralize it, once debugger stopped, type:

pdb.set_trace = lambda: 1

then continue, it wont break again.

georgexsh
  • 15,984
  • 2
  • 37
  • 62
  • 1
    this will work . but this will disable all following pdb.set_trace(). I can also do this "if my_flag: pdb.set_trace()", but this seems a little urgly. – scott huang Sep 12 '17 at 08:08
  • @scotthuang debug condition variable is okay to me, and, you could create pdb alias for quick disable/enable `set_trace`. – georgexsh Sep 12 '17 at 16:14
  • jump don't work. every time python interpreter hit the set_trace, it will halt. I think I ask too much out of pdb. I will just add set_tract at 1st line of my program. then set breakpoint by command. if I don't need it, I will clear the breakpoint. – scott huang Sep 14 '17 at 06:40
  • @scotthuang you could jump out of for loop, but this changed code logic. – georgexsh Sep 14 '17 at 06:42
  • yes. I could, your method is suitable for some context, I used that method with other language's debugger. at this point, I think I will try to workaround this. – scott huang Sep 14 '17 at 06:45
  • Brilliant, been looking for something like this for some time. – Lars Larsson Jan 11 '19 at 15:03
10

Setting a breakpoint (requires Python 3.7):

breakpoint()

Disabling breakpoints set with the breakpoint() function:

import os
os.environ["PYTHONBREAKPOINT"] = "0"

Long story:

In the 3.7 version of Python, the breakpoint() built-in function for setting breakpoints was introduced. By default, it will call pdb.set_trace(). Also, since the 3.7 version of Python the PYTHONBREAKPOINT environment variable is available. It is considered when the breakpoint() function is used.
So, in order to disable these breakpoints (set with the breakpoint() function), one can just set the PYTHONBREAKPOINT environment variable like this:

import os
os.environ["PYTHONBREAKPOINT"] = "0"

It may be useful to mention here sys.breakpointhook() which was also added in the 3.7 version of Python and allows to customize breakpoints behavior.

Pavel Shishpor
  • 742
  • 6
  • 14
7

Unfortunately pdb is missing a bunch of functionality (even basic stuff like display lists), and you've found another example of that here. The good news is that pdb++ is a great drop-in replacement for pdb, and one of the things it solves is exactly the problem of disabling set_trace. So you can simply do:

pip install pdbpp

and then at the (Pdb++) prompt, type

pdb.disable()

Easy! And you will get lots of other useful goodies on top of that.

Adam Spiers
  • 17,397
  • 5
  • 46
  • 65
1

It is possible to start a Python script without PDB control but then hit a stray set_trace() left there. To prevent breaking into debugger every time set_trace() is encountered, a similar trick as above (changing the symbol's reference to point to a harmless function) can be applied.

However, the namespace of the debuggee has to be modified, not one of the debugger itself. Simply overwriting pdb.set_trace = lambda:1 or set_trace = lambda:1 did not work for me.

The following trick worked from the pdb prompt:

pdb> globals()['set_trace'] = lambda:1

This line first calls globals() to get access to a dict of the program under debugging, and then modifies the reference of set_trace there.

Grigory Rechistov
  • 2,104
  • 16
  • 25
0

One way around this is to not write the breakpoints in the script itself, but rather set breakpoints when you start python with python -m pdb my_script.py

You then get into a prompt first, before execution of the script starts, and you can write for example

(Pdb) b 321

to set a breakpoint on line 321 (you can also specify file and specify conditions b 321, i == 50)

Then

(Pdb) c

(for continue) to start the execution of the actual script. The breakpoints you set in this way, you can clear when you're done with them with:

(Pdb) cl

(for clear)

user1556435
  • 966
  • 1
  • 10
  • 22