Skip to main content

AST-level execution tracing via sys.monitoring

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.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)
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.3.tar.gz (17.0 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.3-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tracefunc-0.0.3.tar.gz
  • Upload date:
  • Size: 17.0 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.3.tar.gz
Algorithm Hash digest
SHA256 68e4861283c337eab42b155a0bdbc7a0e9a5f876557a23c6723a36446058588d
MD5 a6e5a35348b8afe04765a36ad2c8f6cf
BLAKE2b-256 2de26d8252bc72a70e60ee5c9374d9619d20a292860bf3dcc33207ee8fd41a18

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tracefunc-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.6 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 780b0ad59a09e4448566798f589ce192a9cda1c1550fd3d3a52fa2415a91a00e
MD5 2b582c9a1b357a484e26366faa7cb09e
BLAKE2b-256 81b948ec4f5a43bb4af33bfad9926a14eef00328ef73927714082311c7a45ebf

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