Skip to content

Commit

Permalink
bpo-43121: Fix incorrect SyntaxError message for missing comma (GH-24436
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pablogsal authored Feb 3, 2021
1 parent bfe544d commit d4e6ed7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -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 '}' {
Expand Down
20 changes: 18 additions & 2 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an incorrect :exc:`SyntaxError` message for missing comma in literals.
Patch by Pablo Galindo.
13 changes: 8 additions & 5 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)) // '[' | '{'
&&
Expand All @@ -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;
Expand All @@ -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:
Expand Down

0 comments on commit d4e6ed7

Please sign in to comment.