corelibs-stack-trace (1.0.1)
Installation
pip install --index-url --extra-index-url https://pypi.org/simple corelibs-stack-traceAbout this package
CoreLibs Stack Trace
CoreLibs Python Stack Trace
This is part of the Python CoreLibs
Overview
A collection of stack trace for call traces and exception traces
Install
uv add --index opj-pypi=https://git.egplusww.jp/api/packages/PyPI/pypi/simple/ corelibs-stack-trace
Usage
The CoreLibs debug support module contains the following
- corelibs_strack_trace.stack
corelibs_stack_trace.stack usage
Stack and exception tracing
from corelibs_stack_trace.stack import call_stack, exception_stack
call_stack
print a flat call stack with just method information
def call_stack(
start: int = 0,
skip_last: int = -1,
seperator: str = ' -> ',
reset_start_if_empty: bool = False
) -> str:
- start: start pos, if too high, output will empty until reset_start_if_empty is set
- skip_last: how many on the end to skip, default is the "call_stack" function itself
- seperator: string to separate each entry by, cannot be empty and falls back to ' -> ' if empty
- reset_start_if_empty: if start is set to hight and this is set to True then the start will be set back to 0 and run again
Example:
Below will print a complete call stack in the value error, sample file name is "some_test.py"
from corelibs_stack_trace.stack import call_stack
def some_func(element: str): str
if not element:
raise ValueError(f"Some problem [{call_stack()}]")
return element
def main():
some_func()
if __name__ == "__main__":
main()
Example output for above:
some_test.py:<module>:12 -> some_test.py:main:9 -> some_test.py:some_func:5
exception_stack
Catch stack during an exception thrown, points to the line where the exception was raised
def exception_stack(
exc_stack: OptExcInfo | None = None,
separator: str = ' -> '
) -> str:
- exc_stack: Optional exception stack, if not set sys.exc_info() is used
- seperator: string to separate each entry by, cannot be empty and falls back to ' -> ' if empty
Example:
from corelibs_stack_trace.stack import exception_stack
def __text_exception_test(text: str) -> str:
if not text:
raise ValueError("Call stack returned empty string")
return text
def exception_stack_test():
try:
__text_exception_test("")
except ValueError as e:
exc_stack_str = exception_stack()
print(f"Exception Stack Test: {e}")
print(exc_stack_str)
def main():
exception_stack_test()
if __name__ == "__main__":
main()
Example output for above:
some_test.py:exception_stack_test:9 -> some_test.py:__text_exception_test:5
Development
UV setup
uv must be installed
Python venv setup
After clone, run the command below to install all dependenciss
uv sync
Build and Publish
uv build
uv publish --index opj-pypi --token <gitea token>
Python tests
All python tests are the tests/ folder. They are structured by the source folder layout
run them with
uv run pytest
Get a coverate report
uv run pytest --cov=<project>
uv run pytest --cov=<project> --cov-report=term-missing
Other tests
In the test-run folder usage and run tests are located, runt them below
uv run test-run/<script>