python怎么打开调试信息_如何在Python的调试器中查看异常的详细信息?

pdb将异常类型和值存储在__exception__中。您可以使用以下命令在pdb中打印回溯的异常部分:import traceback; print "".join(traceback.format_exception_only(*__exception__))

例如:> /tmp/test.py(7)()

-> some_function() # Pretend I am debugging from this point using pdb.

(Pdb) next

Exception: Exceptio...ation.',)

> /tmp/test.py(7)()

-> 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命令获得。如果您真的需要完整的回溯,可以将以下内容添加到~/.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)()

-> some_function() # Pretend I am debugging from this point using pdb.

(Pdb) next

Exception: Exceptio...ation.',)

> /tmp/test.py(7)()

-> some_function() # Pretend I am debugging from this point using pdb.

(Pdb) traceback

File "test.py", line 7, in

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和bdb内部,并且很可能会中断。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值