Skip to content

Commit 846a10f

Browse files
bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814)
(cherry picked from commit b2f68b190035540872072ac1d2349e7745e85596) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent d1c0e44 commit 846a10f

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
@@ -947,7 +947,7 @@ invalid_group:
947947
| '(' a='**' expression ')' {
948948
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
949949
invalid_import_from_targets:
950-
| import_from_as_names ',' {
950+
| import_from_as_names ',' NEWLINE {
951951
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
952952

953953
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
@@ -19607,7 +19607,7 @@ invalid_group_rule(Parser *p)
1960719607
return _res;
1960819608
}
1960919609

19610-
// invalid_import_from_targets: import_from_as_names ','
19610+
// invalid_import_from_targets: import_from_as_names ',' NEWLINE
1961119611
static void *
1961219612
invalid_import_from_targets_rule(Parser *p)
1961319613
{
@@ -19618,21 +19618,24 @@ invalid_import_from_targets_rule(Parser *p)
1961819618
}
1961919619
void * _res = NULL;
1962019620
int _mark = p->mark;
19621-
{ // import_from_as_names ','
19621+
{ // import_from_as_names ',' NEWLINE
1962219622
if (p->error_indicator) {
1962319623
D(p->level--);
1962419624
return NULL;
1962519625
}
19626-
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
19626+
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1962719627
Token * _literal;
1962819628
asdl_alias_seq* import_from_as_names_var;
19629+
Token * newline_var;
1962919630
if (
1963019631
(import_from_as_names_var = import_from_as_names_rule(p)) // import_from_as_names
1963119632
&&
1963219633
(_literal = _PyPegen_expect_token(p, 12)) // token=','
19634+
&&
19635+
(newline_var = _PyPegen_expect_token(p, NEWLINE)) // token='NEWLINE'
1963319636
)
1963419637
{
19635-
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
19638+
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1963619639
_res = RAISE_SYNTAX_ERROR ( "trailing comma not allowed without surrounding parentheses" );
1963719640
if (_res == NULL && PyErr_Occurred()) {
1963819641
p->error_indicator = 1;
@@ -19643,7 +19646,7 @@ invalid_import_from_targets_rule(Parser *p)
1964319646
}
1964419647
p->mark = _mark;
1964519648
D(fprintf(stderr, "%*c%s invalid_import_from_targets[%d-%d]: %s failed!\n", p->level, ' ',
19646-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ','"));
19649+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1964719650
}
1964819651
_res = NULL;
1964919652
done:

0 commit comments

Comments
 (0)