diff options
author | Sergey B Kirpichev <skirpichev@gmail.com> | 2024-08-22 11:55:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-22 11:55:30 (GMT) |
commit | 3d7b1a526d858496add5b188c790b8d5fe73b06b (patch) | |
tree | c990713e27b5edba168bfb032b4294a625401c3a | |
parent | 427b106162c7467de8a84476a053dfba9ef16dfa (diff) | |
download | cpython-3d7b1a526d858496add5b188c790b8d5fe73b06b.zip cpython-3d7b1a526d858496add5b188c790b8d5fe73b06b.tar.gz cpython-3d7b1a526d858496add5b188c790b8d5fe73b06b.tar.bz2 |
gh-122546: use same filename for different exceptions in new repl (#123217)
* gh-122546: use same filename for different exceptions in new repl
* +1
-rw-r--r-- | Lib/_pyrepl/console.py | 2 | ||||
-rw-r--r-- | Lib/code.py | 10 | ||||
-rw-r--r-- | Lib/test/test_pyrepl/test_pyrepl.py | 10 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2024-08-22-11-25-19.gh-issue-122546.BSmeE7.rst | 2 |
4 files changed, 14 insertions, 10 deletions
diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index 330ebbd..3e72a56 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -162,7 +162,7 @@ class InteractiveColoredConsole(code.InteractiveConsole): 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 b107982..c559191 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -109,15 +109,7 @@ class InteractiveInterpreter: 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 e11ec74..7798497 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1101,6 +1101,16 @@ class TestMain(TestCase): 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 def test_proper_tracebacklimit(self): env = os.environ.copy() for set_tracebacklimit in [True, False]: 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 0000000..55681ec --- /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. |