Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of traceback objects in Python?

import sys

try:
    raise Exception('foobar')
except:
    info = sys.exc_info()

print(type(e[2])) # <class 'traceback'>
help(traceback) # NameError: name 'traceback' is not defined

What exactly is the type of the traceback objects that Python uses for exception reporting?

The docs on sys.exc_info mention the Reference Manual, but while I've found plenty of information on how to manipulate traceback instances, I want to be able to access the type (class) itself.

like image 588
Noah Avatar asked Jul 26 '17 21:07

Noah


People also ask

What is traceback in Python?

Last Updated : 01 Aug, 2020 Traceback is a python module that provides a standard interface to extract, format and print stack traces of a python program. When it prints the stack trace it exactly mimics the behaviour of a python interpreter. Useful when you want to print the stack trace at any step.

What is the use of stack trace in Python?

This module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.

What are the functions of the traceback module?

The module uses traceback objects — this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info (). The module defines the following functions: Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame) if limit is positive.

How to print the limit of a stack trace in Python?

The module uses traceback objects, this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info (). traceback. print_tb (tb, limit = None, file = None) : If limit is positive it prints upto limit stack trace entries from traceback object tb. Otherwise, print the last abs (limit) entries.


1 Answers

traceback object is an instance of TracebackType present under types module.

types.TracebackType

The type of traceback objects such as found in sys.exc_info()[2].

>>> from types import TracebackType    
>>> isinstance(info[2], TracebackType)
True    
>>> TracebackType
<class 'traceback'>

As pointed out by @user2357112 the name TracebackType is basically an alias to the internal traceback type and is set by raising an exception in types module. The actual traceback type can be found in CPython code.

like image 149
Ashwini Chaudhary Avatar answered Oct 19 '22 20:10

Ashwini Chaudhary