tracefunc
tracefunc takes a function and its arguments, executes it, and returns
a TraceResults list of per-call traces. Each entry is
(stack_str, trace_dict) where stack_str is the call stack (filtered
so fn is the shallowest frame shown; empty when target_func is
None) and trace_dict maps AST-level snippets to
(hit_count, vars_map) with per-hit samples. Comprehensions show up as
their own lines with per-iteration values. Lines that never executed are
omitted unless incl_unhit=True, and if the function raises, the
exception lands in the result’s exc attribute with the traces gathered
up to the raise still returned.
Install
pip install tracefunc
Requires Python 3.12+ (uses sys.monitoring instruction events).
How to use
from tracefunc import tracefunc
from pprint import pprint
Simple function
Here’s a simple example tracing a loop:
def demo(n):
total = 0
for i in range(n): total += i
return total
def show_res(x):
for snippet, (hits, vars_map) in x.items():
print('-', repr(snippet), hits)
pprint(vars_map)
stack, result = tracefunc(demo, 3, target_func=demo)[0]
print(stack)
show_res(result)
demo (2643322203.py:1)
- 'total = 0' 1
{'total': [('int', '0')]}
- 'for i in range(n):' 4
{'i': [('int', '0'), ('int', '1'), ('int', '2'), ('int', '2')],
'n': [('int', '3'), ('int', '3'), ('int', '3'), ('int', '3')],
'range': [('type', "<class 'range'>"),
('type', "<class 'range'>"),
('type', "<class 'range'>"),
('type', "<class 'range'>")]}
- 'total += i' 3
{'i': [('int', '0'), ('int', '1'), ('int', '2')],
'total': [('int', '0'), ('int', '1'), ('int', '3')]}
- 'return total' 1
{'total': [('int', '3')]}
Multiple statements on one physical line
Semicolon-separated statements are tracked separately.
def one_liner(): x = 1; y = 2; return x + y
_, res = tracefunc(one_liner)[0]
show_res(res)
- 'x = 1' 1
{'x': [('int', '1')]}
- 'y = 2' 1
{'y': [('int', '2')]}
- 'return x + y' 1
{'x': [('int', '1')], 'y': [('int', '2')]}
Targeted tracing and call stacks
You can trace a specific target function and see the call stack for each
call. Stack paths are shown relative to fn’s directory when possible.
def target(x):
return x + 1
def another(x): return target(x)
def wrapper(n):
out = []
for i in range(n): out.append(target(i))
out.append(another(10))
return out
for stack, res in tracefunc(wrapper, 2, target_func=target):
print(stack)
show_res(res)
wrapper (1041865549.py:8)
target (1041865549.py:1)
- 'return x + 1' 1
{'x': [('int', '0')]}
wrapper (1041865549.py:8)
target (1041865549.py:1)
- 'return x + 1' 1
{'x': [('int', '1')]}
wrapper (1041865549.py:9)
another (1041865549.py:4)
target (1041865549.py:1)
- 'return x + 1' 1
{'x': [('int', '10')]}
Nested function
Nested definitions appear as statements, and their bodies are traced when called.
def outer(x):
def inner(y):
return x + y
return inner(5)
_, res = tracefunc(outer, 10)[0]
show_res(res)
- 'def inner(y):' 1
{'inner': [('function', '<function outer.<locals>.inner>')]}
- 'return x + y' 1
{'x': [('int', '10')], 'y': [('int', '5')]}
- 'return inner(5)' 1
{'inner': [('function', '<function outer.<locals>.inner>')]}
Metadata
Release files for tracefunc 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tracefunc-0.0.7.tar.gz | 20.2 kB | Without provenance | Details for tracefunc-0.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tracefunc-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for tracefunc-0.0.7-py3-none-any.whl |
Total release size: 35.3 kB
Release files / tracefunc-0.0.7.tar.gz
| Download URL | tracefunc-0.0.7.tar.gz |
|---|---|
| Size | 20.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e22dd04289a402ad1ff198d340b27ee42b8b63aec9a8fadc08025912bd4c006c
|
|
BLAKE2b-256 checksum How to use checksums |
cd06eed06b426935e0beb96ed59e994a10abe3e9489e8508054d2c492be56a37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Release files / tracefunc-0.0.7-py3-none-any.whl
| Download URL | tracefunc-0.0.7-py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bece00dc1b58336065b5b75872d151b97e89c85f80e20af341fec49a7c63d5bd
|
|
BLAKE2b-256 checksum How to use checksums |
d0714eb11e956bea3598e0215165834cbf3897f92fc16b5be667bab79f65a659
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Security for tracefunc 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.