[Python] Is there a way to instrument python code like this to run my tracing function on it?...

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    So for example if I have this python code

    def adder(x,y):
    return x + y


    So I am tracing the values of variables for python source code and I want to run the sys.settrace using my trace function on this code. I am using the tracing like this

    def track_assignments(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
    sys.settrace(trace_calls)
    result = func(*args, **kwargs)
    sys.settrace(None)
    return result
    return wrapper

    @track_assignments
    def main():
    actual code


    Is there a way I can instrument the actual code inside main for tracing?

    Edit : I already know that I can execute the function using eval as suggested here How do I execute a string containing Python code in Python? but due to the security risks involving eval and exec I don't want to execute it using those as the source code is user submitted.

    Continue reading...

Compartilhe esta Página