如果在范围内执行,Ironpython 会丢失堆栈帧
Ironpython missing stack frames if executed in scope
如果我执行这段代码:
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 中...
显然整个异常处理在 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);
如果我执行这段代码:
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 中...
显然整个异常处理在 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);