Registry / testing / stack-data

stack-data

JSON →
library0.6.3pypypi✓ verified 25d ago

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.

pip install stack-data
INSTALL
IMPORT
SIG · STACK-DATA
S
stack-data
testingpythonv0.6.3
Install
1.9s avg
Import
121ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.126s · 18.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.116s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

FrameInfo
from stack_data import FrameInfo
Options
from stack_data import Options
Line
from stack_data import Line

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()
Debug
Known issues
gotchaWhen inspecting recursive functions or deeply nested calls, `stack_data`, similar to Python's built-in traceback, automatically avoids showing all identical repeated frames. It instead provides a `RepeatedFrames` object. If you need to see every single frame, you must explicitly pass `collapse_repeated_frames=False` to `FrameInfo` (not to `Options`).
fix
Pass `collapse_repeated_frames=False` to `stack_data.FrameInfo` when initializing, e.g., `stack_data.FrameInfo(frame, collapse_repeated_frames=False)`.
affects: >=0.1.0
gotchaWhen rendering lines that include markers (e.g., for highlighting or inserting HTML), ensure you use `line.render(markers, escape_html=True)` if you are inserting HTML. This will correctly escape special HTML characters in the Python source code to prevent malformed output, while still allowing your markers to be interpreted as HTML.
fix
Use `line.render(markers, escape_html=True)` when preparing output for HTML display, especially if markers themselves contain unescaped HTML.
affects: >=0.1.0
gotchaThe basic `line.render()` method strips common leading indentation. If preserving exact original indentation (including common leading whitespace) is critical for your display logic, be aware that `line.render()` modifies it. More advanced control over rendering and ranges is available via `line.token_ranges` or `line.range_from_node`.
fix
For custom indentation control, consider iterating through `line.token_ranges` and reconstructing the line, or use `line.range_from_node(node, data)` for more granular control over specific parts of the line.
affects: >=0.1.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'stack_data'
The 'stack-data' package is not installed in the current Python environment, or there's a typo in the import statement (it's 'stack-data' for pip, 'stack_data' for import).
fix
Ensure the package is installed using `pip install stack-data`.
AttributeError: 'Line' object has no attribute 'text'
The user attempted to access a non-existent 'text' attribute on a `stack_data.Line` object; the textual content of a line is accessed via its `render()` method.
fix
Use `line.render()` to get the string representation of the code line.
ERROR: Package 'stack-data' requires a different Python version: '>=3.8' but you have '3.7.x'
The current Python environment's version is older than the minimum required by `stack-data` (version 0.6.3 requires Python 3.8 or newer).
fix
Upgrade your Python installation to version 3.8 or newer, or switch to an environment with a compatible Python version.
TypeError: FrameInfo expected a frame, got <class 'str'>
The `stack_data.FrameInfo` constructor was called with an argument that is not a Python `frame` object, which is required for it to extract frame data.
fix
Pass an actual frame object (e.g., obtained from `sys._getframe()` or an exception traceback) to `stack_data.FrameInfo`.
Upgrade
Version history
0.6.3latest on PyPI · released Sep 30, 2023
Audit
Dependencies
executingrequiredUsed to get the currently executing AST node of a frame and other information.
asttokensrequiredUsed to annotate AST trees with source code positions.
pure_evalrequiredUsed for safely evaluating AST nodes without side effects to identify 'interesting' expressions.
Agent activity
41 hits · last 30 days
node
36
OpenAI (training)
1
Resources
stack-data — pip install stack-data · libregistry