Skip to content

YuYi-09/pytrace-inject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pytrace-inject: Runtime AOP injection for Python.

A lightweight framework that lets you intercept and modify local variables at any execution point of a target function, using sys.settrace.

Quick Start

import random
from pytrace_inject import At, inject, start_trace, end_trace

@inject(At(random.randrange).atcall())
def modify(start, end):
    start, end = 42, 43
    return {'start': start, 'end': end}

start_trace()
print(random.randrange(100, 999))  # Output: 42
end_trace()

Key Concepts

  • At — Where to inject (call time / return time / specific line / custom condition).
  • Tracer — The active injection, can be .remove()'d at any time.
  • Callback — Your function that receives current local variables and returns a dict of replacements (or None).

License

MIT — see LICENSE file.