self.can_colorize = _colorize.can_colorize()
def showsyntaxerror(self, filename=None, **kwargs):
- super().showsyntaxerror(colorize=self.can_colorize, **kwargs)
+ super().showsyntaxerror(filename=filename, **kwargs)
def showtraceback(self):
super().showtraceback(colorize=self.can_colorize)
sys.last_value = value
sys.last_traceback = tb
if filename and type is SyntaxError:
- # Work hard to stuff the correct filename in the exception
- try:
- msg, (dummy_filename, lineno, offset, line) = value.args
- except ValueError:
- # Not the format we expect; leave it alone
- pass
- else:
- # Stuff in the right filename
- value = SyntaxError(msg, (filename, lineno, offset, line))
- sys.last_exc = sys.last_value = value
+ value.filename = filename
# Set the line of text that the exception refers to
source = kwargs.pop('source', '')
lines = source.splitlines()
self.assertIn("spam", output)
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0)
+ @force_not_colorized
+ def test_correct_filename_in_syntaxerrors(self):
+ env = os.environ.copy()
+ commands = "a b c\nexit()\n"
+ output, exit_code = self.run_repl(commands, env=env)
+ if "can't use pyrepl" in output:
+ self.skipTest("pyrepl not available")
+ self.assertIn("SyntaxError: invalid syntax", output)
+ self.assertIn("<python-input-0>", output)
+
def run_repl(
self,
repl_input: str | list[str],
--- /dev/null
+Consistently use same file name for different exceptions in the new repl.
+Patch by Sergey B Kirpichev.