[英]trace with functools.partial
I am working on a project that uses the trace
module to trace the execution of code. 我正在使用trace
模块跟踪代码执行的项目。
When I call trace.Trace(args).run(stuff)
, stuff
has to be either a code object or a string. 当我调用trace.Trace(args).run(stuff)
, stuff
必须是代码对象或字符串。
For now, I am building the function that I will be calling with calls to functools.partial
. 现在,我正在构建要通过调用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. 但是,当我到达跟踪部分时,我无法使用functools.partial
对象执行跟踪。 I can't use myFuncToolsPartialObject.func.func_name(args)
because I can't guarantee that the function will be imported into the local scope. 我不能使用myFuncToolsPartialObject.func.func_name(args)
因为我不能保证该函数将被导入本地范围。 I can get around this with a call to import, but would rather not, at the moment (let's call it a personal preference). 我可以通过调用import来解决此问题,但目前不愿意(我们称其为个人喜好)。
Is there a way by which I can turn the functools.partial
object into a code object for a proper call to trace? 有没有一种方法可以将functools.partial
对象转换为代码对象以进行适当的跟踪?
I know I can ask for myFuncToolsPartialObject.func.func_code
, but I'm wouldn't know how to call it on the required arguments 我知道我可以要求myFuncToolsPartialObject.func.func_code
,但是我不知道如何在必需的参数上调用它
It turns out that trace
has built-in support for just this use case. 事实证明, trace
仅对此用例具有内置支持。 trace.Trace().runfunc(funcObject, *args)
does exactly this. trace.Trace().runfunc(funcObject, *args)
正是这样做的。
runfunc(func, *args, **kwds)
Call func with the given arguments under control of the Trace object with the current tracing parameters.
这样行吗?
trace.Trace().run((lambda :partial_stuff(args)).func_code)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.