Is it possible to remove a break point set with ipdb.set_trace()?

半城伤御伤魂 提交于 2019-12-03 10:39:29

Well, you CAN take advantage of the fact that anything in Python is an object. While in the debugger, you can do something like this:

def f(): pass
ipdb.set_trace = f

set_trace will still be called, but it won't do anything. Of course, it's somewhat permanent, but you can just do

reload ipdb

and you'll get the original behaviour back.

(why would you do this? when you accidentally put a breakpoint in an often-called function that is usually called under a try/except. Once you realize you're stopping 1000 times in this function, you try to ctrl-c, but that gets caught by the try/except and you're back in ipdb again. So, if you're in low-level code, make sure your set_traces have some context:

if myvar in ['some', 'sentinel', 'values']:
    ipdb.set_trace()

etc.

After learning from Corley

ipdb.set_trace = lambda: None

Works for me.

Based on Corley and GLaDOS answers, you can use this trick to set_trace for several loops without overwriting the ipdb.set_trace()

import ipdb
dbg1 = ipdb.set_trace  # BREAKPOINT
for i in range(10):
    my_var2 = 10 / 3
    dbg1()  # BREAKPOINT
    dbg1 = lambda: None
    print(my_var2)


dbg2 = ipdb.set_trace  # BREAKPOINT
for i in range(10):
    my_var2 = 20 / 3
    dbg2()  # BREAKPOINT
    dbg2 = lambda: None
    print(my_var2)

Works for me like a charm.

m01

Running the program should also tell you exactly where you've set your idb.set_trace() when it's being hit (otherwise, try the where or bt commands). You can then remove that line from the file, and restart the program.

Otherwise, you might find this useful, if you feel more experimental.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!