Python Glossary Русский

all Python 3inspectterm

inspect.Traceback

stack and frames

What getframeinfo() returns: filename, lineno, function, code_context, index and positions. Not to be confused with an exception's traceback object.

Syntax

info = inspect.getframeinfo(frame); info.filename, info.lineno, info.function

The name is misleading: this is not the types.TracebackType you get from e.__traceback__ but a summary of a frame (a named tuple before 3.11). Unpacking and indexing work like a five-field tuple; positions (the instruction's lines and columns, since 3.11) is not part of the tuple and is available only as an attribute. You never need to build one by hand.

Examples

import inspect, types
info = inspect.getframeinfo(inspect.currentframe())
print(type(info).__name__, info.function, info.lineno)  # → Traceback <module> 2
print(info.code_context)  # → ['info = inspect.getframeinfo(inspect.currentframe())\n']
print(len(info), info[1] == info.lineno)  # → 5 True
print(isinstance(info, types.TracebackType))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy