IronPython:如何访问追溯局部变量

时间:2009-10-09 22:42:06

标签: c# debugging ironpython

private ScriptEngine pe;
private ScriptScope scope;

private void button1_Click(object sender, EventArgs e)
{
    pe = Python.CreateEngine();
    scope = pe.CreateScope();

    pe.Runtime.SetTrace(OnTraceback);
    pe.Execute(@"
def square(x):
  s = x*x
  return s

a = square(2) + (3)
print ""hello world "" + str(a)
", scope);
}

private TracebackDelegate OnTraceback(TraceBackFrame frame, string result, object payload)
{
    //How can I access the local variables of the current function?

    return OnTraceback;
}

让我再说一遍:我想获取函数的局部变量,当前python执行的位置(行frame.f_lineno周围的函数)。

1 个答案:

答案 0 :(得分:1)

您可以访问frame.f_locals,它可以让您获取/设置所有变量。