bpo-43121: Fix incorrect SyntaxError message for missing comma (GH-24436)

This commit is contained in:
Pablo Galindo 2021-02-03 23:29:26 +00:00 committed by GitHub
parent bfe544d2f2
commit d4e6ed7e5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 8 deletions

View file

@ -694,7 +694,7 @@ invalid_primary:
invalid_comprehension:
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
| ('[' | '{') a=star_named_expression ',' [star_named_expressions] {
| ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
invalid_dict_comprehension:
| '{' a='**' bitwise_or for_if_clauses '}' {

View file

@ -246,9 +246,25 @@
Traceback (most recent call last):
SyntaxError: did you forget parentheses around the comprehension target?
>>> {x,y: None for x,y in range(100)}
# Missing commas in literals collections should not
# produce special error messages regarding missing
# parentheses
>>> [1, 2 3]
Traceback (most recent call last):
SyntaxError: did you forget parentheses around the comprehension target?
SyntaxError: invalid syntax
>>> {1, 2 3}
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> {1:2, 2:5 3:12}
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> (1, 2 3)
Traceback (most recent call last):
SyntaxError: invalid syntax
From compiler_complex_args():

View file

@ -0,0 +1,2 @@
Fixed an incorrect :exc:`SyntaxError` message for missing comma in literals.
Patch by Pablo Galindo.

View file

@ -15217,7 +15217,7 @@ invalid_primary_rule(Parser *p)
// invalid_comprehension:
// | ('[' | '(' | '{') starred_expression for_if_clauses
// | ('[' | '{') star_named_expression ',' star_named_expressions?
// | ('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses
static void *
invalid_comprehension_rule(Parser *p)
{
@ -15258,17 +15258,18 @@ invalid_comprehension_rule(Parser *p)
D(fprintf(stderr, "%*c%s invalid_comprehension[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '(' | '{') starred_expression for_if_clauses"));
}
{ // ('[' | '{') star_named_expression ',' star_named_expressions?
{ // ('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> invalid_comprehension[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
D(fprintf(stderr, "%*c> invalid_comprehension[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
Token * _literal;
void *_opt_var;
UNUSED(_opt_var); // Silence compiler warnings
void *_tmp_132_var;
expr_ty a;
asdl_comprehension_seq* for_if_clauses_var;
if (
(_tmp_132_var = _tmp_132_rule(p)) // '[' | '{'
&&
@ -15277,9 +15278,11 @@ invalid_comprehension_rule(Parser *p)
(_literal = _PyPegen_expect_token(p, 12)) // token=','
&&
(_opt_var = star_named_expressions_rule(p), 1) // star_named_expressions?
&&
(for_if_clauses_var = for_if_clauses_rule(p)) // for_if_clauses
)
{
D(fprintf(stderr, "%*c+ invalid_comprehension[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
D(fprintf(stderr, "%*c+ invalid_comprehension[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "did you forget parentheses around the comprehension target?" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
@ -15290,7 +15293,7 @@ invalid_comprehension_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_comprehension[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
}
_res = NULL;
done: