From 1197f369fc4a5c5e052008d963eadfa4fffcf8ee 2008-06-16 05:42:58 From: Barry Wark Date: 2008-06-16 05:42:58 Subject: [PATCH] added blockID for failures (special case) --- diff --git a/IPython/frontend/cocoa/cocoa_frontend.py b/IPython/frontend/cocoa/cocoa_frontend.py index 576680b..77df8ba 100644 --- a/IPython/frontend/cocoa/cocoa_frontend.py +++ b/IPython/frontend/cocoa/cocoa_frontend.py @@ -40,6 +40,7 @@ from IPython.kernel.engineservice import EngineService, ThreadedEngineService from IPython.frontend.frontendbase import FrontEndBase from twisted.internet.threads import blockingCallFromThread +from twisted.python.failure import Failure #------------------------------------------------------------------------------- # Classes to implement the Cocoa frontend @@ -305,7 +306,12 @@ class IPythonCocoaController(NSObject, FrontEndBase): return block[-1] def update_cell_prompt(self, result): - blockID = result['blockID'] + if(isinstance(result, Failure)): + blockID = result.blockID + else: + blockID = result['blockID'] + + self.insert_text(self.input_prompt(result=result), textRange=NSMakeRange(self.blockRanges[blockID].location,0), scrollToVisible=False diff --git a/IPython/frontend/frontendbase.py b/IPython/frontend/frontendbase.py index 9ff9c29..577bc97 100644 --- a/IPython/frontend/frontendbase.py +++ b/IPython/frontend/frontendbase.py @@ -234,18 +234,23 @@ class FrontEndBase(object): blockID = uuid.uuid4() #random UUID d = self.engine.execute(block) - d.addCallback(self._add_block_id, blockID) d.addCallback(self._add_history, block=block) - d.addCallback(self.update_cell_prompt) + d.addBoth(self._add_block_id, blockID) + d.addBoth(self.update_cell_prompt) d.addCallbacks(self.render_result, errback=self.render_error) return d def _add_block_id(self, result, blockID): - """Add the blockID to result""" + """Add the blockID to result or failure. Unfortunatley, we have to treat failures + differently than result dicts + """ - result['blockID'] = blockID + if(isinstance(result, Failure)): + result.blockID = blockID + else: + result['blockID'] = blockID return result @@ -287,7 +292,17 @@ class FrontEndBase(object): def update_cell_prompt(self, result): """Subclass may override to update the input prompt for a block. Since this method will be called as a twisted.internet.defer.Deferred's callback, - implementations should return result when finished.""" + implementations should return result when finished. + + NP: result is a failure if the execute returned a failre. To get the blockID, you should + do something like:: + if(isinstance(result, twisted.python.failure.Failure)): + blockID = result.blockID + else: + blockID = result['blockID'] + + + """ return result diff --git a/IPython/frontend/tests/test_frontendbase.py b/IPython/frontend/tests/test_frontendbase.py index be099e6..d3c265a 100644 --- a/IPython/frontend/tests/test_frontendbase.py +++ b/IPython/frontend/tests/test_frontendbase.py @@ -80,10 +80,20 @@ class TestFrontendBase(unittest.TestCase): d.addCallback(self.checkBlockID, expected='TEST_ID') + def test_blockID_added_to_failure(self): + block = "raise Exception()" + + d = self.fb.execute(block,blockID='TEST_ID') + d.addErrback(self.checkFailureID, expected='TEST_ID') + def checkBlockID(self, result, expected=""): assert(result['blockID'] == expected) + def checkFailureID(self, failure, expected=""): + assert(failure.blockID == expected) + + def test_callbacks_added_to_execute(self): """test that update_cell_prompt