rr0711
rr0711

Reputation: 17

How to retrieve current executing function from python code?

Using the python script engine set up in C# using IronPython, is there a way to retrieve the current executing function from the python code?

E. g. in my code, the setup python script engine is stored in variable pse. Is there a call similar to pse.GetVariables() that returns the current executing python function?

I would like to be able to pull this information so I could use it in a check within a test, for example.

Upvotes: 0

Views: 229

Answers (1)

dymanoid
dymanoid

Reputation: 15217

You can use the trace functionality for that. Set a trace callback that will be called for each line of the executing script. From that callback, you can get the function name that is currently executing.

pse.SetTrace(IronPythonTraceCallbaback);

And the callback definition:

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;
}

Upvotes: 1

Related Questions