[3.9] bpo-44947: Refine the syntax error for trailing commas in import statements (GH-27814) (GH-27817)

(cherry picked from commit b2f68b1900)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Łukasz Langa 2021-08-18 23:03:59 +02:00 committed by GitHub
parent b2779b2aa1
commit 4e4d35d332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 6 deletions

View File

@ -692,5 +692,5 @@ invalid_group:
| '(' a=starred_expression ')' { | '(' a=starred_expression ')' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") } RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
invalid_import_from_targets: invalid_import_from_targets:
| import_from_as_names ',' { | import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") } RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

View File

@ -727,6 +727,13 @@
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: trailing comma not allowed without surrounding parentheses SyntaxError: trailing comma not allowed without surrounding parentheses
# Check that we dont raise the "trailing comma" error if there is more
# input to the left of the valid part that we parsed.
>>> from t import x,y, and 3
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> (): int >>> (): int
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: only single target (not tuple) can be annotated SyntaxError: only single target (not tuple) can be annotated

View File

@ -0,0 +1,2 @@
Refine the syntax error for trailing commas in import statements. Patch by
Pablo Galindo.

View File

@ -15375,7 +15375,7 @@ invalid_group_rule(Parser *p)
return _res; return _res;
} }
// invalid_import_from_targets: import_from_as_names ',' // invalid_import_from_targets: import_from_as_names ',' NEWLINE
static void * static void *
invalid_import_from_targets_rule(Parser *p) invalid_import_from_targets_rule(Parser *p)
{ {
@ -15386,21 +15386,24 @@ invalid_import_from_targets_rule(Parser *p)
} }
void * _res = NULL; void * _res = NULL;
int _mark = p->mark; int _mark = p->mark;
{ // import_from_as_names ',' { // import_from_as_names ',' NEWLINE
if (p->error_indicator) { if (p->error_indicator) {
D(p->level--); D(p->level--);
return NULL; return NULL;
} }
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','")); D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
Token * _literal; Token * _literal;
asdl_seq* import_from_as_names_var; asdl_seq* import_from_as_names_var;
Token * newline_var;
if ( if (
(import_from_as_names_var = import_from_as_names_rule(p)) // import_from_as_names (import_from_as_names_var = import_from_as_names_rule(p)) // import_from_as_names
&& &&
(_literal = _PyPegen_expect_token(p, 12)) // token=',' (_literal = _PyPegen_expect_token(p, 12)) // token=','
&&
(newline_var = _PyPegen_expect_token(p, NEWLINE)) // token='NEWLINE'
) )
{ {
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','")); D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
_res = RAISE_SYNTAX_ERROR ( "trailing comma not allowed without surrounding parentheses" ); _res = RAISE_SYNTAX_ERROR ( "trailing comma not allowed without surrounding parentheses" );
if (_res == NULL && PyErr_Occurred()) { if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1; p->error_indicator = 1;
@ -15411,7 +15414,7 @@ invalid_import_from_targets_rule(Parser *p)
} }
p->mark = _mark; p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_import_from_targets[%d-%d]: %s failed!\n", p->level, ' ', D(fprintf(stderr, "%*c%s invalid_import_from_targets[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ','")); p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ',' NEWLINE"));
} }
_res = NULL; _res = NULL;
done: done: