diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index d4f4369..07c9ba5 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/testing/tools.py b/IPython/testing/tools.py index 52fdc58..c2fe99b 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -357,6 +357,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() @@ -375,6 +378,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()