Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 566
max Avatar asked Apr 11 '12 22:04

max


People also ask

What is the use of traceback?

In Python, A traceback is a report containing the function calls made in your code at a specific point i.e when you get an error it is recommended that you should trace it backward(traceback). Whenever the code gets an exception, the traceback will give the information about what went wrong in the code.

How do I fix Python traceback error?

If there is no variable defined with that name you will get a NameError exception. To fix the problem, in Python 2, you can use raw_input() . This returns the string entered by the user and does not attempt to evaluate it. Note that if you were using Python 3, input() behaves the same as raw_input() does in Python 2.

What is traceback Format_exc?

traceback. format_exc (limit=None, chain=True) This is like print_exc(limit) but returns a string instead of printing to a file. traceback. format_tb (tb, limit=None)


1 Answers

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.

like image 101
quodlibetor Avatar answered Nov 01 '22 16:11

quodlibetor