`inspect.trace()`vs`traceback` [英] `inspect.trace()` vs `traceback`

查看:219
本文介绍了`inspect.trace()`vs`traceback`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对两个对象之间的区别感到困惑:

I am confused about the difference between two objects:


  • inspect.trace返回的框架列表()正在处理异常

  • sys.exc_info()[2] (或通过调用 sys.excepthook

  • the list of frames returned by inspect.trace() while an exception is being handled
  • the traceback returned by sys.exc_info()[2] (or passed in the call to sys.excepthook)

两个对象包含相同的信息,只是组织成不同的数据结构?

Do the two objects contain the same information, just organized into a different data structure? If not, what one has that the other doesn't?

推荐答案

从inspect.trace的文档:

From the documentation of inspect.trace:


inspect。 trace ([context])

inspect.trace([context])

当前帧与当前正在处理的异常的帧之间的堆栈的帧记录列表被引入。列表中的第一个条目表示调用者;最后一个条目表示提出异常的地方。

Return a list of frame records for the stack between the current frame and the frame in which an exception currently being handled was raised in. The first entry in the list represents the caller; the last entry represents where the exception was raised.

这表明它提供了一个很好的方法来切片和骰子,从 sys.exc_info()[2] 你得到。

which suggests that it provides a nice way to slice and dice which frames from sys.exc_info()[2] you get.

哪个,如果你看看来源:

Which, if you look at the source:

def trace(context=1):
    """Return a list of records for the stack below the current exception."""
    return getinnerframes(sys.exc_info()[2], context)

对于 3.2 2.7 ),正是它的作用,但它通过 getinnerframes ,根据docstring将其注释为一些有用的信息:

(identical for 3.2 or 2.7), is exactly what it does, but it passes it through getinnerframes, which annotates it with some useful information, per the docstring:


获取一份记录列表追踪框架和所有较低的框架。

Get a list of records for a traceback's frame and all lower frames.

每个记录都包含一个框架对象,文件名,行号,函数
名称,上下文行列表和索引。

Each record contains a frame object, filename, line number, function name, a list of lines of context, and index within the context.

而且,由于我对这实际意义很好奇:

And, since I'm curious about what that actually means:

import sys
import inspect
from pprint import pprint


def errorer():
    raise Exception('foo')

def syser():
    try:
        errorer()
    except Exception, e:
        tb = sys.exc_info()[2]
        print tb.tb_frame
        print tb.tb_lasti
        print tb.tb_lineno
        print tb.tb_next

def inspecter():
    try:
        errorer()
    except Exception, e:
        pprint(inspect.trace())

从提示中调用时,这些字段和对象的易于查找定义:

Which, when called from the prompt, while recalling that many of those fields and objects have easy-to-find definitions:

>>> syser()
<frame object at 0x1441240>
6
10
<traceback object at 0x13eb3b0>
>>> inspecter()
[(<frame object at 0x14a5590>,
  '/tmp/errors.py',
  22,
  'inspecter',
  None,
  None),
 (<frame object at 0x14a21b0>,
  '/tmp/errors.py',
  8,
  'errorer',
  None,
  None)]

(线数跳动,因为我搞砸了格式)

(line numbers jumped around because I messed with formatting)

inspect.trace()显然更好一点。

这篇关于`inspect.trace()`vs`traceback`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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