bpo-42374: Allow unparenthesized walrus in genexps (GH-23319)

This fixes a regression that was introduced by the new parser.

Automerge-Triggered-By: GH:lysnikolaou
This commit is contained in:
Lysandros Nikolaou 2020-11-17 01:08:35 +02:00 committed by GitHub
parent f62dad16b8
commit cb3e5ed071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 7 deletions

View File

@ -517,7 +517,7 @@ group[expr_ty]:
| '(' a=(yield_expr | named_expression) ')' { a } | '(' a=(yield_expr | named_expression) ')' { a }
| invalid_group | invalid_group
genexp[expr_ty]: genexp[expr_ty]:
| '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) } | '(' a=named_expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
| invalid_comprehension | invalid_comprehension
set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) } set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
setcomp[expr_ty]: setcomp[expr_ty]:

View File

@ -513,6 +513,15 @@ spam()"""
self.assertEqual(nonlocal_var, None) self.assertEqual(nonlocal_var, None)
f() f()
def test_named_expression_scope_in_genexp(self):
a = 1
b = [1, 2, 3, 4]
genexp = (c := i + a for i in b)
self.assertNotIn("c", locals())
for idx, elem in enumerate(genexp):
self.assertEqual(elem, b[idx] + a)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1,2 @@
Fix a regression introduced by the new parser, where an unparenthesized walrus operator
was not allowed within generator expressions.

View File

@ -11357,7 +11357,7 @@ group_rule(Parser *p)
return _res; return _res;
} }
// genexp: '(' expression ~ for_if_clauses ')' | invalid_comprehension // genexp: '(' named_expression ~ for_if_clauses ')' | invalid_comprehension
static expr_ty static expr_ty
genexp_rule(Parser *p) genexp_rule(Parser *p)
{ {
@ -11377,12 +11377,12 @@ genexp_rule(Parser *p)
UNUSED(_start_lineno); // Only used by EXTRA macro UNUSED(_start_lineno); // Only used by EXTRA macro
int _start_col_offset = p->tokens[_mark]->col_offset; int _start_col_offset = p->tokens[_mark]->col_offset;
UNUSED(_start_col_offset); // Only used by EXTRA macro UNUSED(_start_col_offset); // Only used by EXTRA macro
{ // '(' expression ~ for_if_clauses ')' { // '(' named_expression ~ for_if_clauses ')'
if (p->error_indicator) { if (p->error_indicator) {
D(p->level--); D(p->level--);
return NULL; return NULL;
} }
D(fprintf(stderr, "%*c> genexp[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' expression ~ for_if_clauses ')'")); D(fprintf(stderr, "%*c> genexp[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' named_expression ~ for_if_clauses ')'"));
int _cut_var = 0; int _cut_var = 0;
Token * _literal; Token * _literal;
Token * _literal_1; Token * _literal_1;
@ -11391,7 +11391,7 @@ genexp_rule(Parser *p)
if ( if (
(_literal = _PyPegen_expect_token(p, 7)) // token='(' (_literal = _PyPegen_expect_token(p, 7)) // token='('
&& &&
(a = expression_rule(p)) // expression (a = named_expression_rule(p)) // named_expression
&& &&
(_cut_var = 1) (_cut_var = 1)
&& &&
@ -11400,7 +11400,7 @@ genexp_rule(Parser *p)
(_literal_1 = _PyPegen_expect_token(p, 8)) // token=')' (_literal_1 = _PyPegen_expect_token(p, 8)) // token=')'
) )
{ {
D(fprintf(stderr, "%*c+ genexp[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' expression ~ for_if_clauses ')'")); D(fprintf(stderr, "%*c+ genexp[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' named_expression ~ for_if_clauses ')'"));
Token *_token = _PyPegen_get_last_nonnwhitespace_token(p); Token *_token = _PyPegen_get_last_nonnwhitespace_token(p);
if (_token == NULL) { if (_token == NULL) {
D(p->level--); D(p->level--);
@ -11420,7 +11420,7 @@ genexp_rule(Parser *p)
} }
p->mark = _mark; p->mark = _mark;
D(fprintf(stderr, "%*c%s genexp[%d-%d]: %s failed!\n", p->level, ' ', D(fprintf(stderr, "%*c%s genexp[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' expression ~ for_if_clauses ')'")); p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' named_expression ~ for_if_clauses ')'"));
if (_cut_var) { if (_cut_var) {
D(p->level--); D(p->level--);
return NULL; return NULL;