[Python-checkins] cpython (3.5): Issue #24975: Fix AST compilation for PEP 448 syntax.

yury.selivanov python-checkins at python.org
Wed Sep 2 21:50:34 CEST 2015


https://hg.python.org/cpython/rev/e9df8543d7bc
changeset:   97583:e9df8543d7bc
branch:      3.5
parent:      97541:dcfe871b56ac
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Tue Sep 01 16:10:49 2015 -0400
summary:
  Issue #24975: Fix AST compilation for PEP 448 syntax.

files:
  Lib/test/test_ast.py |  24 +++++++++++++++---------
  Misc/NEWS            |   2 ++
  Python/ast.c         |   6 ++++--
  3 files changed, 21 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -78,9 +78,9 @@
     # Pass,
     "pass",
     # Break
-    "break",
+    "for v in v:break",
     # Continue
-    "continue",
+    "for v in v:continue",
     # for statements with naked tuples (see http://bugs.python.org/issue6704)
     "for a,b in c: pass",
     "[(a,b) for a,b in c]",
@@ -112,6 +112,9 @@
     "async def f():\n async for e in i: 1\n else: 2",
     # AsyncWith
     "async def f():\n async with a as b: 1",
+    # PEP 448: Additional Unpacking Generalizations
+    "{**{1:2}, 2:3}",
+    "{*{1, 2}, 3}",
 ]
 
 # These are compiled through "single"
@@ -231,9 +234,12 @@
                                     (single_tests, single_results, "single"),
                                     (eval_tests, eval_results, "eval")):
             for i, o in zip(input, output):
-                ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
-                self.assertEqual(to_tuple(ast_tree), o)
-                self._assertTrueorder(ast_tree, (0, 0))
+                with self.subTest(action="parsing", input=i):
+                    ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
+                    self.assertEqual(to_tuple(ast_tree), o)
+                    self._assertTrueorder(ast_tree, (0, 0))
+                with self.subTest(action="compiling", input=i):
+                    compile(ast_tree, "?", kind)
 
     def test_slice(self):
         slc = ast.parse("x[::]").body[0].value.slice
@@ -780,8 +786,6 @@
     def test_dict(self):
         d = ast.Dict([], [ast.Name("x", ast.Load())])
         self.expr(d, "same number of keys as values")
-        d = ast.Dict([None], [ast.Name("x", ast.Load())])
-        self.expr(d, "None disallowed")
         d = ast.Dict([ast.Name("x", ast.Load())], [None])
         self.expr(d, "None disallowed")
 
@@ -972,8 +976,8 @@
 ('Module', [('Global', (1, 0), ['v'])]),
 ('Module', [('Expr', (1, 0), ('Num', (1, 0), 1))]),
 ('Module', [('Pass', (1, 0))]),
-('Module', [('Break', (1, 0))]),
-('Module', [('Continue', (1, 0))]),
+('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Break', (1, 11))], [])]),
+('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Continue', (1, 11))], [])]),
 ('Module', [('For', (1, 0), ('Tuple', (1, 4), [('Name', (1, 4), 'a', ('Store',)), ('Name', (1, 6), 'b', ('Store',))], ('Store',)), ('Name', (1, 11), 'c', ('Load',)), [('Pass', (1, 14))], [])]),
 ('Module', [('Expr', (1, 0), ('ListComp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
 ('Module', [('Expr', (1, 0), ('GeneratorExp', (1, 1), ('Tuple', (1, 2), [('Name', (1, 2), 'a', ('Load',)), ('Name', (1, 4), 'b', ('Load',))], ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'a', ('Store',)), ('Name', (1, 13), 'b', ('Store',))], ('Store',)), ('Name', (1, 18), 'c', ('Load',)), [])]))]),
@@ -986,6 +990,8 @@
 ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]),
 ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 7), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]),
 ('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 7), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]),
+('Module', [('Expr', (1, 0), ('Dict', (1, 1), [None, ('Num', (1, 10), 2)], [('Dict', (1, 4), [('Num', (1, 4), 1)], [('Num', (1, 6), 2)]), ('Num', (1, 12), 3)]))]),
+('Module', [('Expr', (1, 0), ('Set', (1, 1), [('Starred', (1, 1), ('Set', (1, 3), [('Num', (1, 3), 1), ('Num', (1, 6), 2)]), ('Load',)), ('Num', (1, 10), 3)]))]),
 ]
 single_results = [
 ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #24975: Fix AST compilation for PEP 448 syntax.
+
 Library
 -------
 
diff --git a/Python/ast.c b/Python/ast.c
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -199,8 +199,10 @@
                             "Dict doesn't have the same number of keys as values");
             return 0;
         }
-        return validate_exprs(exp->v.Dict.keys, Load, 0) &&
-            validate_exprs(exp->v.Dict.values, Load, 0);
+        /* null_ok=1 for keys expressions to allow dict unpacking to work in
+           dict literals, i.e. ``{**{a:b}}`` */
+        return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
+            validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
     case Set_kind:
         return validate_exprs(exp->v.Set.elts, Load, 0);
 #define COMP(NAME) \

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list