Getting traceback information from IronPython exceptions

泄露秘密 提交于 2019-12-05 00:28:28

If you just need a text version you can do:

engine.GetService<ExceptionOperations>().FormatException(exception);

If you really need the Python track back object I would suggest:

Func<PythonTuple> exc_info = engine.Operations.GetMember<Func<PythonTuple>>(engine.GetSysModule(), "exc_info");

Save that somewhere useful and then when you need to invoke it:

TraceBack tb = (TraceBack)exc_info()[2];

That'll work as long as you're catching the exception.

A slightly less supported but easier way to do this would be:

TraceBack tb = PythonOps.GetExceptionInfoLocal(context, exception)[2];

But you need a CodeContext to do this. In 2.0 you can get a CodeContext by:

new CodeContext(new PythonDictionary(), HostingHelpers.GetLanguageContext(engine));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!