Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting traceback information from IronPython exceptions

I have IronPython hosted within my application, whenever I catch an exception thrown from a script, I get unhelpful gibberish like this:

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 )

What I would like to get instead is the python traceback. Is there anyway to get this information?

like image 340
jumpinjackie Avatar asked Sep 23 '09 10:09

jumpinjackie


People also ask

How do I print traceback of exception in Python?

Method 2: By using print_exception() method. This method prints exception information and stack trace entries from traceback object tb to file. Parameters: This method accepts the following parameters: if tb argument is not None, it prints a header Traceback (most recent call last):

What are common traceback errors?

Some of the common traceback errors are:NameError. IndexError. KeyError. TypeError.

What is traceback Print_exc ()?

traceback. print_exc(limit = None, file = None, chain = True) : This is a shorthand for print_exception(*sys. exc_info(), limit, file, chain). traceback. print_last(limit = None, file = None, chain = True) : It works only after an exception has reached an interactive prompt.


1 Answers

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));
like image 64
Dino Viehland Avatar answered Sep 29 '22 01:09

Dino Viehland