3.11.4 The interpreter stack

When the following functions return ``frame records,'' each record is a tuple of six items: the frame object, the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list. The optional context argument specifies the number of lines of context to return, which are centered around the current line.

getouterframes(frame[, context])
Get a list of frame records for a frame and all higher (calling) frames.

getinnerframes(traceback[, context])
Get a list of frame records for a traceback's frame and all lower frames.

currentframe()
Return the frame object for the caller's stack frame.

stack([context])
Return a list of frame records for the stack above the caller's frame.

trace([context])
Return a list of frame records for the stack below the current exception.

Stackframes stored directly or indirectly in local variables can easily cause reference cycles the garbage collector can't collect, leading to memory leaks. To avoid this, it's a good idea to explicitly remove the cycle in a finally clause. For example:

def handle_stackframe_without_leak():
    frame = inspect.currentframe()
    try:
        # do something with the frame
    finally:
        del frame
See About this document... for information on suggesting changes.