Skip to content

Commit 6948964

Browse files
authored
bpo-34013: Generalize the invalid legacy statement error message (GH-27389)
1 parent 2f54fba commit 6948964

File tree

6 files changed

+337
-244
lines changed

6 files changed

+337
-244
lines changed

Grammar/python.gram

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

856861
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)
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)