From f5f0d537e3215c278fcf732aefb3c45ebd219698 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 16 Jul 2024 20:37:05 +0300 Subject: [PATCH 1/4] gh-121804: always show error location for SyntaxError's in new repl >>> def good(x, y): ... ... def bad(x, x): ... File "", line 2 def bad(x, x): ... ^ SyntaxError: duplicate argument 'x' in function definition --- Lib/_pyrepl/console.py | 10 +++++----- Lib/code.py | 6 +++++- Lib/idlelib/pyshell.py | 2 +- Lib/test/test_pyrepl/test_interact.py | 14 ++++++++++++++ .../2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst | 3 +++ 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index a8d3f520340dcf..43193432af2ec6 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -161,8 +161,8 @@ def __init__( super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg] self.can_colorize = _colorize.can_colorize() - def showsyntaxerror(self, filename=None): - super().showsyntaxerror(colorize=self.can_colorize) + def showsyntaxerror(self, filename=None, **kwargs): + super().showsyntaxerror(colorize=self.can_colorize, **kwargs) def showtraceback(self): super().showtraceback(colorize=self.can_colorize) @@ -171,7 +171,7 @@ def runsource(self, source, filename="", symbol="single"): try: tree = ast.parse(source) except (SyntaxError, OverflowError, ValueError): - self.showsyntaxerror(filename) + self.showsyntaxerror(filename, source=source) return False if tree.body: *_, last_stmt = tree.body @@ -188,10 +188,10 @@ def runsource(self, source, filename="", symbol="single"): f"Try the asyncio REPL ({python} -m asyncio) to use" f" top-level 'await' and run background asyncio tasks." ) - self.showsyntaxerror(filename) + self.showsyntaxerror(filename, source=source) return False except (OverflowError, ValueError): - self.showsyntaxerror(filename) + self.showsyntaxerror(filename, source=source) return False if code is None: diff --git a/Lib/code.py b/Lib/code.py index a55fced0704b1d..92fa40d884fe00 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -64,7 +64,7 @@ def runsource(self, source, filename="", symbol="single"): code = self.compile(source, filename, symbol) except (OverflowError, SyntaxError, ValueError): # Case 1 - self.showsyntaxerror(filename) + self.showsyntaxerror(filename, source=source) return False if code is None: @@ -123,6 +123,10 @@ def showsyntaxerror(self, filename=None, **kwargs): # Stuff in the right filename value = SyntaxError(msg, (filename, lineno, offset, line)) sys.last_exc = sys.last_value = value + source = kwargs.pop('source', "") + if source and not value.text and type is SyntaxError: + # Set the line of text that the exception refers to + value.text = source.splitlines()[value.lineno - 1] if sys.excepthook is sys.__excepthook__: lines = traceback.format_exception_only(type, value, colorize=colorize) self.write(''.join(lines)) diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index d8b2652d5d7979..e882c6cb3b8d19 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -706,7 +706,7 @@ def prepend_syspath(self, filename): del _filename, _sys, _dirname, _dir \n""".format(filename)) - def showsyntaxerror(self, filename=None): + def showsyntaxerror(self, filename=None, **kwargs): """Override Interactive Interpreter method: Use Colorizing Color the offending position instead of printing it and pointing at it diff --git a/Lib/test/test_pyrepl/test_interact.py b/Lib/test/test_pyrepl/test_interact.py index 31f08cdb25e078..c3139ae2a7b841 100644 --- a/Lib/test/test_pyrepl/test_interact.py +++ b/Lib/test/test_pyrepl/test_interact.py @@ -88,6 +88,20 @@ def test_runsource_returns_false_for_failed_compilation(self): self.assertFalse(result) self.assertIn('SyntaxError', f.getvalue()) + @force_not_colorized + def test_runsource_show_syntax_error_location(self): + console = InteractiveColoredConsole() + source = "def f(x, x): ..." + f = io.StringIO() + with contextlib.redirect_stderr(f): + result = console.runsource(source) + self.assertFalse(result) + r = """ + def f(x, x): ... + ^ +SyntaxError: duplicate argument 'x' in function definition""" + self.assertIn(r, f.getvalue()) + def test_runsource_shows_syntax_error_for_failed_compilation(self): console = InteractiveColoredConsole() source = "print('Hello, world!'" diff --git a/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst new file mode 100644 index 00000000000000..35e21c5c859f4e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst @@ -0,0 +1,3 @@ +Add new kwarg ``source`` for +:meth:`InteractiveInterpreter.showsyntaxerror()` to support enhanced error +messages, including error locations. Patch by Sergey B Kirpichev. From 25dfe269c3a3d2bd85e707995c929c68fec34dcf Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Wed, 17 Jul 2024 06:58:19 +0300 Subject: [PATCH 2/4] amend news --- .../next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst index 35e21c5c859f4e..e82312108704c5 100644 --- a/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst +++ b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst @@ -1,3 +1,3 @@ Add new kwarg ``source`` for -:meth:`InteractiveInterpreter.showsyntaxerror()` to support enhanced error +:meth:`code.InteractiveInterpreter.showsyntaxerror()` to support enhanced error messages, including error locations. Patch by Sergey B Kirpichev. From 1aeabf2ce42d01fbf3c76daef8d794a18ea39149 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 18 Aug 2024 17:21:27 +0300 Subject: [PATCH 3/4] + reorder guard statements --- Lib/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/code.py b/Lib/code.py index dd5e1ecb79aba0..8b1006d4a3d52a 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -142,7 +142,7 @@ def _showtraceback(self, typ, value, tb, source): sys.last_traceback = tb value = value.with_traceback(tb) # Set the line of text that the exception refers to - if source and not value.text and typ is SyntaxError: + if source and typ is SyntaxError and not value.text: value.text = source.splitlines()[value.lineno - 1] sys.last_exc = sys.last_value = value if sys.excepthook is sys.__excepthook__: From bb89ab1158104e93c76a0552a78fdf0e6a8ee0f4 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 19 Aug 2024 15:49:06 +0300 Subject: [PATCH 4/4] address review: add guard and rewrote news entry --- Lib/code.py | 6 ++++-- .../Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/code.py b/Lib/code.py index 8b1006d4a3d52a..b1079824a75414 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -142,8 +142,10 @@ def _showtraceback(self, typ, value, tb, source): sys.last_traceback = tb value = value.with_traceback(tb) # Set the line of text that the exception refers to - if source and typ is SyntaxError and not value.text: - value.text = source.splitlines()[value.lineno - 1] + lines = source.splitlines() + if (source and typ is SyntaxError + and not value.text and len(lines) >= value.lineno): + value.text = lines[value.lineno - 1] sys.last_exc = sys.last_value = value if sys.excepthook is sys.__excepthook__: self._excepthook(typ, value, tb) diff --git a/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst index e82312108704c5..1cc1cde7c22704 100644 --- a/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst +++ b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst @@ -1,3 +1,2 @@ -Add new kwarg ``source`` for -:meth:`code.InteractiveInterpreter.showsyntaxerror()` to support enhanced error -messages, including error locations. Patch by Sergey B Kirpichev. +Correctly show error locations, when :exc:`SyntaxError` raised in new repl. +Patch by Sergey B Kirpichev.