如何在Python的调试器中查看异常的详细信息?

时间:2011-06-05 05:34:18

标签: python debugging

有时我在调试时会引发异常。

例如,请考虑以下代码:

def some_function():  # Pretend this function is in a library...
    # ...and deep within the library is an exception:
    raise Exception('An exception message with valuable information.')

import pdb; pdb.set_trace()
try:
    some_function()  # Pretend I am debugging from this point using pdb.
except:
    pass

在从some_function()调用进行调试时,如果我发出next命令,我将看到有关引发[和捕获]的异常的以下详细信息:

Exception: Exceptio...ation.',)

这是我正在工作的终端的直接复制/粘贴:

> /tmp/test.py(7)<module>()
-> some_function()  # Pretend I am debugging from this point using pdb.
(Pdb) next
Exception: Exceptio...ation.',)
> /tmp/test.py(7)<module>()
-> some_function()  # Pretend I am debugging from this point using pdb.
(Pdb) 

查看整个异常消息会很有用。我怎么能在pdb中做到这一点?

2 个答案:

答案 0 :(得分:22)

pdb将异常类型和值存储在__exception__中。您可以使用以下内容在pdb中打印回溯的异常部分:

import traceback; print "".join(traceback.format_exception_only(*__exception__))

例如:

> /tmp/test.py(7)<module>()
-> some_function()  # Pretend I am debugging from this point using pdb.
(Pdb) next
Exception: Exceptio...ation.',)
> /tmp/test.py(7)<module>()
-> some_function()  # Pretend I am debugging from this point using pdb.
(Pdb) import traceback; print "".join(traceback.format_exception_only(*__exception__))
Exception: An exception message with valuable information.

(Pdb) 

不幸的是,这不包括其余的回溯,但所有这些信息都可以通过where的{​​{1}}命令获得。如果您真的想要完整的追溯,可以将以下内容添加到pdb文件中或将其粘贴到终端中:

~/.pdbrc

然后你可以使用新的!global __currentframe, __stack; from inspect import currentframe as __currentframe, stack as __stack !global __format_exception_only, __print_stack; from traceback import format_exception_only as __format_exception_only, print_stack as __print_stack !global __Pdb; from pdb import Pdb as __Pdb # find the last frame on the stack with an object named "pdb" or "self" that is a pdb.Pdb object # works for pdb called the usual way, or pdb.pm(), or pdb.set_trace() !global __pdb; __pdb = [__framerec[0].f_locals.get("pdb") or __framerec[0].f_locals.get("self") for __framerec in __stack() if (__framerec[0].f_locals.get("pdb") or __framerec[0].f_locals.get("self")).__class__ == __Pdb][-1] alias traceback __print_stack(__pdb.stack[-1][0]); print "".join(__format_exception_only(*__exception__)) 别名来获得你想要的东西:

traceback

警告:所有这些都依赖于未记录的> /tmp/test.py(7)<module>() -> some_function() # Pretend I am debugging from this point using pdb. (Pdb) next Exception: Exceptio...ation.',) > /tmp/test.py(7)<module>() -> some_function() # Pretend I am debugging from this point using pdb. (Pdb) traceback File "test.py", line 7, in <module> some_function() # Pretend I am debugging from this point using pdb. File "test.py", line 3, in some_function raise Exception('An exception message with valuable information.') Exception: An exception message with valuable information. (Pdb) pdb内部,并可能会中断。

答案 1 :(得分:6)

python调试器没有“中断异常” - 如果你习惯了这个功能,这可能会非常令人沮丧。因此,我采取了记录条纹并从那里开始工作的政策。

import logging
try:
    raise Exception('An exception message with valuable information.')
except:
    logging.exception('Error in test code')

如果您使用一个好的IDE(例如带有pydev的Eclipse),则堆栈跟踪的日志条目将成为超链接,直接跳转到代码中的适当位置。

您可以通过导入traceback

在代码中的任何位置转储堆栈跟踪
import traceback
trace = traceback.format_exc()