##// END OF EJS Templates
Backport PR #4372: Don't assume that SyntaxTB is always called with a SyntaxError...
MinRK -
Show More
@@ -138,3 +138,12 b' class SyntaxErrorTest(unittest.TestCase):'
138 # The SyntaxError should point to the correct line
138 # The SyntaxError should point to the correct line
139 with tt.AssertPrints(["7/", "SyntaxError"]):
139 with tt.AssertPrints(["7/", "SyntaxError"]):
140 ip.magic("run " + fname)
140 ip.magic("run " + fname)
141
142 def test_non_syntaxerror(self):
143 # SyntaxTB may be called with an error other than a SyntaxError
144 # See e.g. gh-4361
145 try:
146 raise ValueError('QWERTY')
147 except ValueError:
148 with tt.AssertPrints('QWERTY'):
149 ip.showsyntaxerror()
@@ -1198,7 +1198,8 b' class SyntaxTB(ListTB):'
1198 # If the source file has been edited, the line in the syntax error can
1198 # If the source file has been edited, the line in the syntax error can
1199 # be wrong (retrieved from an outdated cache). This replaces it with
1199 # be wrong (retrieved from an outdated cache). This replaces it with
1200 # the current value.
1200 # the current value.
1201 if isinstance(value.filename, py3compat.string_types) \
1201 if isinstance(value, SyntaxError) \
1202 and isinstance(value.filename, py3compat.string_types) \
1202 and isinstance(value.lineno, int):
1203 and isinstance(value.lineno, int):
1203 linecache.checkcache(value.filename)
1204 linecache.checkcache(value.filename)
1204 newtext = ulinecache.getline(value.filename, value.lineno)
1205 newtext = ulinecache.getline(value.filename, value.lineno)
@@ -363,6 +363,9 b' class AssertPrints(object):'
363 setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
363 setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
364
364
365 def __exit__(self, etype, value, traceback):
365 def __exit__(self, etype, value, traceback):
366 if value is not None:
367 # If an error was raised, don't check anything else
368 return False
366 self.tee.flush()
369 self.tee.flush()
367 setattr(sys, self.channel, self.orig_stream)
370 setattr(sys, self.channel, self.orig_stream)
368 printed = self.buffer.getvalue()
371 printed = self.buffer.getvalue()
@@ -381,6 +384,9 b' class AssertNotPrints(AssertPrints):'
381
384
382 Counterpart of AssertPrints"""
385 Counterpart of AssertPrints"""
383 def __exit__(self, etype, value, traceback):
386 def __exit__(self, etype, value, traceback):
387 if value is not None:
388 # If an error was raised, don't check anything else
389 return False
384 self.tee.flush()
390 self.tee.flush()
385 setattr(sys, self.channel, self.orig_stream)
391 setattr(sys, self.channel, self.orig_stream)
386 printed = self.buffer.getvalue()
392 printed = self.buffer.getvalue()
General Comments 0
You need to be logged in to leave comments. Login now