diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index 43193432af2ec6..f04a1ba9fa47ba 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(colorize=self.can_colorize, **kwargs) + super().showsyntaxerror(filename=filename, **kwargs) def showtraceback(self): super().showtraceback(colorize=self.can_colorize) diff --git a/Lib/code.py b/Lib/code.py index aec7d61c1b896d..ccf2d9d144ca01 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -113,16 +113,7 @@ def showsyntaxerror(self, filename=None, **kwargs): 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() diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index d5eafc5eb58cac..a433212fe92ddc 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1100,6 +1100,16 @@ 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) + if "can't use pyrepl" in output: + self.skipTest("pyrepl not available") + self.assertIn("SyntaxError: invalid syntax", output) + self.assertIn("", 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 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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy