https://github.com/python/cpython/commit/354d55eb1fa40f272419aa6459ee5d2c4804c8ea
commit: 354d55eb1fa40f272419aa6459ee5d2c4804c8ea
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-08-19T15:19:23+01:00
summary:
gh-121804: Always show error location for SyntaxError's in new repl (#121886)
files:
A Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst
M Lib/_pyrepl/console.py
M Lib/code.py
M Lib/idlelib/pyshell.py
M Lib/test/test_pyrepl/test_interact.py
diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py
index 2b6c6beab7b304..330ebbd6185679 100644
--- a/Lib/_pyrepl/console.py
+++ b/Lib/_pyrepl/console.py
@@ -161,6 +161,9 @@ 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, **kwargs):
+ super().showsyntaxerror(**kwargs)
+
def _excepthook(self, typ, value, tb):
import traceback
lines = traceback.format_exception(
@@ -173,7 +176,7 @@ def runsource(self, source, filename="<input>",
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
@@ -190,10 +193,10 @@ def runsource(self, source, filename="<input>",
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 6860b61a48df3f..b1079824a75414 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -64,7 +64,7 @@ def runsource(self, source, filename="<input>",
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:
@@ -94,7 +94,7 @@ def runcode(self, code):
except:
self.showtraceback()
- def showsyntaxerror(self, filename=None):
+ def showsyntaxerror(self, filename=None, **kwargs):
"""Display the syntax error that just occurred.
This doesn't display a stack trace because there isn't one.
@@ -118,7 +118,8 @@ def showsyntaxerror(self, filename=None):
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
- self._showtraceback(typ, value, None)
+ source = kwargs.pop('source', "")
+ self._showtraceback(typ, value, None, source)
finally:
typ = value = tb = None
@@ -132,14 +133,20 @@ def showtraceback(self):
"""
try:
typ, value, tb = sys.exc_info()
- self._showtraceback(typ, value, tb.tb_next)
+ self._showtraceback(typ, value, tb.tb_next, "")
finally:
typ = value = tb = None
- def _showtraceback(self, typ, value, tb):
+ def _showtraceback(self, typ, value, tb, source):
sys.last_type = typ
sys.last_traceback = tb
- sys.last_exc = sys.last_value = value = value.with_traceback(tb)
+ value = value.with_traceback(tb)
+ # Set the line of text that the exception refers to
+ 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)
else:
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 369dab316af132..b7adaffbac0e22 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..1cc1cde7c22704
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-07-16-20-49-07.gh-issue-121804.gYN-In.rst
@@ -0,0 +1,2 @@
+Correctly show error locations, when :exc:`SyntaxError` raised in new repl.
+Patch by Sergey B Kirpichev.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]