Skip to content

Commit b977f85

Browse files
authored
[3.10] bpo-34013: Generalize the invalid legacy statement error message (GH-27389). (GH-27391)
(cherry picked from commit 6948964) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent be24154 commit b977f85

File tree

7 files changed

+339
-246
lines changed

7 files changed

+339
-246
lines changed

Grammar/python.gram

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,15 @@ expression_without_invalid[expr_ty]:
848848
| a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
849849
| disjunction
850850
| lambdef
851+
invalid_legacy_expression:
852+
| a=NAME b=expression_without_invalid {
853+
_PyPegen_check_legacy_stmt(p, a) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Missing parentheses in call to '%U'.", a->v.Name.id) : NULL}
854+
851855
invalid_expression:
856+
| invalid_legacy_expression
852857
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
853858
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
854-
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
859+
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
855860
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
856861

857862
invalid_named_expression:

Lib/test/test_exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,15 @@ def ckmsg(src, msg, exception=SyntaxError):
175175
ckmsg(s, "Missing parentheses in call to 'print'. "
176176
"Did you mean print(\"old style\", end=\" \")?")
177177

178+
s = 'print f(a+b,c)'
179+
ckmsg(s, "Missing parentheses in call to 'print'.")
180+
178181
s = '''exec "old style"'''
179182
ckmsg(s, "Missing parentheses in call to 'exec'")
180183

184+
s = 'exec f(a+b,c)'
185+
ckmsg(s, "Missing parentheses in call to 'exec'.")
186+
181187
# should not apply to subclasses, see issue #31161
182188
s = '''if True:\nprint "No indent"'''
183189
ckmsg(s, "expected an indented block after 'if' statement on line 1", IndentationError)

Lib/test/test_syntax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ def test_expression_with_assignment(self):
13051305
)
13061306

13071307
def test_curly_brace_after_primary_raises_immediately(self):
1308-
self._check_error("f{", "invalid syntax", mode="single")
1308+
self._check_error("f{}", "invalid syntax", mode="single")
13091309

13101310
def test_assign_call(self):
13111311
self._check_error("f() = 1", "assign")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Generalize the invalid legacy statement custom error message (like the one
2+
generated when "print" is called without parentheses) to include more
3+
generic expressions. Patch by Pablo Galindo

0 commit comments

Comments
 (0)