From 4b6795b3999886afdc1dd5e8fe62ec755f6a816b Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 22 Aug 2024 11:29:37 +0300 Subject: [PATCH 1/2] gh-122546: use same filename for different exceptions in new repl --- Lib/_pyrepl/console.py | 2 +- Lib/code.py | 10 +--------- Lib/test/test_pyrepl/test_pyrepl.py | 8 ++++++++ .../2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst | 2 ++ 4 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index 330ebbd6185679..3e72a56807f6fb 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -162,7 +162,7 @@ def __init__( self.can_colorize = _colorize.can_colorize() def showsyntaxerror(self, filename=None, **kwargs): - super().showsyntaxerror(**kwargs) + super().showsyntaxerror(filename=filename, **kwargs) def _excepthook(self, typ, value, tb): import traceback diff --git a/Lib/code.py b/Lib/code.py index b1079824a75414..c559191d8a747b 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -109,15 +109,7 @@ def showsyntaxerror(self, filename=None, **kwargs): try: typ, value, tb = sys.exc_info() if filename and typ 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)) + value.filename = filename source = kwargs.pop('source', "") self._showtraceback(typ, value, None, source) finally: diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index e11ec74aa14058..57a1f2c286f877 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1100,6 +1100,14 @@ def test_not_wiping_history_file(self): 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) + self.assertIn("SyntaxError: invalid syntax", output) + self.assertIn("", output) + @force_not_colorized def test_proper_tracebacklimit(self): env = os.environ.copy() diff --git a/Misc/NEWS.d/next/Library/2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst b/Misc/NEWS.d/next/Library/2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst new file mode 100644 index 00000000000000..55681eced77666 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst @@ -0,0 +1,2 @@ +Consistently use same file name for different exceptions in the new repl. +Patch by Sergey B Kirpichev. From 76b80e928465b8d82e526638f5f6411276caf4c8 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 22 Aug 2024 12:24:14 +0300 Subject: [PATCH 2/2] +1 --- Lib/test/test_pyrepl/test_pyrepl.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 57a1f2c286f877..779849759225e5 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1105,6 +1105,8 @@ 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("", output)