Noah
Noah

Reputation: 1429

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.

Upvotes: 26

Views: 10525

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

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.

Upvotes: 36

Related Questions