Skip to content

Commit 7ac84e6

Browse files
committed
bpo-45727: Make the syntax error for missing comma more consistent
1 parent f291404 commit 7ac84e6

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
@@ -1078,11 +1079,10 @@ invalid_legacy_expression:
10781079
"Missing parentheses in call to '%U'. Did you mean %U(...)?", a->v.Name.id, a->v.Name.id) : NULL}
10791080

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

10881088
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
@@ -9717,6 +9717,7 @@ expressions_rule(Parser *p)
97179717

97189718
// expression:
97199719
// | invalid_expression
9720+
// | invalid_legacy_expression
97209721
// | disjunction 'if' disjunction 'else' expression
97219722
// | disjunction
97229723
// | lambdef
@@ -9762,6 +9763,25 @@ expression_rule(Parser *p)
97629763
D(fprintf(stderr, "%*c%s expression[%d-%d]: %s failed!\n", p->level, ' ',
97639764
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_expression"));
97649765
}
9766+
if (p->call_invalid_rules) { // invalid_legacy_expression
9767+
if (p->error_indicator) {
9768+
D(p->level--);
9769+
return NULL;
9770+
}
9771+
D(fprintf(stderr, "%*c> expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
9772+
void *invalid_legacy_expression_var;
9773+
if (
9774+
(invalid_legacy_expression_var = invalid_legacy_expression_rule(p)) // invalid_legacy_expression
9775+
)
9776+
{
9777+
D(fprintf(stderr, "%*c+ expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
9778+
_res = invalid_legacy_expression_var;
9779+
goto done;
9780+
}
9781+
p->mark = _mark;
9782+
D(fprintf(stderr, "%*c%s expression[%d-%d]: %s failed!\n", p->level, ' ',
9783+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_legacy_expression"));
9784+
}
97659785
{ // disjunction 'if' disjunction 'else' expression
97669786
if (p->error_indicator) {
97679787
D(p->level--);
@@ -18207,7 +18227,6 @@ invalid_legacy_expression_rule(Parser *p)
1820718227
}
1820818228

1820918229
// invalid_expression:
18210-
// | invalid_legacy_expression
1821118230
// | !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid
1821218231
// | disjunction 'if' disjunction !('else' | ':')
1821318232
static void *
@@ -18220,25 +18239,6 @@ invalid_expression_rule(Parser *p)
1822018239
}
1822118240
void * _res = NULL;
1822218241
int _mark = p->mark;
18223-
if (p->call_invalid_rules) { // invalid_legacy_expression
18224-
if (p->error_indicator) {
18225-
D(p->level--);
18226-
return NULL;
18227-
}
18228-
D(fprintf(stderr, "%*c> invalid_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
18229-
void *invalid_legacy_expression_var;
18230-
if (
18231-
(invalid_legacy_expression_var = invalid_legacy_expression_rule(p)) // invalid_legacy_expression
18232-
)
18233-
{
18234-
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "invalid_legacy_expression"));
18235-
_res = invalid_legacy_expression_var;
18236-
goto done;
18237-
}
18238-
p->mark = _mark;
18239-
D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ',
18240-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "invalid_legacy_expression"));
18241-
}
1824218242
{ // !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid
1824318243
if (p->error_indicator) {
1824418244
D(p->level--);
@@ -18256,7 +18256,7 @@ invalid_expression_rule(Parser *p)
1825618256
)
1825718257
{
1825818258
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
18259-
_res = RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
18259+
_res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
1826018260
if (_res == NULL && PyErr_Occurred()) {
1826118261
p->error_indicator = 1;
1826218262
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)