From de0ead4396b4f4d797a1f61356f830cee9f712d0 2012-01-04 21:03:43 From: Thomas Kluyver Date: 2012-01-04 21:03:43 Subject: [PATCH] Fix display of SyntaxError in Python 3. Closes gh-1225 --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 5e8c486..f3fa1ee 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1681,20 +1681,12 @@ class InteractiveShell(SingletonConfigurable, Magic): sys.last_traceback = last_traceback if filename and etype is SyntaxError: - # Work hard to stuff the correct filename in the exception try: - msg, (dummy_filename, lineno, offset, line) = value + value.filename = filename except: # Not the format we expect; leave it alone pass - else: - # Stuff in the right filename - try: - # Assume SyntaxError is a class exception - value = SyntaxError(msg, (filename, lineno, offset, line)) - except: - # If that failed, assume SyntaxError is a string - value = msg, (filename, lineno, offset, line) + stb = self.SyntaxTB.structured_traceback(etype, value, []) self._showtraceback(etype, value, stb) diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 7dbf914..bb11204 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -548,44 +548,40 @@ class ListTB(TBTools): have_filedata = False Colors = self.Colors list = [] - try: - stype = Colors.excName + etype.__name__ + Colors.Normal - except AttributeError: - stype = etype # String exceptions don't get special coloring + stype = Colors.excName + etype.__name__ + Colors.Normal if value is None: + # Not sure if this can still happen in Python 2.6 and above list.append( str(stype) + '\n') else: if etype is SyntaxError: - try: - msg, (filename, lineno, offset, line) = value - except: - have_filedata = False - else: - have_filedata = True - #print 'filename is',filename # dbg - if not filename: filename = "" - list.append('%s File %s"%s"%s, line %s%d%s\n' % \ - (Colors.normalEm, - Colors.filenameEm, filename, Colors.normalEm, - Colors.linenoEm, lineno, Colors.Normal )) - if line is not None: - i = 0 - while i < len(line) and line[i].isspace(): - i = i+1 - list.append('%s %s%s\n' % (Colors.line, - line.strip(), - Colors.Normal)) - if offset is not None: - s = ' ' - for c in line[i:offset-1]: - if c.isspace(): - s = s + c - else: - s = s + ' ' - list.append('%s%s^%s\n' % (Colors.caret, s, - Colors.Normal) ) - value = msg - s = self._some_str(value) + have_filedata = True + #print 'filename is',filename # dbg + if not value.filename: value.filename = "" + list.append('%s File %s"%s"%s, line %s%d%s\n' % \ + (Colors.normalEm, + Colors.filenameEm, value.filename, Colors.normalEm, + Colors.linenoEm, value.lineno, Colors.Normal )) + if value.text is not None: + i = 0 + while i < len(value.text) and value.text[i].isspace(): + i = i+1 + list.append('%s %s%s\n' % (Colors.line, + value.text.strip(), + Colors.Normal)) + if value.offset is not None: + s = ' ' + for c in value.text[i:value.offset-1]: + if c.isspace(): + s = s + c + else: + s = s + ' ' + list.append('%s%s^%s\n' % (Colors.caret, s, + Colors.Normal) ) + + try: + s = value.msg + except Exception: + s = self._some_str(value) if s: list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, Colors.Normal, s)) @@ -596,7 +592,7 @@ class ListTB(TBTools): if have_filedata: ipinst = ipapi.get() if ipinst is not None: - ipinst.hooks.synchronize_with_editor(filename, lineno, 0) + ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) return list diff --git a/IPython/lib/tests/test_irunner.py b/IPython/lib/tests/test_irunner.py index b49bc86..bdbfd88 100644 --- a/IPython/lib/tests/test_irunner.py +++ b/IPython/lib/tests/test_irunner.py @@ -13,7 +13,6 @@ import unittest # IPython imports from IPython.lib import irunner -from IPython.testing.decorators import known_failure_py3 from IPython.utils.py3compat import doctest_refactor_print # Testing code begins @@ -58,8 +57,6 @@ class RunnerTestCase(unittest.TestCase): self.assert_(mismatch==0,'Number of mismatched lines: %s' % mismatch) - # The SyntaxError appears differently in Python 3, for some reason. - @known_failure_py3 def testIPython(self): """Test the IPython runner.""" source = doctest_refactor_print("""