如果在范围内执行,Ironpython会丢失堆栈帧

时间:2018-01-01 13:19:20

标签: python unity3d ironpython

如果我执行此代码:

app2

我得到了堆栈跟踪:

    var engine = Python.CreateEngine();
    ScriptScope sc = engine.CreateScope ();
    var ss = engine.CreateScriptSourceFromString ("\ndef f():\n    raise Exception('hello generator')\n    yield 42\n\ndef g():\n    f().next()\n\ng()\n", "a.py");

    try {
        ss.Execute();
    } catch(Exception e) {
        UnityEngine.Debug.Log(engine.GetService<ExceptionOperations>().FormatException(e));
    }

但是当我在范围内执行它时:

Traceback (most recent call last):
  File "a.py", line 7, in g
  File "a.py", line 3, in f
Exception: hello generator

我得到了缺少堆栈跟踪信息的追溯......

    var engine = Python.CreateEngine();
    ScriptScope sc = engine.CreateScope ();
    var ss = engine.CreateScriptSourceFromString ("\ndef f():\n    raise Exception('hello generator')\n    yield 42\n\ndef g():\n    f().next()\n\ng()\n", "a.py");

    try {
        ss.Execute(sc);
    } catch(Exception e) {
        UnityEngine.Debug.Log(engine.GetService<ExceptionOperations>().FormatException(e));
    }

为什么呢?我怎样才能使它发挥作用?

编辑:测试结束后,似乎问题只存在于Unity ......

1 个答案:

答案 0 :(得分:0)

显然,Unity,Mono 2和IronPython 2.6.2已经破坏了整个异常处理。我在这里发帖,以防有人正在寻找适合我的工作解决方案:

        List<DynamicStackFrame> frames = ExceptionHelpers.AssociateDynamicStackFrames (e);
        if (frames == null)
            frames = new List<DynamicStackFrame> ();

        String text = "Traceback (most recent call first):\n";
        foreach (DynamicStackFrame frame in frames) {
            text += " File \"" + frame.GetFileName () + "\", line " + frame.GetFileLineNumber () + ", in " + frame.GetMethodName () + "\n";
        }
        text += "Exception: " + e.Message;
        Debug.Log (text);
        frames.Clear ();
        ExceptionHelpers.ClearDynamicStackFrames (e);