【发布时间】:2014-01-14 21:10:15
【问题描述】:
$ ./runtests.py -v tests/managers/test_customer.py:CustomerManagerTest.test_register_without_subscription --ipdb
...
test_register_without_subscription (tests.managers.test_customer.CustomerManagerTest) ...
- TRACEBACK --------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/case.py", line 331, in run
testMethod()
File "*****/tests/managers/test_customer.py", line 198, in test_register_without_subscription
1/0
ZeroDivisionError: integer division or modulo by zero
--------------------------------------------------------------------------------
> *****/tests/managers/test_customer.py(198)test_register_without_subscription()
197 def test_register_without_subscription(self):
--> 198 1/0
199 ...
ipdb> import sys
ipdb> sys.exc_info()
(<type 'exceptions.AttributeError'>, AttributeError("Pdb instance has no attribute 'do_sys'",), <traceback object at 0x47eb908>)
ipdb>
我在ipdb help 中找不到任何显示当前异常的命令。
import sys; print sys.exc_info() 不起作用。
目前我这样做:
try:
do_something_that_raises_an_exception()
except Exception as exc:
import ipdb; ipdb.set_trace()
然后我可以和exc一起分析它。
如何轻松获得对当前有效异常的引用?
【问题讨论】:
-
看起来这可能已经在这里得到了回答:stackoverflow.com/a/19211195/399704
-
@AaronD,您链接中的答案仅适用于
pdb,不适用于ipdb... -
@AaronD,当我尝试在
except中检索sys.last_value时,我得到AttributeError: 'module' object has no attribute 'last_value':bugs.python.org/issue6307
标签: python debugging exception pdb ipdb