2009-09-23 8 views
11

Je IronPython hébergé au sein de ma demande, chaque fois que je surprends une exception levée d'un script, je reçois du charabia inutile comme ceci:Obtenir des informations de retraçage exceptions 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) 

Ce que je voudrais faire à la place est le traceback python. Est-il possible d'obtenir cette information?

Répondre

15

Si vous avez juste besoin d'une version de texte que vous pouvez faire:

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

Si vous avez vraiment besoin de l'objet le suivi retour Python Je suggère:

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

Enregistrer que quelque part utile et puis quand vous besoin de l'appeler:

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

Cela fonctionnera tant que vous attrapez l'exception.

Un peu moins soutenu, mais plus facile de le faire serait:

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

Mais vous avez besoin d'un CodeContext pour ce faire. Dans 2.0, vous pouvez obtenir un CodeContext par:

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