From fbf7c5fec2e6f570afe682e24989dd1441cb94e4 2008-09-05 22:36:12 From: gvaroquaux Date: 2008-09-05 22:36:12 Subject: [PATCH] Capture errors in user's input lines and raises meaningfull exceptions. --- diff --git a/IPython/frontend/linefrontendbase.py b/IPython/frontend/linefrontendbase.py index 8c8872e..494fc71 100644 --- a/IPython/frontend/linefrontendbase.py +++ b/IPython/frontend/linefrontendbase.py @@ -21,6 +21,7 @@ import re import IPython import sys import codeop +import traceback from frontendbase import FrontEndBase from IPython.kernel.core.interpreter import Interpreter @@ -147,10 +148,18 @@ class LineFrontEndBase(FrontEndBase): and not re.findall(r"\n[\t ]*\n[\t ]*$", string)): return False else: - # Add line returns here, to make sure that the statement is - # complete. - return codeop.compile_command(string.rstrip() + '\n\n', - "", "exec") + self.capture_output() + try: + # Add line returns here, to make sure that the statement is + # complete. + is_complete = codeop.compile_command(string.rstrip() + '\n\n', + "", "exec") + self.release_output() + except Exception, e: + # XXX: Hack: return True so that the + # code gets executed and the error captured. + is_complete = True + return is_complete def write(self, string, refresh=True): diff --git a/IPython/frontend/wx/wx_frontend.py b/IPython/frontend/wx/wx_frontend.py index a6cbe4d..57255bc 100644 --- a/IPython/frontend/wx/wx_frontend.py +++ b/IPython/frontend/wx/wx_frontend.py @@ -324,6 +324,17 @@ class WxController(ConsoleWidget, PrefilterFrontEnd): #-------------------------------------------------------------------------- + # FrontEndBase interface + #-------------------------------------------------------------------------- + + def render_error(self, e): + start_line = self.GetCurrentLine() + self.write('\n' + e + '\n') + for i in range(start_line, self.GetCurrentLine()): + self._markers[i] = self.MarkerAdd(i, _ERROR_MARKER) + + + #-------------------------------------------------------------------------- # ConsoleWidget interface #--------------------------------------------------------------------------