]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-122546: use same filename for different exceptions in new repl (GH-123217...
authorSergey B Kirpichev <skirpichev@gmail.com>
Thu, 22 Aug 2024 23:28:09 +0000 (02:28 +0300)
committerGitHub <noreply@github.com>
Thu, 22 Aug 2024 23:28:09 +0000 (00:28 +0100)
Lib/_pyrepl/console.py
Lib/code.py
Lib/test/test_pyrepl/test_pyrepl.py
Misc/NEWS.d/next/Library/2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst [new file with mode: 0644]

index 43193432af2ec6062165927c88e4df8e0f7d6b71..f04a1ba9fa47baf74869a5cd2ce2a2b2cd1f8daa 100644 (file)
@@ -162,7 +162,7 @@ class InteractiveColoredConsole(code.InteractiveConsole):
         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)
index aec7d61c1b896d3c7ef728b3b912eea94dfb88de..ccf2d9d144ca011f392b94202058326498df3533 100644 (file)
@@ -113,16 +113,7 @@ class InteractiveInterpreter:
         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()
index d5eafc5eb58cac268ee234521875ac7832f9cc6d..a433212fe92ddc0e920547dc32d570d7781274c6 100644 (file)
@@ -1100,6 +1100,16 @@ class TestMain(TestCase):
         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],
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 (file)
index 0000000..55681ec
--- /dev/null
@@ -0,0 +1,2 @@
+Consistently use same file name for different exceptions in the new repl.
+Patch by Sergey B Kirpichev.