From 4826a7e23d008ded9f324ce4b087150f5f1a388b 2008-07-15 06:39:45 From: Gael Varoquaux Date: 2008-07-15 06:39:45 Subject: [PATCH] Make code execution more robust. --- diff --git a/IPython/frontend/linefrontendbase.py b/IPython/frontend/linefrontendbase.py index b45fb69..27dd070 100644 --- a/IPython/frontend/linefrontendbase.py +++ b/IPython/frontend/linefrontendbase.py @@ -101,21 +101,28 @@ class LineFrontEndBase(FrontEndBase): if raw_string is None: raw_string = string # Create a false result, in case there is an exception - result = dict(number=self.prompt_number) + self.last_result = dict(number=self.prompt_number) try: self.history.input_cache[-1] = raw_string result = self.shell.execute(python_string) + self.last_result = result self.render_result(result) - except Exception, e: + except: self.show_traceback() finally: - self.prompt_number += 1 - self.new_prompt(self.prompt % (result['number'] + 1)) - # Start a new empty history entry - self._add_history(None, '') - # The result contains useful information that can be used - # elsewhere. - self.last_result = result + self.after_execute() + + + def after_execute(self): + """ All the operations required after an execution to put the + terminal back in a shape where it is usable. + """ + self.prompt_number += 1 + self.new_prompt(self.prompt % (self.last_result['number'] + 1)) + # Start a new empty history entry + self._add_history(None, '') + # The result contains useful information that can be used + # elsewhere. def _on_enter(self): @@ -124,13 +131,15 @@ class LineFrontEndBase(FrontEndBase): """ current_buffer = self.get_current_edit_buffer() cleaned_buffer = self.prefilter_input(current_buffer) - if self.is_complete(cleaned_buffer): + if self.is_complete(cleaned_buffer + '\n'): + # The '\n' is important in case prefiltering empties the + # line, to get a new prompt. self.execute(cleaned_buffer, raw_string=current_buffer) else: if len(current_buffer.split('\n'))>1: - self.write('\n' + self._get_indent_string(current_buffer)) + self.write(self._get_indent_string(current_buffer)) else: - self.write('\n\t') + self.write('\t') #-------------------------------------------------------------------------- @@ -138,7 +147,7 @@ class LineFrontEndBase(FrontEndBase): #-------------------------------------------------------------------------- def _get_indent_string(self, string): - print >>sys.__stderr__, string.split('\n') + string = string.replace('\t', ' '*4) string = string.split('\n')[-1] indent_chars = len(string) - len(string.lstrip()) indent_string = '\t'*(indent_chars // 4) + \ diff --git a/IPython/frontend/wx/console_widget.py b/IPython/frontend/wx/console_widget.py index dafcbeb..e5e855d 100644 --- a/IPython/frontend/wx/console_widget.py +++ b/IPython/frontend/wx/console_widget.py @@ -343,6 +343,7 @@ class ConsoleWidget(editwindow.EditWindow): if event.KeyCode in (13, wx.WXK_NUMPAD_ENTER) and \ event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN): catched = True + self.write('\n') self._on_enter() elif event.KeyCode == wx.WXK_HOME: diff --git a/IPython/frontend/wx/wx_frontend.py b/IPython/frontend/wx/wx_frontend.py index 92573cf..6997aed 100644 --- a/IPython/frontend/wx/wx_frontend.py +++ b/IPython/frontend/wx/wx_frontend.py @@ -32,7 +32,7 @@ from IPython.frontend.prefilterfrontend import PrefilterFrontEnd class IPythonWxController(PrefilterFrontEnd, ConsoleWidget): output_prompt = \ - '\n\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02%i\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02' + '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02%i\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02' #-------------------------------------------------------------------------- # Public API