Ipython debugger: full trace on interactive pdb?

I recently switched from ipython0.10 to ipython0.11. In ipython0.11, I see only a small fragment of the full trace when starting the python debugger (i.e. using %pdb ), whereas in ipython0.10 I would see the full trace. As far as I can tell, the full trace is not directly accessible from the pdb command line - you can navigate through it with "u", but you cannot see it directly.

So, is there a way to show the full trace? For example, the configuration parameter <

Or, even more useful, is there a way for ipython to simply show the exception that was caught instead of showing where in the code it was caught?

EDIT: Example:

 In [1]: pdb Automatic pdb calling has been turned ON In [2]: 1/0 > <ipython-input-2-05c9758a9c21>(1)<module>() -1 1/0 ipdb> q --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /Users/adam/<ipython-input-2-05c9758a9c21> in <module>() ----> 1 1/0 ZeroDivisionError: integer division or modulo by zero 

I would like to see a ZeroDivisionError before q 'out from pdb.

+6
source share
1 answer

is there a way for ipython to simply show the Exception that was caught rather than showing where in the code it was caught?

You can use sys.excepthook :

 import sys def exc_hook(type, value, traceback): print type sys.excepthook = exc_hook 

From the sys documentation:

sys.excepthook(type, value, traceback)

This function throws the specified trace and sys.stderr exception.

When an exception is thrown and fails, the sys.excepthook interpreter is called with three arguments, the exception class, instance exception, and trace object. In an interactive session, this happens just before the control returns to the prompt; in Python, this will happen just before the program exits. Handling such top-level exceptions can be configured by assigning another function with three arguments to sys.excepthook .

sys.__displayhook__
sys.__excepthook__

These objects contain the initial displayhook and excepthook values ​​at the beginning of the program. They are saved so that displayhook and excepthook can be restored if they are replaced by broken objects.


You can also try running ipython with the --xmode option installed on Plain

From IPython Link :

 $ ipython [options] files --xmode=<modename> 

Exception reporting mode.

Valid Modes: Normal, Context, and Detailed.

Normal : looks like a regular python trace trace.

Context : prints 5 lines of context source code around each line in the trace.

Detailed : similar to the context, but additionally prints variables that are currently visible where the exception occurred (shortening their line if they are too long). This can be very slow if you have a huge data structure whose string representation is difficult to compute. Perhaps your computer freezes for some time with the processor using 100%. If this happens, you can cancel the trace with Ctrl-C (possibly by hitting it more than once).

Here are some examples of use. Note the line differences for each trace:

--xmode=Plain

 [ 19:55 jon@hozbox ~/SO/python ]$ ipython --xmode=Plain ipython-debugger-full-traceback-on-interactive-pdb.py ------------------------------------------------------------ Traceback (most recent call last): File "ipython-debugger-full-traceback-on-interactive-pdb.py", line 2, in <module> 1 / 0 ZeroDivisionError: integer division or modulo by zero 

--xmode=Context

 [ 19:55 jon@hozbox ~/SO/python ]$ ipython --xmode=Context ipython-debugger-full-traceback-on-interactive-pdb.py --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /home/jon/SO/python/ipython-debugger-full-traceback-on-interactive-pdb.py in <module>() 1 ----> 2 #!/usr/bin/python 3 1 / 0 4 5 ZeroDivisionError: integer division or modulo by zero 

--xmode=Verbose

 [ 19:54 jon@hozbox ~/SO/python ]$ ipython --xmode=Verbose ipython-debugger-full-traceback-on-interactive-pdb.py --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /home/jon/SO/python/ipython-debugger-full-traceback-on-interactive-pdb.py in <module>() 1 ----> 2 #!/usr/bin/python 3 1 / 0 4 5 ZeroDivisionError: integer division or modulo by zero 

And without specifying a .py file:

--xmode=Plain

 [ 19:55 jon@hozbox ~/SO/python ]$ ipython --xmode=Plain In [1]: 1 / 0 ------------------------------------------------------------ Traceback (most recent call last): File "<ipython console>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero 

--xmode=Context

 [ 20:03 jon@hozbox ~/SO/python ]$ ipython --xmode=Context In [1]: 1 / 0 --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /home/jon/SO/python/<ipython console> in <module>() ZeroDivisionError: integer division or modulo by zero 

--xmode=Verbose

 [ 20:01 jon@hozbox ~/SO/python ]$ ipython --xmode=Verbose In [1]: 1 / 0 --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /home/jon/SO/python/<ipython console> in <module>() ZeroDivisionError: integer division or modulo by zero 

Using the Python debugger .

+6
source

Source: https://habr.com/ru/post/900652/


All Articles