From 8deeb138245daee0d74ed27674ca5ec832deff6c 2014-02-21 18:05:26 From: MinRK Date: 2014-02-21 18:05:26 Subject: [PATCH] Backport PR #4372: Don't assume that SyntaxTB is always called with a SyntaxError closes #5184 --- diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index 1d7ae51..17a948b 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -138,3 +138,12 @@ class SyntaxErrorTest(unittest.TestCase): # The SyntaxError should point to the correct line with tt.AssertPrints(["7/", "SyntaxError"]): ip.magic("run " + fname) + + def test_non_syntaxerror(self): + # SyntaxTB may be called with an error other than a SyntaxError + # See e.g. gh-4361 + try: + raise ValueError('QWERTY') + except ValueError: + with tt.AssertPrints('QWERTY'): + ip.showsyntaxerror() diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index 846d1a1..215f73d 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -1198,7 +1198,8 @@ class SyntaxTB(ListTB): # If the source file has been edited, the line in the syntax error can # be wrong (retrieved from an outdated cache). This replaces it with # the current value. - if isinstance(value.filename, py3compat.string_types) \ + if isinstance(value, SyntaxError) \ + and isinstance(value.filename, py3compat.string_types) \ and isinstance(value.lineno, int): linecache.checkcache(value.filename) newtext = ulinecache.getline(value.filename, value.lineno) diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index 2761213..2833587 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -363,6 +363,9 @@ class AssertPrints(object): setattr(sys, self.channel, self.buffer if self.suppress else self.tee) def __exit__(self, etype, value, traceback): + if value is not None: + # If an error was raised, don't check anything else + return False self.tee.flush() setattr(sys, self.channel, self.orig_stream) printed = self.buffer.getvalue() @@ -381,6 +384,9 @@ class AssertNotPrints(AssertPrints): Counterpart of AssertPrints""" def __exit__(self, etype, value, traceback): + if value is not None: + # If an error was raised, don't check anything else + return False self.tee.flush() setattr(sys, self.channel, self.orig_stream) printed = self.buffer.getvalue()