[Python-checkins] bpo-42374: Allow unparenthesized walrus in genexps (GH-23319) (GH-23329)

lysnikolaou webhook-mailer at python.org
Mon Nov 16 18:39:07 EST 2020


https://github.com/python/cpython/commit/2b800ef809eefbc96a536e4b43a8285f2353f64d
commit: 2b800ef809eefbc96a536e4b43a8285f2353f64d
branch: 3.9
author: Lysandros Nikolaou <lisandrosnik at gmail.com>
committer: lysnikolaou <lisandrosnik at gmail.com>
date: 2020年11月17日T01:38:58+02:00
summary:
bpo-42374: Allow unparenthesized walrus in genexps (GH-23319) (GH-23329)
This fixes a regression that was introduced by the new parser.
(cherry picked from commit cb3e5ed0716114393696ec7201e51fe0595eab4f)
files:
A Misc/NEWS.d/next/Core and Builtins/2020-11-16-17-57-09.bpo-42374.t7np1E.rst
M Grammar/python.gram
M Lib/test/test_named_expressions.py
M Parser/pegen/parse.c
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 60eeb367e95ba..257d2f5153263 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -503,7 +503,7 @@ group[expr_ty]:
 | '(' a=(yield_expr | named_expression) ')' { a }
 | invalid_group
 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
 set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
 setcomp[expr_ty]:
diff --git a/Lib/test/test_named_expressions.py b/Lib/test/test_named_expressions.py
index c813830ce6d3c..d614f3267fad3 100644
--- a/Lib/test/test_named_expressions.py
+++ b/Lib/test/test_named_expressions.py
@@ -513,6 +513,15 @@ def g():
 self.assertEqual(nonlocal_var, None)
 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__":
 unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-11-16-17-57-09.bpo-42374.t7np1E.rst b/Misc/NEWS.d/next/Core and Builtins/2020-11-16-17-57-09.bpo-42374.t7np1E.rst
new file mode 100644
index 0000000000000..d86d038c8425c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-11-16-17-57-09.bpo-42374.t7np1E.rst	
@@ -0,0 +1,2 @@
+Fix a regression introduced by the new parser, where an unparenthesized walrus operator
+was not allowed within generator expressions.
\ No newline at end of file
diff --git a/Parser/pegen/parse.c b/Parser/pegen/parse.c
index 97cefa9c2a3d4..c78abfadd2bc9 100644
--- a/Parser/pegen/parse.c
+++ b/Parser/pegen/parse.c
@@ -11381,7 +11381,7 @@ group_rule(Parser *p)
 return _res;
 }
 
-// genexp: '(' expression ~ for_if_clauses ')' | invalid_comprehension
+// genexp: '(' named_expression ~ for_if_clauses ')' | invalid_comprehension
 static expr_ty
 genexp_rule(Parser *p)
 {
@@ -11401,12 +11401,12 @@ genexp_rule(Parser *p)
 UNUSED(_start_lineno); // Only used by EXTRA macro
 int _start_col_offset = p->tokens[_mark]->col_offset;
 UNUSED(_start_col_offset); // Only used by EXTRA macro
- { // '(' expression ~ for_if_clauses ')'
+ { // '(' named_expression ~ for_if_clauses ')'
 if (p->error_indicator) {
 D(p->level--);
 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;
 Token * _literal;
 Token * _literal_1;
@@ -11415,7 +11415,7 @@ genexp_rule(Parser *p)
 if (
 (_literal = _PyPegen_expect_token(p, 7)) // token='('
 &&
- (a = expression_rule(p)) // expression
+ (a = named_expression_rule(p)) // named_expression
 &&
 (_cut_var = 1)
 &&
@@ -11424,7 +11424,7 @@ genexp_rule(Parser *p)
 (_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);
 if (_token == NULL) {
 D(p->level--);
@@ -11444,7 +11444,7 @@ genexp_rule(Parser *p)
 }
 p->mark = _mark;
 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) {
 D(p->level--);
 return NULL;


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /