简体   繁体   中英

`inspect.trace()` vs `traceback`

I am confused about the difference between two objects:

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

From the documentation of inspect.trace:

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.

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)

(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() is obviously a bit nicer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM