Python, pdb, adding a breakpoint that just breaks

I sometimes set breakpoints in deep loop code as follows:

import pdb; pdb.set_trace() 

If I press c , then it continues, but is interrupted again at the next iteration of the loop. Is there any way to clear this pdb breakpoint? The b command does not list it.

Or is there one liner that I can paste into my Python source file that sets a soft breakpoint that can be cleared?

Or, ideally, one insert that sets the trace, then clears?


Edit: I'm interested in any editor that allows you to set breakpoints.

I am currently running my script from emacs as follows:

 Mx pdb Run ~/.virtualenvs/.../python2.7/pdb.py (like this): ~/.virtualenvs/.../python2.7/pdb.py ~/start-myserver.py 
+4
source share
4 answers

One insert that sets the trace is then cleared (overwritten by an empty function):

 import pdb; pdb.set_trace(); pdb.set_trace = lambda: 0 

Call reload(pdb) to restore pdb.set_trace . ( imp.reload(pdb) in Python 3.x)

+1
source

Instead of setting a breakpoint with set_trace you can configure and run the debugger manually. pdb.Pdb.set_break() takes a temprorary argument, which will remove the breakpoint on the first hit.

 import pdb def traced_function(): for i in range(4): print(i) # line 5 if __name__ == '__main__': import pdb p = pdb.Pdb() # set break on the print line (5): p.set_break(__file__, 5, temporary=True) p.run('traced_function()') 

Output Example:

 $ python pdb_example.py > <string>(1)<module>() (Pdb) c Deleted breakpoint 1 at /tmp/pdb_example.py:5 > /tmp/test.py(5)traced_function() -> print(i) # line 5 (Pdb) c 0 1 2 3 

The same could be done by running the program using pdb from the command line, but setting this way allows you to save breakpoints between calls and not lose them when you exit the debugger session.

+1
source

Given that import pdb; pdb.set_trace() import pdb; pdb.set_trace() is a temporary line of code, I think the easiest way is to just use a boolean:

 has_trace = True for line in data: if has_trace: import pdb; pdb.set_trace() has_trace = False # ... other code here 
0
source

Maybe not what you want, but in emacs,

I can break the C-space and unlock it with some key (which I cannot remember :()

-one
source

Source: https://habr.com/ru/post/1498366/


All Articles