From 95ba0a2b3de65919491a3e4343c39bfe468d86dd 2008-07-02 05:18:55 From: Barry Wark Date: 2008-07-02 05:18:55 Subject: [PATCH] using wrapped_execute in ThreadedEngineService to add information to exception (like executeAndRaise) --- diff --git a/IPython/kernel/engineservice.py b/IPython/kernel/engineservice.py index c8ec1fc..0529931 100644 --- a/IPython/kernel/engineservice.py +++ b/IPython/kernel/engineservice.py @@ -395,6 +395,7 @@ class EngineService(object, service.Service): return d + # The IEngine methods. See the interface for documentation. def execute(self, lines): @@ -862,6 +863,30 @@ class ThreadedEngineService(EngineService): def __init__(self, shellClass=Interpreter, mpi=None): EngineService.__init__(self, shellClass, mpi) + def wrapped_execute(self, msg, lines): + """Wrap self.shell.execute to add extra information to tracebacks""" + + try: + result = self.shell.execute(lines) + except Exception,e: + # This gives the following: + # et=exception class + # ev=exception class instance + # tb=traceback object + et,ev,tb = sys.exc_info() + # This call adds attributes to the exception value + et,ev,tb = self.shell.formatTraceback(et,ev,tb,msg) + # Add another attribute + + # Create a new exception with the new attributes + e = et(ev._ipython_traceback_text) + e._ipython_engine_info = msg + + # Re-raise + raise e + + return result + def execute(self, lines): # Only import this if we are going to use this class @@ -871,6 +896,6 @@ class ThreadedEngineService(EngineService): 'method':'execute', 'args':[lines]} - d = threads.deferToThread(self.shell.execute, lines) + d = threads.deferToThread(self.wrapped_execute, msg, lines) d.addCallback(self.addIDToResult) return d