python ipdb偶尔显示没有代码行 [英] python ipdb occasionally shows no code lines

查看:214
本文介绍了python ipdb偶尔显示没有代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

import ipdb; ipdb.set_trace()

有时(但不总是)将我丢入ipdb em>显示周围的代码行,即使我发出 l 命令。也就是说,我得到一些类似于

sometimes (but not always) drops me into ipdb without showing surrounding lines of code, even if I issue the l command. Ie, I get something like

> /path/to/file.py(58)main()
ipdb>

而不是

> /path/to/file.py(58)main()
-> print('hello 2')
  55     print('hello')
  56     import pdb; pdb.set_trace()
  57
  58  -> print('hello 2')
  59     print('hello 3')
ipdb>

有谁知道如何显示代码行?

Does anyone know how to show lines of code?

编辑:如果我 s 进入一个新功能(位于另一个文件中),则每一面都会出现一个单独的一行。

If I step into a new function (situated in another file), a single surrounding line each side does appear.

推荐答案

ipdb 是一个包装库存python调试器 pdb ,因此在 ipdb 无法显示行号的情况下

ipdb is a wrapper around the stock python debugger pdb so in those cases where ipdb can't show line numbers neither will pdb be able to do so.

pdb 从python模块 linecache 获取其源文本。还有各种各样的东西可以解决它。最明显的情况是当不是源文件时。

pdb gets its source text from python module linecache. And there are various things that can foil it. The most obvious case is when there is no source file.

如果您正在评估字符串,则会发生这种情况。例如可能在 eval(x + 1) exec(z = 1 + 2)中间

This happens if you are evaluating a string. For example maybe are in the middle of eval("x+1") or exec("z=1+2").

按照相同的方式,您可能已经通过 exec 定义了函数,而您现在正在运行功能。例如:

Along those same lines, you might have defined function via exec and you are now in the middle of running that function. For example:

exec("def five(): return 5")
five()

您可能能够告诉您在这种情况下的方式是调整堆栈框架并查看呼叫上下文。所以当这种情况发生时,运行 up bt (backtrace):

The way you may be able to tell that you are in this kind of case is to adjust the stack frame and look at the calling context. So when that happens, run up or bt (backtrace):

如果您看到:

 (Pdb) up
> <string>(1)<module>()

; string> 事情意味着你在这种情况。回溯可能会显示如下:

The <string> thing means that you are in this situation. A backtrace may show something like:

/usr/lib/python2.7/bdb.py(400)run()
-> exec cmd in globals, locals
> <string>(1)<module>()

源可能不存在的其他方式可能是源代码已被删除,或者字节码可能是全部生成 ,或通过AST。

Other ways where the source might not exist may be that the source code was deleted, or maybe the bytecode was generated all by itself, or via an AST.

另外还有一个Python调试器,名为 trepan (对于Python 3,请参阅 trepan3k ),更难尝试批次找到源文本。并且它还尝试通过使用不仅仅是文件名的basename部分来验证其显示的源代码与Python解释器的运行方式是否匹配。

There is another debugger for Python called trepan (for Python 3 see trepan3k) that tries a lot harder to find the source text. And it also tries to verify that the source code it shows matches what the Python interpreter is running by using more than just the basename part of the filename.

调试器甚至可以在没有的时候重建Python源代码!这个魔法是通过 uncompyle6 完成的。

The debugger can even reconstruct the Python source code when there is none! This magic is done via uncompyle6.

所以显示这个,这是一个这个简单的Python程序的例子:

So show this, here is an example for this simple Python program:

x = 3
eval("x+1")
exec("z=2")

现在我们运行调试器:

$ trepan3k /tmp/foo.py
(/tmp/foo.py:1): <module>
-> 1 x = 3
(trepan3k) step
(/tmp/foo.py:2 @6): <module>
-- 2 eval("x+1")

(trepan3k) step
(<string>:1): <module>
(/tmp/foo.py:2 @12): <module>
-> 2 eval("x+1")
(trepan3k) list
** No file <string> found
(trepan3k) deparse .
return x + 1
(trepan3k) step
(<string>:1 @7): <module>
(/tmp/foo.py:2 @12): <module>
<- 2 eval("x+1")
R=> 4
(trepan3k) step
(/tmp/foo.py:3 @16): <module>
-- 3 exec("z=2")
(trepan3k) list
End position changed to last line 3 
 1      x = 3
 2      eval("x+1")
 3  ->  exec("z=2")
(trepan3k) step
(<string>:1): <module>
(/tmp/foo.py:3 @22): <module>
-> 3 exec("z=2")
(trepan3k) list
** No file <string> found
(trepan3k) deparse .
z = 2

如果这还不够,您还可以拆卸代码以查看那。如果你碰巧知道python源文件在哪里,但由于某些原因,调试器无法自行找到它,您可以使用 set substitute

If this isn't enough, you can also disassemble the code to see that. And if you happen to know where the python source file is but for some reason the debugger can't find it on its own, you can tell it where the source code lives using set substitute.

这篇关于python ipdb偶尔显示没有代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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