From 7cbab5de7efe9cc716aa341fdb0bc217f8238f1b 2008-07-01 06:24:21 From: Barry Wark Date: 2008-07-01 06:24:21 Subject: [PATCH] improved error rendering for cocoa_frontend --- diff --git a/IPython/frontend/cocoa/cocoa_frontend.py b/IPython/frontend/cocoa/cocoa_frontend.py index e21da15..b366905 100644 --- a/IPython/frontend/cocoa/cocoa_frontend.py +++ b/IPython/frontend/cocoa/cocoa_frontend.py @@ -24,6 +24,7 @@ __docformat__ = "restructuredtext en" # Imports #----------------------------------------------------------------------------- +import sys import objc import uuid @@ -56,12 +57,29 @@ from twisted.python.failure import Failure class AutoreleasePoolWrappedThreadedEngineService(ThreadedEngineService): """Wrap all blocks in an NSAutoreleasePool""" - def wrapped_execute(self, lines): + def wrapped_execute(self, msg, lines): """wrapped_execute""" - - p = NSAutoreleasePool.alloc().init() - result = self.shell.execute(lines) - p.drain() + try: + p = NSAutoreleasePool.alloc().init() + 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 + finally: + p.drain() return result @@ -73,7 +91,7 @@ class AutoreleasePoolWrappedThreadedEngineService(ThreadedEngineService): 'method':'execute', 'args':[lines]} - d = threads.deferToThread(self.wrapped_execute, lines) + d = threads.deferToThread(self.wrapped_execute, msg, lines) d.addCallback(self.addIDToResult) return d @@ -241,7 +259,7 @@ class IPythonCocoaController(NSObject, FrontEndBase): #print inputRange,self.current_block_range() self.insert_text('\n' + - self.output_prompt(result) + + self.output_prompt(number=result['number']) + result.get('display',{}).get('pprint','') + '\n\n', textRange=NSMakeRange(inputRange.location+inputRange.length, @@ -250,7 +268,11 @@ class IPythonCocoaController(NSObject, FrontEndBase): def render_error(self, failure): - self.insert_text('\n\n'+str(failure)+'\n\n') + self.insert_text('\n' + + self.output_prompt() + + '\n' + + failure.getErrorMessage() + + '\n\n') self.start_new_block() return failure diff --git a/IPython/frontend/frontendbase.py b/IPython/frontend/frontendbase.py index 45c5684..9a3b10e 100644 --- a/IPython/frontend/frontendbase.py +++ b/IPython/frontend/frontendbase.py @@ -106,13 +106,13 @@ class IFrontEnd(zi.Interface): pass - def input_prompt(number=None): + def input_prompt(number=''): """Returns the input prompt by subsituting into self.input_prompt_template """ pass - def output_prompt(number=None): + def output_prompt(number=''): """Returns the output prompt by subsituting into self.output_prompt_template """ @@ -180,7 +180,7 @@ class FrontEndBase(object): self.history = history - def input_prompt(self, number=None): + def input_prompt(self, number=''): """Returns the current input prompt It would be great to use ipython1.core.prompts.Prompt1 here @@ -193,7 +193,7 @@ class FrontEndBase(object): return self.continuation_prompt_template.safe_substitute() - def output_prompt(self, number=None): + def output_prompt(self, number=''): """Returns the output prompt for result""" return self.output_prompt_template.safe_substitute({'number':number})