No project description provided
Project description
STACKWALKER
A Python library for bidirectional call stack inspection and frame analysis. Provides utilities for walking through Python's call stack in both directions - from current frame to callers (backward) and from root to current frame (forward).
Features
- Bidirectional Navigation: Navigate call stack both backward (to callers) and forward (from root)
- Frame Information Extraction: Get function names, line numbers, and module information
- Safe Frame Handling: Graceful error handling with sensible defaults
- Flexible Frame Search: Find frames by function name and module
- Complete Stack Analysis: Collect and analyze entire call stacks
Installation
pip install stackwalker
Quick Start
from stackwalker import stackwalker
def caller_function():
target_function()
def target_function():
# Get current frame info
current = stackwalker.get_frame_by_index(0)
print(f"Current: {current['caller_name']}") # target_function
# Get caller frame info
caller = stackwalker.get_frame_by_index(-1)
print(f"Caller: {caller['caller_name']}") # caller_function
# Get complete frame list
frames = stackwalker.get_frame_list()
for frame in frames:
print(f"{frame['caller_name']} at line {frame['caller_line']}")
API Reference
Core Functions
get_frame_by_index(index: int)
Get frame information by index position.
index = 0: _collect_frame_stack()index < 0: Navigate backward to callers, -4 is caller of get_frame_by_index.index > 0: Navigate forward from root caller (frames before stackwalker)
get_frame_by_name(caller_name: str, module_name: str, offset: int = 1)
Find a frame by function and module name.
get_frame_list()
Get complete list of all frames in the call stack.
get_frame_name_list()
Get list of (module_name, function_name) tuples for all frames.
Frame Information Structure
Each frame returns a dictionary with:
{
'caller_name': str, # Function name
'caller_line': int, # Line number
'module_name': str, # Module name
'caller_locals': dict, # Local variables
'caller_filename': str # Source filename
}
Use Cases
- Debugging Tools: Build sophisticated debugging utilities
- Logging Systems: Automatic caller detection for logs
- Code Analysis: Analyze call patterns and stack traces
- Testing Utilities: Inspect test execution context
- Profiling: Track function call hierarchies
Requirements
- Python 3.8+
- Works with standard Python implementations that support
inspect.currentframe()
Testing
# Using unittest
python -m unittest tests.test_stackwalker
# Using pytest
pytest tests/test_stackwalker.py
License
MIT License - see LICENSE file for details.
Contributing
Contributions welcome! Please read the contributing guidelines and submit pull
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stackwalker-0.4.2-py3-none-any.whl.
File metadata
- Download URL: stackwalker-0.4.2-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
196ff3713263f39e63217c28e53e19d0476174fa2ee831d8efb8aa75b33d6e11
|
|
| MD5 |
b609439a1ae1d467bd9cddc47bec59f7
|
|
| BLAKE2b-256 |
b968cba4d78085e0117051ee40dcbdaf2e6daf1b235c884037ab419e3b84b309
|