gh-122546: use same filename for different exceptions in new repl (#123217)

* gh-122546: use same filename for different exceptions in new repl

* +1
This commit is contained in:
Sergey B Kirpichev 2024-08-22 14:55:30 +03:00 committed by GitHub
parent 427b106162
commit 3d7b1a526d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 10 deletions

View file

@ -162,7 +162,7 @@ class InteractiveColoredConsole(code.InteractiveConsole):
self.can_colorize = _colorize.can_colorize() self.can_colorize = _colorize.can_colorize()
def showsyntaxerror(self, filename=None, **kwargs): def showsyntaxerror(self, filename=None, **kwargs):
super().showsyntaxerror(**kwargs) super().showsyntaxerror(filename=filename, **kwargs)
def _excepthook(self, typ, value, tb): def _excepthook(self, typ, value, tb):
import traceback import traceback

View file

@ -109,15 +109,7 @@ class InteractiveInterpreter:
try: try:
typ, value, tb = sys.exc_info() typ, value, tb = sys.exc_info()
if filename and typ is SyntaxError: if filename and typ is SyntaxError:
# Work hard to stuff the correct filename in the exception value.filename = filename
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))
source = kwargs.pop('source', "") source = kwargs.pop('source', "")
self._showtraceback(typ, value, None, source) self._showtraceback(typ, value, None, source)
finally: finally:

View file

@ -1100,6 +1100,16 @@ class TestMain(TestCase):
self.assertIn("spam", output) self.assertIn("spam", output)
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0) 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)
@force_not_colorized @force_not_colorized
def test_proper_tracebacklimit(self): def test_proper_tracebacklimit(self):
env = os.environ.copy() env = os.environ.copy()

View file

@ -0,0 +1,2 @@
Consistently use same file name for different exceptions in the new repl.
Patch by Sergey B Kirpichev.