diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index c1530d2..d4f4369 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -108,8 +108,33 @@ class IndentationErrorTest(unittest.TestCase): with tt.AssertPrints("zoon()", suppress=False): ip.magic('run %s' % fname) +se_file_1 = """1 +2 +7/ +""" + +se_file_2 = """7/ +""" + class SyntaxErrorTest(unittest.TestCase): def test_syntaxerror_without_lineno(self): with tt.AssertNotPrints("TypeError"): with tt.AssertPrints("line unknown"): ip.run_cell("raise SyntaxError()") + + def test_changing_py_file(self): + with TemporaryDirectory() as td: + fname = os.path.join(td, "foo.py") + with open(fname, 'w') as f: + f.write(se_file_1) + + with tt.AssertPrints(["7/", "SyntaxError"]): + ip.magic("run " + fname) + + # Modify the file + with open(fname, 'w') as f: + f.write(se_file_2) + + # The SyntaxError should point to the correct line + with tt.AssertPrints(["7/", "SyntaxError"]): + ip.magic("run " + fname) diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index f397c11..a8fc8d5 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -346,6 +346,8 @@ class AssertPrints(object): """ def __init__(self, s, channel='stdout', suppress=True): self.s = s + if isinstance(self.s, str): + self.s = [self.s] self.channel = channel self.suppress = suppress @@ -359,7 +361,8 @@ class AssertPrints(object): self.tee.flush() setattr(sys, self.channel, self.orig_stream) printed = self.buffer.getvalue() - assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed) + for s in self.s: + assert s in printed, notprinted_msg.format(s, self.channel, printed) return False printed_msg = """Found {0!r} in printed output (on {1}): @@ -376,7 +379,8 @@ class AssertNotPrints(AssertPrints): self.tee.flush() setattr(sys, self.channel, self.orig_stream) printed = self.buffer.getvalue() - assert self.s not in printed, printed_msg.format(self.s, self.channel, printed) + for s in self.s: + assert s not in printed, printed_msg.format(s, self.channel, printed) return False @contextmanager