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

问题描述 投票:1回答:1

如果我执行此代码:

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

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

Traceback (most recent call last):
Exception: hello generator

为什么?我怎样才能使它工作?

编辑:测试后,似乎问题只出现在Unity ...

python unity3d ironpython
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.