Skip to content

Commit 546cefc

Browse files
authored
bpo-45727: Make the syntax error for missing comma more consistent (GH-29427)
1 parent c8c21bd commit 546cefc

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ expressions[expr_ty]:
618618

619619
expression[expr_ty] (memo):
620620
| invalid_expression
621+
| invalid_legacy_expression
621622
| a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
622623
| disjunction
623624
| lambdef
@@ -1080,11 +1081,10 @@ invalid_legacy_expression:
10801081
"Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}
10811082

10821083
invalid_expression:
1083-
| invalid_legacy_expression
10841084
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
10851085
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
10861086
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
1087-
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
1087+
_PyPegen_check_legacy_stmt(p, a) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
10881088
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
10891089

10901090
invalid_named_expression:

Lib/test/test_exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def testSyntaxErrorOffset(self):
233233
check('[file for\n str(file) in []]', 2, 2)
234234
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
235235
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)
236+
check("a b c d e f", 1, 1)
236237

237238
# Errors thrown by compile.c
238239
check('class foo:return 1', 1, 11)

Parser/parser.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9719,6 +9719,7 @@ expressions_rule(Parser *p)
97199719

97209720
// expression:
97219721
// | invalid_expression
9722+
// | invalid_legacy_expression
97229723
// | disjunction 'if' disjunction 'else' expression
97239724
// | disjunction
97249725
// | lambdef
@@ -9764,6 +9765,25 @@ expression_rule(Parser *p)
97649765
D(fprintf(stderr, "%*c%s expression[%d-%d]: %s failed!\n", p->level, ' ',
97659766
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_expression"));
97669767
}
9768+
if (p->call_invalid_rules) { // invalid_legacy_expression
9769+
if (p->error_indicator) {
9770+
D(p->level--);
9771+
return NULL;
9772+
}
9773+
D(fprintf(stderr, "%*c> expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
9774+
void *invalid_legacy_expression_var;
9775+
if (
9776+
(invalid_legacy_expression_var = invalid_legacy_expression_rule(p)) // invalid_legacy_expression
9777+
)
9778+
{
9779+
D(fprintf(stderr, "%*c+ expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
9780+
_res = invalid_legacy_expression_var;
9781+
goto done;
9782+
}
9783+
p->mark = _mark;
9784+
D(fprintf(stderr, "%*c%s expression[%d-%d]: %s failed!\n", p->level, ' ',
9785+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_legacy_expression"));
9786+
}
97679787
{ // disjunction 'if' disjunction 'else' expression
97689788
if (p->error_indicator) {
97699789
D(p->level--);
@@ -18239,7 +18259,6 @@ invalid_legacy_expression_rule(Parser *p)
1823918259
}
1824018260

1824118261
// invalid_expression:
18242-
// | invalid_legacy_expression
1824318262
// | !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid
1824418263
// | disjunction 'if' disjunction !('else' | ':')
1824518264
static void *
@@ -18252,25 +18271,6 @@ invalid_expression_rule(Parser *p)
1825218271
}
1825318272
void * _res = NULL;
1825418273
int _mark = p->mark;
18255-
if (p->call_invalid_rules) { // invalid_legacy_expression
18256-
if (p->error_indicator) {
18257-
D(p->level--);
18258-
return NULL;
18259-
}
18260-
D(fprintf(stderr, "%*c> invalid_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
18261-
void *invalid_legacy_expression_var;
18262-
if (
18263-
(invalid_legacy_expression_var = invalid_legacy_expression_rule(p)) // invalid_legacy_expression
18264-
)
18265-
{
18266-
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
18267-
_res = invalid_legacy_expression_var;
18268-
goto done;
18269-
}
18270-
p->mark = _mark;
18271-
D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ',
18272-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_legacy_expression"));
18273-
}
1827418274
{ // !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid
1827518275
if (p->error_indicator) {
1827618276
D(p->level--);
@@ -18288,7 +18288,7 @@ invalid_expression_rule(Parser *p)
1828818288
)
1828918289
{
1829018290
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
18291-
_res = RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
18291+
_res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
1829218292
if (_res == NULL && PyErr_Occurred()) {
1829318293
p->error_indicator = 1;
1829418294
D(p->level--);

Parser/pegen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ _PyPegen_check_barry_as_flufl(Parser *p, Token* t) {
7979

8080
int
8181
_PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
82-
assert(name->kind == Name_kind);
82+
if (name->kind != Name_kind) {
83+
return 0;
84+
}
8385
const char* candidates[2] = {"print", "exec"};
8486
for (int i=0; i<2; i++) {
8587
if (PyUnicode_CompareWithASCIIString(name->v.Name.id, candidates[i]) == 0) {

0 commit comments

Comments
 (0)