Skip to content

Commit b2f68b1

Browse files
authored
bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814)
1 parent 31ee985 commit b2f68b1

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ invalid_group:
11691169
| '(' a='**' expression ')' {
11701170
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
11711171
invalid_import_from_targets:
1172-
| import_from_as_names ',' {
1172+
| import_from_as_names ',' NEWLINE {
11731173
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
11741174

11751175
invalid_with_stmt:

Lib/test/test_syntax.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,13 @@
12021202
Traceback (most recent call last):
12031203
SyntaxError: trailing comma not allowed without surrounding parentheses
12041204
1205+
# Check that we dont raise the "trailing comma" error if there is more
1206+
# input to the left of the valid part that we parsed.
1207+
1208+
>>> from t import x,y, and 3
1209+
Traceback (most recent call last):
1210+
SyntaxError: invalid syntax
1211+
12051212
>>> (): int
12061213
Traceback (most recent call last):
12071214
SyntaxError: only single target (not tuple) can be annotated
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Refine the syntax error for trailing commas in import statements. Patch by
2+
Pablo Galindo.

Parser/parser.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19540,7 +19540,7 @@ invalid_group_rule(Parser *p)
1954019540
return _res;
1954119541
}
1954219542

19543-
// invalid_import_from_targets: import_from_as_names ','
19543+
// invalid_import_from_targets: import_from_as_names ',' NEWLINE
1954419544
static void *
1954519545
invalid_import_from_targets_rule(Parser *p)
1954619546
{
@@ -19551,21 +19551,24 @@ invalid_import_from_targets_rule(Parser *p)
1955119551
}
1955219552
void * _res = NULL;
1955319553
int _mark = p->mark;
19554-
{ // import_from_as_names ','
19554+
{ // import_from_as_names ',' NEWLINE
1955519555
if (p->error_indicator) {
1955619556
D(p->level--);
1955719557
return NULL;
1955819558
}
19559-
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
19559+
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1956019560
Token * _literal;
1956119561
asdl_alias_seq* import_from_as_names_var;
19562+
Token * newline_var;
1956219563
if (
1956319564
(import_from_as_names_var = import_from_as_names_rule(p)) // import_from_as_names
1956419565
&&
1956519566
(_literal = _PyPegen_expect_token(p, 12)) // token=','
19567+
&&
19568+
(newline_var = _PyPegen_expect_token(p, NEWLINE)) // token='NEWLINE'
1956619569
)
1956719570
{
19568-
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
19571+
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1956919572
_res = RAISE_SYNTAX_ERROR ( "trailing comma not allowed without surrounding parentheses" );
1957019573
if (_res == NULL && PyErr_Occurred()) {
1957119574
p->error_indicator = 1;
@@ -19576,7 +19579,7 @@ invalid_import_from_targets_rule(Parser *p)
1957619579
}
1957719580
p->mark = _mark;
1957819581
D(fprintf(stderr, "%*c%s invalid_import_from_targets[%d-%d]: %s failed!\n", p->level, ' ',
19579-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ','"));
19582+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1958019583
}
1958119584
_res = NULL;
1958219585
done:

0 commit comments

Comments
 (0)