ipython调试器:交互式pdb上的完全回溯? [英] ipython debugger: full traceback on interactive pdb?

查看:119
本文介绍了ipython调试器:交互式pdb上的完全回溯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从ipython0.10切换到ipython0.11。在ipython0.11中,我只看到python调试器参与时的完整回溯的小片段(即使用%pdb ),而在ipython0.10中,我会看到完全追溯。据我所知,pdb命令行无法直接访问完整的回溯 - 您可以使用'u'导航它,但无法直接看到它。



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



或者,更有用的是,有没有办法让ipython只显示被捕获的异常,而不是显示代码中的位置被抓了?



编辑:示例:

 在[1]中:pdb 
自动pdb呼叫已开启

在[2]中:1/0
> < ipython-input-2-05c9758a9c21>(1)< module>()
-1 1/0

ipdb> q
--------------------------------------------- ------------------------------
ZeroDivisionError Traceback(最近一次调用最后一次)
/ Users / adam /< IPython的输入-2-05c9758a9c21> in< module>()
----> 1 1 $

ZeroDivisionError:整数除法或模数为零

I我希望在 q 之前看到ZeroDivisionError 从pdb中出来。

解决方案


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


您可以使用 sys.excepthook

  import sys 

def exc_hook(类型,值,追溯):
打印类型

sys.excepthook = exc_hook

来自 sys 模块文档


sys.excepthook(type,value,追溯)



此函数打印出给定的tra
sys.stderr的ceback和异常。



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



sys .__ displayhook __

sys .__ excepthook __



这些对象包含程序开头的displayhook和
excepthook的原始值。它们被保存,以便
displayhook和excepthook可以恢复,以防它们碰巧用破损的物体取代







您也可以尝试启动ipython,并将 - xmode 选项设置为 Plain



来自 IPython参考


  $ ipython [options] files 

--xmode =< modename>

异常报告模式。



有效模式:普通,上下文和详细。



普通:类似于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(最近一次调用最后一次):
文件ipython-debugger-full-traceback-on -interactive-pdb.py,第2行,< module>
1/0
ZeroDivisionError:整数除法或模数为零

- xmode =上下文

  [19 :55 jon @ hozbox~ / SO / python] $ ipython --xmode =上下文ipython-debugger-full-traceback-on-interactive-pdb.py 
------------- -------------------------------------------------- ------------
ZeroDivisionError Traceback(最近一次调用最后一次)

/ 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:整数除法或模数为零

- xmode =详细

  [19:54 jon @ hozbox~ / SO / python] $ ipython --xmode =详细ipython-debugger-full-traceback- on-interactive-pdb.py 
--------------------------------------- ------------------------------------
ZeroDivisionError Traceback(最近一次调用最后一次)

/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:整数除法或模数为零

并且未指定.py文件:



- xmode =普通

  [19:55 jon @hozbox~ / SO / python] $ ipython --xmode = Plain 

在[1]中:1/0
--------------- ---------------------------------------------
Traceback (最近的呼叫最后一次):
文件< ipython console>,第1行,< module>
ZeroDivisionError:整数除法或模数为零

--xmode =上下文

  [20:03 jon @ hozbox~ / SO / python] $ ipython --xmode =上下文

在[1]中:1/0
------------------- -------------------------------------------------- ------
ZeroDivisionError Traceback(最近一次调用最后一次)

/ home / jon / SO / python /< ipython console> in< module>()

ZeroDivisionError:整数除法或模数为零

- xmode =详细

  [20:01 jon @ hozbox~ / SO / python] $ ipython --xmode =详细


在[1]中:1/0
---- -------------------------------------------------- ---------------------
ZeroDivisionError Traceback(最近一次调用最后一次)

/ home / jon / SO / python / < ipython console> in< module>()

ZeroDivisionError:整数除法或模数为零



< hr>

使用Python调试器


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.

解决方案

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.

这篇关于ipython调试器:交互式pdb上的完全回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆