Skip to content

Commit 4e4d35d

Browse files
ambvpablogsal
andauthored
[3.9] bpo-44947: Refine the syntax error for trailing commas in import statements (pythonGH-27814) (pythonGH-27817)
(cherry picked from commit b2f68b1) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
1 parent b2779b2 commit 4e4d35d

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

Grammar/python.gram

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,5 +692,5 @@ invalid_group:
692692
| '(' a=starred_expression ')' {
693693
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
694694
invalid_import_from_targets:
695-
| import_from_as_names ',' {
695+
| import_from_as_names ',' NEWLINE {
696696
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

Lib/test/test_syntax.py

+7
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,13 @@
727727
Traceback (most recent call last):
728728
SyntaxError: trailing comma not allowed without surrounding parentheses
729729
730+
# Check that we dont raise the "trailing comma" error if there is more
731+
# input to the left of the valid part that we parsed.
732+
733+
>>> from t import x,y, and 3
734+
Traceback (most recent call last):
735+
SyntaxError: invalid syntax
736+
730737
>>> (): int
731738
Traceback (most recent call last):
732739
SyntaxError: only single target (not tuple) can be annotated
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/pegen/parse.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -15375,7 +15375,7 @@ invalid_group_rule(Parser *p)
1537515375
return _res;
1537615376
}
1537715377

15378-
// invalid_import_from_targets: import_from_as_names ','
15378+
// invalid_import_from_targets: import_from_as_names ',' NEWLINE
1537915379
static void *
1538015380
invalid_import_from_targets_rule(Parser *p)
1538115381
{
@@ -15386,21 +15386,24 @@ invalid_import_from_targets_rule(Parser *p)
1538615386
}
1538715387
void * _res = NULL;
1538815388
int _mark = p->mark;
15389-
{ // import_from_as_names ','
15389+
{ // import_from_as_names ',' NEWLINE
1539015390
if (p->error_indicator) {
1539115391
D(p->level--);
1539215392
return NULL;
1539315393
}
15394-
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
15394+
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1539515395
Token * _literal;
1539615396
asdl_seq* import_from_as_names_var;
15397+
Token * newline_var;
1539715398
if (
1539815399
(import_from_as_names_var = import_from_as_names_rule(p)) // import_from_as_names
1539915400
&&
1540015401
(_literal = _PyPegen_expect_token(p, 12)) // token=','
15402+
&&
15403+
(newline_var = _PyPegen_expect_token(p, NEWLINE)) // token='NEWLINE'
1540115404
)
1540215405
{
15403-
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
15406+
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1540415407
_res = RAISE_SYNTAX_ERROR ( "trailing comma not allowed without surrounding parentheses" );
1540515408
if (_res == NULL && PyErr_Occurred()) {
1540615409
p->error_indicator = 1;
@@ -15411,7 +15414,7 @@ invalid_import_from_targets_rule(Parser *p)
1541115414
}
1541215415
p->mark = _mark;
1541315416
D(fprintf(stderr, "%*c%s invalid_import_from_targets[%d-%d]: %s failed!\n", p->level, ' ',
15414-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ','"));
15417+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ',' NEWLINE"));
1541515418
}
1541615419
_res = NULL;
1541715420
done:

0 commit comments

Comments
 (0)