Skip to content

Commit c8f4069

Browse files
authored
[3.13] gh-121804: Always show error location for SyntaxError's in new repl (GH-121886) (#123148)
(cherry picked from commit 354d55e)
1 parent 21399a0 commit c8f4069

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

Lib/_pyrepl/console.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ def __init__(
161161
super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg]
162162
self.can_colorize = _colorize.can_colorize()
163163

164-
def showsyntaxerror(self, filename=None):
165-
super().showsyntaxerror(colorize=self.can_colorize)
164+
def showsyntaxerror(self, filename=None, **kwargs):
165+
super().showsyntaxerror(colorize=self.can_colorize, **kwargs)
166166

167167
def showtraceback(self):
168168
super().showtraceback(colorize=self.can_colorize)
@@ -171,7 +171,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
171171
try:
172172
tree = ast.parse(source)
173173
except (SyntaxError, OverflowError, ValueError):
174-
self.showsyntaxerror(filename)
174+
self.showsyntaxerror(filename, source=source)
175175
return False
176176
if tree.body:
177177
*_, last_stmt = tree.body
@@ -188,10 +188,10 @@ def runsource(self, source, filename="<input>", symbol="single"):
188188
f"Try the asyncio REPL ({python} -m asyncio) to use"
189189
f" top-level 'await' and run background asyncio tasks."
190190
)
191-
self.showsyntaxerror(filename)
191+
self.showsyntaxerror(filename, source=source)
192192
return False
193193
except (OverflowError, ValueError):
194-
self.showsyntaxerror(filename)
194+
self.showsyntaxerror(filename, source=source)
195195
return False
196196

197197
if code is None:

Lib/code.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
6464
code = self.compile(source, filename, symbol)
6565
except (OverflowError, SyntaxError, ValueError):
6666
# Case 1
67-
self.showsyntaxerror(filename)
67+
self.showsyntaxerror(filename, source=source)
6868
return False
6969

7070
if code is None:
@@ -123,6 +123,12 @@ def showsyntaxerror(self, filename=None, **kwargs):
123123
# Stuff in the right filename
124124
value = SyntaxError(msg, (filename, lineno, offset, line))
125125
sys.last_exc = sys.last_value = value
126+
# Set the line of text that the exception refers to
127+
source = kwargs.pop('source', '')
128+
lines = source.splitlines()
129+
if (source and type is SyntaxError
130+
and not value.text and len(lines) >= value.lineno):
131+
value.text = lines[value.lineno - 1]
126132
if sys.excepthook is sys.__excepthook__:
127133
lines = traceback.format_exception_only(type, value, colorize=colorize)
128134
self.write(''.join(lines))

Lib/idlelib/pyshell.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ def prepend_syspath(self, filename):
706706
del _filename, _sys, _dirname, _dir
707707
\n""".format(filename))
708708

709-
def showsyntaxerror(self, filename=None):
709+
def showsyntaxerror(self, filename=None, **kwargs):
710710
"""Override Interactive Interpreter method: Use Colorizing
711711
712712
Color the offending position instead of printing it and pointing at it

Lib/test/test_pyrepl/test_interact.py

+14
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ def test_runsource_returns_false_for_failed_compilation(self):
8888
self.assertFalse(result)
8989
self.assertIn('SyntaxError', f.getvalue())
9090

91+
@force_not_colorized
92+
def test_runsource_show_syntax_error_location(self):
93+
console = InteractiveColoredConsole()
94+
source = "def f(x, x): ..."
95+
f = io.StringIO()
96+
with contextlib.redirect_stderr(f):
97+
result = console.runsource(source)
98+
self.assertFalse(result)
99+
r = """
100+
def f(x, x): ...
101+
^
102+
SyntaxError: duplicate argument 'x' in function definition"""
103+
self.assertIn(r, f.getvalue())
104+
91105
def test_runsource_shows_syntax_error_for_failed_compilation(self):
92106
console = InteractiveColoredConsole()
93107
source = "print('Hello, world!'"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly show error locations, when :exc:`SyntaxError` raised in new repl.
2+
Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)