Get reference to the current exception
Asked Answered
P

2

23
$ ./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> 

I could not find any command in ipdb help that shows me current exception.

Doing import sys; print sys.exc_info() doesn't work.

Currently I do:

try:
    do_something_that_raises_an_exception()
except Exception as exc:
    import ipdb; ipdb.set_trace()

then I can work with exc to analyze it.

How to easily get a reference to the currently effective exception?

Perjure answered 27/12, 2013 at 9:15 Comment(3)
Looks like this may have already been answered here: https://mcmap.net/q/517476/-get-last-exception-in-pdbSallust
@AaronD, the answer in your link only works in pdb, not ipdb...Machutte
@AaronD, i am getting AttributeError: 'module' object has no attribute 'last_value' when trying to retrieve sys.last_value inside an except: bugs.python.org/issue6307Perjure
M
45

This has frustrated me too for a while. I eventually found the answer here, along with a good detailed explanation.

The short answer is, use the ! magic prefix (!sys.exc_info()):

In [4]: 1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
...
ipdb> !sys.exc_info()
(<type 'exceptions.AttributeError'>, AttributeError("'exceptions.ZeroDivisionError' object has no attribute '_render_traceback_'",), <traceback object at 0x101c55fc8>)

This basically tells the debugger: "guessing wouldn't be necessary. it is python code I'm typing", thus preventing it from trying to guess what you mean by "sys", a process during which some internal exception is raised, overwriting sys.exc_info(), which used to hold your original exception.

Machutte answered 9/4, 2014 at 20:28 Comment(5)
Thanks, this works. I was hoping there is a built-in ipdb command for this, otherwise I have to type: !import sys; sys.exc_info()Perjure
@warwaruk you can define an alias in your .pdbrcMachutte
Thanks! I ended up with putting alias exc_info !import sys; sys.exc_info() into ~/.pdbrcPerjure
This works in both pdb & ipdb. To assign to a variable, you can use !import sys; exc=sys.exc_info() then you can explore it, import traceback and print it, etc.Skylark
Very helpful!. FYI, The link for where you got the idea no longer leads to anything relevant.Epsomite
M
0

Since Python 3.12, you can access the current exception as $_exception.

See here for details.

Machutte answered 3/5 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.