summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-08-23 08:01:17 (GMT)
committerGitHub <noreply@github.com>2023-08-23 08:01:17 (GMT)
commit2dfbd4f36dd83f88f5df64c33612dd34eff256bb (patch)
treefca70758a85ee8f31122ec1f9cd882b425d5cf6b /Python
parent79fdacc0059a3959074d2d9d054653eae1dcfe06 (diff)
downloadcpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.zip
cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.tar.gz
cpython-2dfbd4f36dd83f88f5df64c33612dd34eff256bb.tar.bz2
gh-108113: Make it possible to optimize an AST (#108282)
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c39
-rw-r--r--Python/compile.c18
-rw-r--r--Python/pythonrun.c22
3 files changed, 47 insertions, 32 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;
diff --git a/Python/compile.c b/Python/compile.c
index 3260dba..4b2f70a 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -557,6 +557,24 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
return co;
}
+int
+_PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
+ int optimize, PyArena *arena)
+{
+ PyFutureFeatures future;
+ if (!_PyFuture_FromAST(mod, filename, &future)) {
+ return -1;
+ }
+ int flags = future.ff_features | cf->cf_flags;
+ if (optimize == -1) {
+ optimize = _Py_GetConfig()->optimization_level;
+ }
+ if (!_PyAST_Optimize(mod, arena, optimize, flags)) {
+ return -1;
+ }
+ return 0;
+}
+
static void
compiler_free(struct compiler *c)
{
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index a3de779..05b7dfa 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -22,7 +22,6 @@
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException, _Py_Offer_Suggestions
#include "pycore_pylifecycle.h" // _Py_UnhandledKeyboardInterrupt
#include "pycore_pystate.h" // _PyInterpreterState_GET()
-#include "pycore_symtable.h" // _PyFuture_FromAST()
#include "pycore_sysmodule.h" // _PySys_Audit()
#include "pycore_traceback.h" // _PyTraceBack_Print_Indented()
@@ -1792,24 +1791,6 @@ error:
return NULL;
}
-static int
-ast_optimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
- int optimize, PyArena *arena)
-{
- PyFutureFeatures future;
- if (!_PyFuture_FromAST(mod, filename, &future)) {
- return -1;
- }
- int flags = future.ff_features | cf->cf_flags;
- if (optimize == -1) {
- optimize = _Py_GetConfig()->optimization_level;
- }
- if (!_PyAST_Optimize(mod, arena, optimize, flags)) {
- return -1;
- }
- return 0;
-}
-
PyObject *
Py_CompileStringObject(const char *str, PyObject *filename, int start,
PyCompilerFlags *flags, int optimize)
@@ -1827,8 +1808,7 @@ Py_CompileStringObject(const char *str, PyObject *filename, int start,
}
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
if ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_OPTIMIZED_AST) {
- if (ast_optimize(mod, filename, flags, optimize, arena) < 0) {
- _PyArena_Free(arena);
+ if (_PyCompile_AstOptimize(mod, filename, flags, optimize, arena) < 0) {
return NULL;
}
}