Skip to main content

A wrapper for sys.settrace to enable programs to have more than one trace active.

Project description

Tracewrapper

Wrapper for Pythons sys.settrace

Allows you to run multiple traces at once and also allows you to queue up a class instead of the default show_trace() function. Also allows you to filter events, modules and functions.

To Install:

pip3 install tracewrapper

or

python -m pip install tracewrapper

Example:

from tracewrapper import tracewrapper
import opcode

#Show_Trace function just like you'd feed to sys.settrace()
def show_trace1(frame, event, arg):
    code = frame.f_code
    offset = frame.f_lasti
    print(f"Trace1| {event:10} | {str(arg):>4} |", end=' ')
    print(f"{frame.f_lineno:>4} | {frame.f_lasti:>6} |", end=' ')
    print(f"{opcode.opname[code.co_code[offset]]:<18} | {str(frame.f_locals):<35} |")
    return show_trace1

#Another Show_Trace function just like you'd feed to sys.settrace()
def show_trace2(frame, event, arg):
    code = frame.f_code
    offset = frame.f_lasti
    print(f"Trace2| {event:10} | {str(arg):>4} |", end=' ')
    print(f"{frame.f_lineno:>4} | {frame.f_lasti:>6} |", end=' ')
    print(f"{opcode.opname[code.co_code[offset]]:<18} | {str(frame.f_locals):<35} |")
    return show_trace2

#Show_Trace as a class
class TClass(tracewrapper.TracerClass):
    def trace(self, frame, event, arg):
        code = frame.f_code
        offset = frame.f_lasti
        print(f"Trace3| {event:10} | {str(arg):>4} |", end=' ')
        print(f"{frame.f_lineno:>4} | {frame.f_lasti:>6} |", end=' ')
        print(f"{opcode.opname[code.co_code[offset]]:<18} | {str(frame.f_locals):<35} |")
        return self

#Demo fibanaci function to trace
def fib(n):
    i, f1, f2 = 1, 1, 1
    while i < n:
        f1, f2 = f2, f1 + f2
        i += 1
    return f1

#Instanciate the tracewrapper() class
tracer=tracewrapper.tracewrapper()

#Add our two demo show_trace functions
tracer.add(show_trace1)
tracer.add(show_trace2)

#Instanciate TClass() our inherited TracerClass()
tc=TClass()

#Add TracerClass's trace method to Tracer
tracer.add(tc.trace)

#Activate it
tc.start()

#Start tracing
tracer.start()

#Call our demo function
fib(6)

#Stop Tracing
tracer.stop()

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

tracewrapper-0.0.8.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

tracewrapper-0.0.8-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file tracewrapper-0.0.8.tar.gz.

File metadata

  • Download URL: tracewrapper-0.0.8.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for tracewrapper-0.0.8.tar.gz
Algorithm Hash digest
SHA256 d0789b513a3f1ca572f09e16158485cda6897e63de796294c02addaa194194ca
MD5 4f93743366264425d79ea04625c4defc
BLAKE2b-256 12813d4ab8556a0fefb1553723dfe9f59bbf631d7be2cc37bce082f670c9cb78

See more details on using hashes here.

File details

Details for the file tracewrapper-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: tracewrapper-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for tracewrapper-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6b0992a20b2632a3e4a612304c61204ff48fb050815bf996cca353490514954d
MD5 7791f2f3623bfa3b835c6be539c9af85
BLAKE2b-256 a5158584735e98fd8a8822133ec08e97e4734752088f7f2ab7e2cced39a971bc

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