PDB:在控制台中时出现异常 - 完整堆栈跟踪

发布于 2024-08-30 17:01:41 字数 261 浏览 3 评论 0原文

在 pdb 控制台中,输入导致异常的语句只会导致单行堆栈跟踪,例如,

(Pdb) someFunc()
*** TypeError: __init__() takes exactly 2 arguments (1 given)

但是我想找出 someFunc 中错误的确切来源。即在这种情况下,__init__ 附加到哪个类。

有没有办法在 Pdb 中获取完整的堆栈跟踪?

When at the pdb console, entering a statement which causes an exception results in just a single line stack trace, e.g.

(Pdb) someFunc()
*** TypeError: __init__() takes exactly 2 arguments (1 given)

However I'd like to figure out where exactly in someFunc the error originates. i.e. in this case, which class __init__ is attached to.

Is there a way to get a full stack trace in Pdb?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-09-06 17:01:41

最简单的方法是在代码中定义一个函数,调用 someFunc() 并打印回溯,然后从 Pdb 调用该函数。

或者,您可以自己打印回溯。给定这个源代码:

def foo(a):
    pass

def bar(b):
    foo(b, 2)

def some_func():
    bar(3)

if __name__=='__main__':
    import pdb
    pdb.set_trace()

那么我们可以这样做:

C:\temp>test.py
--Return--
> c:\temp\test.py(12)<module>()->None
-> pdb.set_trace()
(Pdb) import traceback
(Pdb) exec "try: some_func()\nexcept: traceback.print_exc()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\temp\test.py", line 8, in some_func
    bar(3)
  File "C:\temp\test.py", line 5, in bar
    foo(b, 2)
TypeError: foo() takes exactly 1 argument (2 given)
(Pdb)

The easiest way would be to define a function in your code that calls someFunc() and prints the traceback then call that from Pdb.

Alternatively you can print the traceback for yourself. Given this source code:

def foo(a):
    pass

def bar(b):
    foo(b, 2)

def some_func():
    bar(3)

if __name__=='__main__':
    import pdb
    pdb.set_trace()

Then we can do this:

C:\temp>test.py
--Return--
> c:\temp\test.py(12)<module>()->None
-> pdb.set_trace()
(Pdb) import traceback
(Pdb) exec "try: some_func()\nexcept: traceback.print_exc()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\temp\test.py", line 8, in some_func
    bar(3)
  File "C:\temp\test.py", line 5, in bar
    foo(b, 2)
TypeError: foo() takes exactly 1 argument (2 given)
(Pdb)
指尖凝香 2024-09-06 17:01:41

pdb 支持递归调用的 debug 语句:

$ nosetests xxxx -x --pdb
-> some code line with error
(Pdb) debug os.sdfafa()  # something that raises exception
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()  # new line that raised excetion
((Pdb))  # number of parentheses indicates debugger recusion depth
((Pdb)) debug os.someothersdfsdf()
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
(((Pdb))) 

pdb supports debug statements for recursive invocation:

$ nosetests xxxx -x --pdb
-> some code line with error
(Pdb) debug os.sdfafa()  # something that raises exception
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()  # new line that raised excetion
((Pdb))  # number of parentheses indicates debugger recusion depth
((Pdb)) debug os.someothersdfsdf()
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
(((Pdb))) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文