【问题标题】:Getting traceback information from IronPython exceptions从 IronPython 异常中获取回溯信息
【发布时间】:2009-09-23 10:48:15
【问题描述】:

我在我的应用程序中托管了 IronPython,每当我捕捉到脚本抛出的异常时,我都会收到这样无用的胡言乱语:

IronPython.NewTypes.System.Exception_1$1: Error occurred during conversion ---> Microsoft.Scripting.ArgumentTypeException: expected int, got DispMethod
   at _stub_$245##245(Closure , CallSite , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call1[T0,TRet](Func`3 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update1[T,T0,TRet](CallSite site, T0 arg0)
   at _stub_$227##227(Closure , CallSite , Object )
   at IronPython.Runtime.Converter.Convert(Object value, Type to)
   at IronPython.Runtime.Operations.ArrayOps.SetItem(Array data, Int32 index, Object value)
   at _stub_$244##244(Closure , CallSite , Object , Object , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call3[T0,T1,T2,TRet](Func`5 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update3[T,T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at ConvertToFgf$223##223(Closure , Object , Object , Object )
   at _stub_$192##192(Closure , CallSite , CodeContext , Object , Object , Object , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call5[T0,T1,T2,T3,T4,TRet](Func`7 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update5[T,T0,T1,T2,T3,T4,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
   at Convert$224##224(Closure , Object , Object )
   --- End of inner exception stack trace ---
   at Convert$224##224(Closure , Object , Object )
   at _stub_$42##42(Closure , CallSite , CodeContext , Object , Object , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call4[T0,T1,T2,T3,TRet](Func`6 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update4[T,T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at Run$225##225(Closure )

我想得到的是 python 回溯。有没有办法得到这些信息?

【问题讨论】:

    标签: exception ironpython traceback


    【解决方案1】:

    如果您只需要文本版本,您可以这样做:

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

    如果你真的需要 Python track back 对象,我会建议:

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

    将它保存在有用的地方,然后在需要时调用它:

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

    只要您发现异常,它就会起作用。

    一个稍微受支持但更简单的方法是:

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

    但是您需要一个 CodeContext 来执行此操作。在 2.0 中,您可以通过以下方式获取 CodeContext:

    new CodeContext(new PythonDictionary(), HostingHelpers.GetLanguageContext(engine));
    

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2016-04-21
      • 1970-01-01
      相关资源
      最近更新 更多