是否可以删除使用ipdb.set_trace()设置的断点? [英] Is it possible to remove a break point set with ipdb.set_trace()?

查看:428
本文介绍了是否可以删除使用ipdb.set_trace()设置的断点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python代码中的某处使用了ipdb.set_trace().是否可以使用IPDB命令忽略此断点?

I used ipdb.set_trace() somewhere in my Python code. Is it possible to ignore this break point using a IPDB command?

clear告诉我它清除了所有断点,但是当IPDB偶然碰到ipdb.set_trace()行时,它再次停止.

clear tells me that it cleared all break points, but IPDB stops again when it stumbles upon the line with ipdb.set_trace().

disable 1告诉我:No breakpoint numbered 1 ignore 1说:Breakpoint index '1' is not valid

要澄清一下:当然,我可以简单地从源代码中删除断点.但这需要退出调试器并重新启动它.通常,需要花费很多工作才能到达某个地方,而重新启动调试器会使工作变得更加困难.同样,如果有一个巨大的循环,并且您要检查循环中的对象,最简单的方法是在循环之后直接在对象之后放置一个断点.然后我该如何跳过循环(以及成千上万的调用set_trace()),并在循环后使用next单步执行代码?

To clarify: Of course I could simply remove the break point from my source code. But this would require to quit the debugger and to start it again. Often it needs a lot of work to get somewhere and restarting the debugger makes life more difficult. Also if there is a huge loop and you want inspect objects in the loop, the easiest is to place a break point in the loop directly after the object. How could I then skip the loop (and all thousands of calls set_trace()) and step through the code after the loop using next?

推荐答案

好吧,您可以利用Python中的任何东西都是对象的事实.在调试器中,您可以执行以下操作:

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仍将被调用,但不会执行任何操作. 当然,它有些永久,但是您可以做

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.

(为什么要这样做?当您在通常称为try/except的通常调用的函数中意外地设置断点时,一旦意识到要在该函数中停止1000次,就可以尝试按ctrl- c,但是被try/except捕获了,您又回到了ipdb中.因此,如果您使用的是低级代码,请确保您的set_traces具有一些上下文:

(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()

这篇关于是否可以删除使用ipdb.set_trace()设置的断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆