Skip to content

exec_and_record_function_calls

exec_and_record_function_calls(code, globals_)

Execute the code while recording all the function calls which originate from the code object.

While recording function calls, we use sys.settrace() with LineaPy's tracer to extract relevant information during the runtime of the user's code's function, and reset the tracer after the user function has completed execution to prevent unnecessary logging. However, to ensure LineaPy works correctly while debugging using VSCode, we first capture any existing tracers using sys.gettrace(), perform our analysis using the LineaPy tracer, and reset the existing tracer using sys.settrace()

Source code in lineapy/system_tracing/exec_and_record_function_calls.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def exec_and_record_function_calls(
    code: CodeType, globals_: Dict[str, object]
) -> TraceFunc:
    """
    Execute the code while recording all the function calls which originate from the code object.

    While recording function calls, we use sys.settrace() with LineaPy's tracer to extract relevant
    information during the runtime of the user's code's function, and reset the tracer after the user
    function has completed execution to prevent unnecessary logging.
    However, to ensure LineaPy works correctly while debugging using VSCode, we first capture any
    existing tracers using sys.gettrace(), perform our analysis using the LineaPy tracer, and reset
    the existing tracer using sys.settrace()
    """
    logger.debug("Executing code")
    original_trace = gettrace()
    trace_func = TraceFunc(code)
    try:
        settrace(trace_func)
        exec(code, globals_)
    # Always stop tracing even if exception raised
    finally:
        settrace(original_trace)
    return trace_func

Was this helpful?

Help us improve docs with your feedback!