diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-08-23 08:01:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-23 08:01:17 (GMT) |
commit | 2dfbd4f36dd83f88f5df64c33612dd34eff256bb (patch) | |
tree | fca70758a85ee8f31122ec1f9cd882b425d5cf6b /Python/bltinmodule.c | |
parent | 79fdacc0059a3959074d2d9d054653eae1dcfe06 (diff) | |
download | cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.zip cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.tar.gz cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.tar.bz2 |
gh-108113: Make it possible to optimize an AST (#108282)
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index d06efcf..787f53f 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -804,23 +804,40 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, if (is_ast == -1) goto error; if (is_ast) { - if (flags & PyCF_ONLY_AST) { + if ((flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST) { + // return an un-optimized AST result = Py_NewRef(source); } else { - PyArena *arena; - mod_ty mod; + // Return an optimized AST or code object - arena = _PyArena_New(); - if (arena == NULL) - goto error; - mod = PyAST_obj2mod(source, arena, compile_mode); - if (mod == NULL || !_PyAST_Validate(mod)) { - _PyArena_Free(arena); + PyArena *arena = _PyArena_New(); + if (arena == NULL) { goto error; } - result = (PyObject*)_PyAST_Compile(mod, filename, - &cf, optimize, arena); + + if (flags & PyCF_ONLY_AST) { + mod_ty mod = PyAST_obj2mod(source, arena, compile_mode); + if (mod == NULL || !_PyAST_Validate(mod)) { + _PyArena_Free(arena); + goto error; + } + if (_PyCompile_AstOptimize(mod, filename, &cf, optimize, + arena) < 0) { + _PyArena_Free(arena); + goto error; + } + result = PyAST_mod2obj(mod); + } + else { + mod_ty mod = PyAST_obj2mod(source, arena, compile_mode); + if (mod == NULL || !_PyAST_Validate(mod)) { + _PyArena_Free(arena); + goto error; + } + result = (PyObject*)_PyAST_Compile(mod, filename, + &cf, optimize, arena); + } _PyArena_Free(arena); } goto finally; |