diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2019-03-18 13:51:53 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-18 13:51:53 +0000 |
commit | 0c9258a6d299e0484538ef8d4b23f30515283db2 (patch) | |
tree | 0f38434d24356b79691c3fe31206b5cc0b7e701b | |
parent | 2ddc7f6d6223840c9971b36b30da4b3371d6e52b (diff) | |
download | cpython-git-0c9258a6d299e0484538ef8d4b23f30515283db2.tar.gz |
bpo-36332: Allow compile() to handle AST objects with assignment expressions (GH-12398)
-rw-r--r-- | Lib/test/test_ast.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst | 2 | ||||
-rw-r--r-- | Python/ast.c | 7 |
3 files changed, 17 insertions, 3 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index a7ee0da38d..e39d1f2e17 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -135,6 +135,9 @@ exec_tests = [ "@deco1\n@deco2()\nclass C: pass", # Decorator with generator argument "@deco(a for a in b)\ndef f(): pass", + # Simple assignment expression + "(a := 1)", + ] # These are compiled through "single" @@ -276,6 +279,13 @@ class AST_Tests(unittest.TestCase): with self.subTest(action="compiling", input=i, kind=kind): compile(ast_tree, "?", kind) + def test_ast_validation(self): + # compile() is the only function that calls PyAST_Validate + snippets_to_validate = exec_tests + single_tests + eval_tests + for snippet in snippets_to_validate: + tree = ast.parse(snippet) + compile(tree, '<string>', 'exec') + def test_slice(self): slc = ast.parse("x[::]").body[0].value.slice self.assertIsNone(slc.upper) @@ -1677,6 +1687,7 @@ exec_results = [ ('Module', [('AsyncFunctionDef', (3, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (3, 15))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])], None, None)], []), ('Module', [('ClassDef', (3, 0), 'C', [], [], [('Pass', (3, 9))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 0), ('Name', (2, 1), 'deco2', ('Load',)), [], [])])], []), ('Module', [('FunctionDef', (2, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (2, 9))], [('Call', (1, 1), ('Name', (1, 1), 'deco', ('Load',)), [('GeneratorExp', (1, 5), ('Name', (1, 6), 'a', ('Load',)), [('comprehension', ('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 17), 'b', ('Load',)), [], 0)])], [])], None, None)], []), +('Module', [('Expr', (1, 0), ('NamedExpr', (1, 1), ('Name', (1, 1), 'a', ('Store',)), ('Constant', (1, 6), 1, None)))], []), ] single_results = [ ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Constant', (1, 0), 1, None), ('Add',), ('Constant', (1, 2), 2, None)))]), diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst b/Misc/NEWS.d/next/Core and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst new file mode 100644 index 0000000000..2c69c1c565 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-03-18-09-27-54.bpo-36332.yEC-Vz.rst @@ -0,0 +1,2 @@ +The builtin :func:`compile` can now handle AST objects that contain +assignment expressions. Patch by Pablo Galindo. diff --git a/Python/ast.c b/Python/ast.c index 971b8ddc8c..e9154fecff 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -316,13 +316,14 @@ validate_expr(expr_ty exp, expr_context_ty ctx) return validate_exprs(exp->v.List.elts, ctx, 0); case Tuple_kind: return validate_exprs(exp->v.Tuple.elts, ctx, 0); + case NamedExpr_kind: + return validate_expr(exp->v.NamedExpr.value, Load); /* This last case doesn't have any checking. */ case Name_kind: return 1; - default: - PyErr_SetString(PyExc_SystemError, "unexpected expression"); - return 0; } + PyErr_SetString(PyExc_SystemError, "unexpected expression"); + return 0; } static int |