pdb旁路错误/跳转失败:只能从“行”跟踪事件中跳转 [英] pdb bypass error/Jump failed: can only jump from a 'line' trace event

查看:217
本文介绍了pdb旁路错误/跳转失败:只能从“行”跟踪事件中跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pdb调试Python程序。该程序可能是这样的:

I'm trying to debug a Python program using pdb. The program could be like this:

def main():
    a = 1
    print(b)
    c = 2
    d = 3

显然,print(b)是一个错字应该是print(a),但这并不重要,我可以使用文本编辑器修复它,但是我想绕过此错误并继续调试。

Apparently, print(b) is a typo which should be print(a) but it is not important and I can fix it with the text editor but I want to bypass this error and continue debugging.

我尝试了跳转,例如跳转4(假设 c = 2是第4行),但出现错误跳转失败:f_lineno只能由一行设置跟踪功能,这意味着我在编程时需要提供行跟踪功能。

I tried jump, like jump 4(assuming "c=2" is line 4) but I was given error "Jump failed: f_lineno can only be set by a line trace function", which means I need to give a line trace function when I'm programming.

那么,有没有一种方法可以解决这个问题,或者在使用pdb时还有其他方法绕过错误行吗?

So, is there a way to deal with this problem, or is there some other way to bypass the error line when using pdb?

推荐答案

TLDR:这是 pdb的验尸模式,在这种模式下,跳跃不起作用。但这仍然非常有用。

TLDR: this is pdb's post-mortem mode in which jumping is not supposed to work. But it's still very useful.

伦勃朗绘画(公共领域)

我使用python 3.8.2将其复制为 ***跳转失败:只能从行跟踪事件跳转通过运行脚本在pdb下来执行。像这样: python3 -m pdb -cc script.py 并尝试跳到pdb提示符中的另一行,然后出现。

I reproduce it with python 3.8.2 as *** Jump failed: can only jump from a 'line' trace event by running the script "under pdb" like so: python3 -m pdb -c c script.py and trying to jump to another line in pdb prompt which then appears.

发生了什么:一个未处理的异常,在这种情况下, NameError:未定义名称'b'导致python停止解释脚本; pdb拦截了这种情况,并进入了事后检验模式。

What's happened: an unhandled exception, in this case NameError: name 'b' is not defined caused python to stop interpreting the script; pdb intercepted this situation and entered its post-mortem mode.

正如Almar Klein很好地将其放在他的博客文章

As Almar Klein nicely put it in his blog post,


事后调试是指在进入调试模式之后的概念em>东西坏了。没有涉及断点的设置,因此它非常快速,您可以检查完整的堆栈跟踪,使其成为跟踪错误的有效方法。

Post-mortem debugging refers to the concept of entering debug mode after something has broken. There is no setting of breakpoints involved, so it's very quick and you can inspect the full stack trace, making it an effective way of tracing errors.

尽管跳转下一步返回在邮寄中不起作用-mortem, bt ll pp 以及直接在pdb的交互式shell中导入模块和运行任意python代码的可能性非常有效找出根本原因的方法。在我们的简单示例中, NameError 的根本原因会在快速 ll 时立即显示:pdb前缀是>> 的代码。

Although jump, next, return won't work in post-mortem, bt, up, down, ll and pp, along with the possibility to import modules and run arbitrary python code directly in the pdb's interactive shell can be very effective ways to get the root cause. In our simple example the root cause of the NameError is shown immediately upon a quick ll: pdb prefixes the offending line of code with >>.

我们没有通过 -cc (表示继续),pdb会在提示程序的第一行之前显示其提示并暂停,因此您将有机会逐步执行整个程序或

Had we not passed -c c (meaning continue), pdb would have shown its prompt and paused before the first line of your program is interpreted, so you'd have a chance to step through the whole program or set a breakpoint before or at the offending line, and jump over it, never entering the post-mortem.

即使在事后检查中,您也可以在程序中的任何位置准备一个断点,例如:第2行中断2 ,并说 c continue 这样pdb将完成验尸,重新加载文件并使用更新的断点集重新启动程序。

Even in post-mortem, you can prepare a breakpoint anywhere in the program, e.g. break 2 for line 2, and say c or continue so pdb will finish post-mortem, reload the file, and restart the program with the updated set of breakpoints.

另一种处理方法是 import pdb pdb.set_trace()用可疑代码-或从python 3.7开始,只需 breakpoint()-并正常运行python程序(不再低于 pdb),然后允许 jump 下一个返回等,以及其他所有内容-当达到断点时。

Another way to deal with it is to import pdb and pdb.set_trace() in suspicious code - or since python 3.7, simply breakpoint() - and run the python program normally (not "under" pdb anymore) which allows then to jump, next, return etc, as well as everything else - when the breakpoint is hit.

如果您的Python程序是通过行为启动的:

If your Python program is started through behave:


  • 请确保每当使用pdb或类似的调试器(无论是否验尸模式)时,都使用-no-capture 运行它,以避免行为的stdin / stdout捕获导致pdb无响应和/或提示不可见。

  • 此外,如果要使用pdb验尸m ode:

  • make sure to run it with --no-capture whenever using pdb or similar debuggers (whether post-mortem mode or not), to avoid problems with behave's stdin/stdout capturing making pdb unresponsive and/or its prompt invisible.
  • also, if you want to use the pdb post-mortem mode:

environment.py

def after_step(context, step):  # pylint: disable=unused-argument
    if step.status == 'failed':
        import pdb
        pdb.post_mortem(step.exc_traceback)

这篇关于pdb旁路错误/跳转失败:只能从“行”跟踪事件中跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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