Skip to main content

AST-level execution tracing via sys.settrace

Project description

tracefunc

tracefunc takes a function and its arguments, executes it, and returns a dictionary mapping each source code snippet to a tuple of (hit_count, variables_dict). This lets you see exactly how many times each statement ran and what values the variables held at each execution.

Install

pip install tracefunc

Requires Python 3.11+ (uses PEP 657 source positions).

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)
result = tracefunc(demo, 3)
show_res(result)
- '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

show_res(tracefunc(one_liner))
- 'x = 1' 1
{'x': [('int', '1')]}
- 'y = 2' 1
{'y': [('int', '2')]}
- 'return x + y' 1
{'x': [('int', '1')], 'y': [('int', '2')]}

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)

show_res(tracefunc(outer, 10))
- '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>')]}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tracefunc-0.0.2.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tracefunc-0.0.2-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file tracefunc-0.0.2.tar.gz.

File metadata

  • Download URL: tracefunc-0.0.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for tracefunc-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f204df891eb7c8ad8f01692e3d1f513ba00b371e393160b5a7e199adb1de96a1
MD5 0b7e8c8b65ff062f7b51c6c29b23427b
BLAKE2b-256 e79279a8291b8554650741603979cf0c8cbf1897f9e1962873c057f5150ba25e

See more details on using hashes here.

File details

Details for the file tracefunc-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tracefunc-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for tracefunc-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 411e43ffbf061be07bd571564794c7c47d65e531bb97e0c30146b853f7114f88
MD5 865eddc9dadc2118af35994796d53a1f
BLAKE2b-256 164b4de4e2445f8b4c495cd0f3d87e37fafa565b210cb0abb9099f2fd752ed9a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page