ipython 调试器:交互式 pdb 上的完整回溯?

发布于 2024-12-13 21:55:42 字数 831 浏览 0 评论 0原文

我最近从 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

我希望在 pdb 中看到 ZeroDivisionError before q'。

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

So, is there any way to show the full traceback? Such as a configuration parameter?

Or, even more usefully, is there any way to have ipython just show the Exception that was caught, rather than 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'd like to see the ZeroDivisionError before q'ing out of the pdb.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-12-20 21:55:43

有没有办法让 ipython 只显示异常
捕获,而不是显示在代码中的何处捕获?

您可以使用 sys.excepthook

import sys

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

sys.excepthook = exc_hook

来自 sys 模块文档

sys.excepthook(类型、值、回溯)

该函数打印出给定的回溯和异常
sys.stderr。

当引发异常且未捕获异常时,解释器会调用
sys.excepthook 带三个参数,异常类,异常
实例和回溯对象。在交互式会话中,这
发生在控制权返回提示之前;在Python中
程序这发生在程序退出之前。的处理
可以通过分配另一个来自定义此类顶级异常
sys.excepthook 的三参数函数。

sys.__displayhook__
sys.__excepthook__

这些对象包含displayhook和的原始值
excepthook 在程序开始处。他们得救是为了
displayhook 和 excepthook 可以在碰巧发生时恢复
替换为损坏的物体。


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

IPython 参考

$ ipython [选项] 文件

--xmode=<模式名称>;

异常报告模式。

有效模式:Plain、Context 和 Verbose。

Plain:类似于Python的普通回溯打印。

上下文:在回溯中的每一行周围打印 5 行上下文源代码。

详细:与 Context 类似,但另外打印异常发生处当前可见的变量(缩短它们的长度)
如果字符串太长)。如果发生这种情况,这可能会非常慢
拥有一个巨大的数据结构,其字符串表示很复杂
来计算。您的计算机可能会出现 CPU 冻结一段时间
使用率为 100%。如果发生这种情况,您可以使用以下命令取消回溯
Ctrl-C(可能不止一次)。

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

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

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

使用 Python 调试器

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

You could use sys.excepthook:

import sys

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

sys.excepthook = exc_hook

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


You can also try starting ipython with the --xmode option set to Plain

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

Here are some example usages. Notice the difference in lines for each traceback:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文