gpt4 book ai didi

python - ipython 调试器 : full traceback on interactive pdb?

转载 作者:太空狗 更新时间:2023-10-30 00:00:20 25 4
gpt4 key购买 nike

我最近从 ipython0.10 切换到 ipython0.11。在 ipython0.11 中,当 python 调试器参与时(即使用 %pdb),我只看到完整回溯的一小段,而在 ipython0.10 中,我会看到完整的回溯。据我所知,完整的回溯不能直接从 pdb 命令行访问——您可以使用“u”浏览它,但不能直接看到它。

那么,有没有办法显示完整的回溯?比如配置参数?

或者,更有用的是,有什么方法可以让 ipython 只显示捕获到的异常,而不是显示它在代码中的什么位置被捕获?

编辑:示例:

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

我希望在 q 从 pdb 中看到 ZeroDivisionError。

最佳答案

is there any way to have ipython just show the Exception that was caught, rather than showing where in the code it was caught?

你可以使用 sys.excepthook :

import sys

def exc_hook(type, value, traceback):
print type

sys.excepthook = exc_hook

来自sys module documentation :

sys.excepthook(type, value, traceback)

This function prints out a given traceback and exception to sys.stderr.

When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook.

sys.__displayhook__
sys.__excepthook__

These objects contain the original values of displayhook and excepthook at the start of the program. They are saved so that displayhook and excepthook can be restored in case they happen to get replaced with broken objects.


您也可以尝试将 --xmode 选项设置为 Plain

来启动 ipython

来自 IPython reference :

$ ipython [options] files

--xmode=<modename>

Mode for exception reporting.

Valid modes: Plain, Context and Verbose.

Plain: similar to python’s normal traceback printing.

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

Verbose: similar to Context, but additionally prints the variables currently visible where the exception happened (shortening their strings if too long). This can potentially be very slow, if you happen to have a huge data structure whose string representation is complex to compute. Your computer may appear to freeze for a while with cpu usage at 100%. If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting it more than once).

以下是一些示例用法。请注意每个回溯的行数差异:

--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

并且不指定 .py 文件:

--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 .

关于python - ipython 调试器 : full traceback on interactive pdb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com