all Python 3inspectfunction
inspect.trace()
stack and frames
A list of FrameInfo records from the current frame to the place where the exception being handled was raised. Works only inside except.
Syntax
inspect.trace(context=1) -> list[FrameInfo]
Outside an except block trace() does not fail but silently returns an empty list, a common cause of an empty debug dump. The order is the reverse of stack(): the handler's frame comes first and the frame that raised comes last, so the failure point is trace()[-1]. Like stack(), the result holds live frames with their locals, so do not keep it around; for a log traceback.format_exc() is enough.
Examples
import inspect
def boom():
1 / 0
def mid():
boom()
try:
mid()
except ZeroDivisionError:
print([fi.function for fi in inspect.trace(0)]) # → ['<module>', 'mid', 'boom']
print(inspect.trace(0)[-1].lineno) # → 3
print(inspect.trace()) # → [] — вне except
See also
Open in the full glossarydocs.python.orgReport an inaccuracy