Stack Data

0.6.3 · active · verified Sat Mar 28

stack-data is a Python library designed to extract detailed information from stack frames and tracebacks, offering more useful and customizable displays than Python's default tracebacks. It's currently at version 0.6.3 and is actively maintained, powering features in tools like IPython and futurecoder.

Warnings

Install

Imports

Quickstart

This example demonstrates basic usage by creating a FrameInfo object from the current frame and then iterating through its lines to print the source code, highlighting the current line.

import inspect
import stack_data

def foo():
    result = []
    for i in range(2):
        row = []
        result.append(row)
        print_stack()
        for j in range(2):
            row.append(i * j)
    return result

def print_stack():
    frame = inspect.currentframe().f_back
    frame_info = stack_data.FrameInfo(frame)
    print(f"{frame_info.code.co_name} at line {frame_info.lineno}")
    print("-----------")
    for line in frame_info.lines:
        print(f"{'-->' if line.is_current else ' '} {line.lineno:4} | {line.render()}")

foo()

view raw JSON →