all Python 3inspectfunction
inspect.getinnerframes()
stack and frames
A list of FrameInfo records for a traceback object: from the frame where the exception was caught to the frame where it was raised.
Syntax
inspect.getinnerframes(traceback, context=1) -> list[FrameInfo]
It takes a traceback (e.__traceback__), not the exception itself, so it works after the except block too, unlike inspect.trace(). It also means a stored exception keeps every frame and its locals alive through its traceback: in long-lived structures keep text (traceback.format_exception), not the object. Via fi.frame.f_locals you can see variable values at the moment of failure, which a plain traceback does not show.
Examples
import inspect
def parse(text):
value = text.strip()
return int(value)
try:
parse(' x ')
except ValueError as e:
err = e
frames = inspect.getinnerframes(err.__traceback__, 0)
print([fi.function for fi in frames]) # → ['<module>', 'parse']
print(frames[-1].frame.f_locals['value']) # → x
del frames, err # иначе кадры живут вместе с исключением
See also
Open in the full glossarydocs.python.orgReport an inaccuracy