[3.10] bpo-34013: Generalize the invalid legacy statement error messa… · python/cpython@b977f85 (original) (raw)
File tree
7 files changed
lines changed
- Misc/NEWS.d/next/Core and Builtins
7 files changed
lines changed
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
@@ -848,10 +848,15 @@ expression_without_invalid[expr_ty]: | |||
848 | 848 | | a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) } | |
849 | 849 | | disjunction | |
850 | 850 | | 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 | + | ||
851 | 855 | invalid_expression: | |
856 | + | invalid_legacy_expression | ||
852 | 857 | # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf" | |
853 | 858 | # 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 { | |
855 | 860 | RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") } | |
856 | 861 | ||
857 | 862 | invalid_named_expression: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -175,9 +175,15 @@ def ckmsg(src, msg, exception=SyntaxError): | ||
175 | 175 | ckmsg(s, "Missing parentheses in call to 'print'. " |
176 | 176 | "Did you mean print(\"old style\", end=\" \")?") |
177 | 177 | |
178 | +s = 'print f(a+b,c)' | |
179 | +ckmsg(s, "Missing parentheses in call to 'print'.") | |
180 | + | |
178 | 181 | s = '''exec "old style"''' |
179 | 182 | ckmsg(s, "Missing parentheses in call to 'exec'") |
180 | 183 | |
184 | +s = 'exec f(a+b,c)' | |
185 | +ckmsg(s, "Missing parentheses in call to 'exec'.") | |
186 | + | |
181 | 187 | # should not apply to subclasses, see issue #31161 |
182 | 188 | s = '''if True:\nprint "No indent"''' |
183 | 189 | ckmsg(s, "expected an indented block after 'if' statement on line 1", IndentationError) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1305,7 +1305,7 @@ def test_expression_with_assignment(self): | ||
1305 | 1305 | ) |
1306 | 1306 | |
1307 | 1307 | 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") | |
1309 | 1309 | |
1310 | 1310 | def test_assign_call(self): |
1311 | 1311 | self._check_error("f() = 1", "assign") |
Original file line number | Diff line number | Diff 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 |