如何从python代码中检索当前正在执行的函数?

2024-06-16 17:08:59 发布

您现在位置:Python中文网/ 问答频道 /正文

使用C中设置的python脚本引擎#使用IronPython,是否有方法从python代码中检索当前正在执行的函数?你知道吗

例如,在我的代码中,setuppython脚本引擎存储在变量pse中。是否有类似于pse.GetVariables()的调用返回当前正在执行的python函数?你知道吗

例如,我希望能够提取这些信息,以便在测试的检查中使用。你知道吗


Tags: 方法函数代码引擎脚本信息ironpythonpse
1条回答
网友
1楼 · 发布于 2024-06-16 17:08:59

你可以使用跟踪功能。设置将为执行脚本的每一行调用的跟踪回调。从该回调中,可以获得当前正在执行的函数名。你知道吗

pse.SetTrace(IronPythonTraceCallbaback);

以及回调定义:

static TracebackDelegate IronPythonTraceBack(TraceBackFrame frame, string result, object payload)
{
    if (frame != null && frame.f_code != null)
    {
         // Here you can access the executing function information
         FunctionCode functionCode = frame.f_code;
         string functionName = frame.f_code.co_name;
    }

    return IronPythonTraceBack;
}

相关问题 更多 >