Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4e6ed7

Browse files
authored
bpo-43121: Fix incorrect SyntaxError message for missing comma (GH-24436)
1 parent bfe544d commit d4e6ed7

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ invalid_primary:
694694
invalid_comprehension:
695695
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
696696
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
697-
| ('[' | '{') a=star_named_expression ',' [star_named_expressions] {
697+
| ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
698698
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
699699
invalid_dict_comprehension:
700700
| '{' a='**' bitwise_or for_if_clauses '}' {

Lib/test/test_syntax.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,25 @@
246246
Traceback (most recent call last):
247247
SyntaxError: did you forget parentheses around the comprehension target?
248248
249-
>>> {x,y: None for x,y in range(100)}
249+
# Missing commas in literals collections should not
250+
# produce special error messages regarding missing
251+
# parentheses
252+
253+
>>> [1, 2 3]
250254
Traceback (most recent call last):
251-
SyntaxError: did you forget parentheses around the comprehension target?
255+
SyntaxError: invalid syntax
256+
257+
>>> {1, 2 3}
258+
Traceback (most recent call last):
259+
SyntaxError: invalid syntax
260+
261+
>>> {1:2, 2:5 3:12}
262+
Traceback (most recent call last):
263+
SyntaxError: invalid syntax
264+
265+
>>> (1, 2 3)
266+
Traceback (most recent call last):
267+
SyntaxError: invalid syntax
252268
253269
From compiler_complex_args():
254270
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed an incorrect :exc:`SyntaxError` message for missing comma in literals.
2+
Patch by Pablo Galindo.

Parser/parser.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15217,7 +15217,7 @@ invalid_primary_rule(Parser *p)
1521715217

1521815218
// invalid_comprehension:
1521915219
// | ('[' | '(' | '{') starred_expression for_if_clauses
15220-
// | ('[' | '{') star_named_expression ',' star_named_expressions?
15220+
// | ('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses
1522115221
static void *
1522215222
invalid_comprehension_rule(Parser *p)
1522315223
{
@@ -15258,17 +15258,18 @@ invalid_comprehension_rule(Parser *p)
1525815258
D(fprintf(stderr, "%*c%s invalid_comprehension[%d-%d]: %s failed!\n", p->level, ' ',
1525915259
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '(' | '{') starred_expression for_if_clauses"));
1526015260
}
15261-
{ // ('[' | '{') star_named_expression ',' star_named_expressions?
15261+
{ // ('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses
1526215262
if (p->error_indicator) {
1526315263
D(p->level--);
1526415264
return NULL;
1526515265
}
15266-
D(fprintf(stderr, "%*c> invalid_comprehension[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
15266+
D(fprintf(stderr, "%*c> invalid_comprehension[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
1526715267
Token * _literal;
1526815268
void *_opt_var;
1526915269
UNUSED(_opt_var); // Silence compiler warnings
1527015270
void *_tmp_132_var;
1527115271
expr_ty a;
15272+
asdl_comprehension_seq* for_if_clauses_var;
1527215273
if (
1527315274
(_tmp_132_var = _tmp_132_rule(p)) // '[' | '{'
1527415275
&&
@@ -15277,9 +15278,11 @@ invalid_comprehension_rule(Parser *p)
1527715278
(_literal = _PyPegen_expect_token(p, 12)) // token=','
1527815279
&&
1527915280
(_opt_var = star_named_expressions_rule(p), 1) // star_named_expressions?
15281+
&&
15282+
(for_if_clauses_var = for_if_clauses_rule(p)) // for_if_clauses
1528015283
)
1528115284
{
15282-
D(fprintf(stderr, "%*c+ invalid_comprehension[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
15285+
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"));
1528315286
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "did you forget parentheses around the comprehension target?" );
1528415287
if (_res == NULL && PyErr_Occurred()) {
1528515288
p->error_indicator = 1;
@@ -15290,7 +15293,7 @@ invalid_comprehension_rule(Parser *p)
1529015293
}
1529115294
p->mark = _mark;
1529215295
D(fprintf(stderr, "%*c%s invalid_comprehension[%d-%d]: %s failed!\n", p->level, ' ',
15293-
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
15296+
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
1529415297
}
1529515298
_res = NULL;
1529615299
done:

0 commit comments

Comments
 (0)