I am working on a project that uses the trace
module to trace the execution of code.
When I call trace.Trace(args).run(stuff)
, stuff
has to be either a code object or a string.
For now, I am building the function that I will be calling with calls to functools.partial
. This part works exactly as I need it to.
However, when I get to the tracing part, I am not able to execute the trace with a functools.partial
object. I can't use myFuncToolsPartialObject.func.func_name(args)
because I can't guarantee that the function will be imported into the local scope. I can get around this with a call to import, but would rather not, at the moment (let's call it a personal preference).
Is there a way by which I can turn the functools.partial
object into a code object for a proper call to trace?
I know I can ask for myFuncToolsPartialObject.func.func_code
, but I'm wouldn't know how to call it on the required arguments
It turns out that trace
has built-in support for just this use case. trace.Trace().runfunc(funcObject, *args)
does exactly this.
From the docs:
runfunc(func, *args, **kwds)
Call func with the given arguments under control of the Trace object with the current tracing parameters.