summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Python/ast_opt.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tools/python3/Python/ast_opt.c')
-rw-r--r--contrib/tools/python3/Python/ast_opt.c53
1 files changed, 29 insertions, 24 deletions
diff --git a/contrib/tools/python3/Python/ast_opt.c b/contrib/tools/python3/Python/ast_opt.c
index e8a53786ff4..fa6bc70d748 100644
--- a/contrib/tools/python3/Python/ast_opt.c
+++ b/contrib/tools/python3/Python/ast_opt.c
@@ -1,10 +1,19 @@
/* AST Optimizer */
#include "Python.h"
#include "pycore_ast.h" // _PyAST_GetDocString()
-#include "pycore_compile.h" // _PyASTOptimizeState
-#include "pycore_long.h" // _PyLong
-#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_format.h" // F_LJUST
+#include "pycore_long.h" // _PyLong
+#include "pycore_pystate.h" // _PyThreadState_GET()
+#include "pycore_setobject.h" // _PySet_NextEntry()
+
+
+typedef struct {
+ int optimize;
+ int ff_features;
+
+ int recursion_depth; /* current recursion depth */
+ int recursion_limit; /* recursion limit */
+} _PyASTOptimizeState;
static int
@@ -133,15 +142,6 @@ check_complexity(PyObject *obj, Py_ssize_t limit)
}
return limit;
}
- else if (PyFrozenSet_Check(obj)) {
- Py_ssize_t i = 0;
- PyObject *item;
- Py_hash_t hash;
- limit -= PySet_GET_SIZE(obj);
- while (limit >= 0 && _PySet_NextEntry(obj, &i, &item, &hash)) {
- limit = check_complexity(item, limit);
- }
- }
return limit;
}
@@ -165,9 +165,8 @@ safe_multiply(PyObject *v, PyObject *w)
return NULL;
}
}
- else if (PyLong_Check(v) && (PyTuple_Check(w) || PyFrozenSet_Check(w))) {
- Py_ssize_t size = PyTuple_Check(w) ? PyTuple_GET_SIZE(w) :
- PySet_GET_SIZE(w);
+ else if (PyLong_Check(v) && PyTuple_Check(w)) {
+ Py_ssize_t size = PyTuple_GET_SIZE(w);
if (size) {
long n = PyLong_AsLong(v);
if (n < 0 || n > MAX_COLLECTION_SIZE / size) {
@@ -189,8 +188,7 @@ safe_multiply(PyObject *v, PyObject *w)
}
}
else if (PyLong_Check(w) &&
- (PyTuple_Check(v) || PyFrozenSet_Check(v) ||
- PyUnicode_Check(v) || PyBytes_Check(v)))
+ (PyTuple_Check(v) || PyUnicode_Check(v) || PyBytes_Check(v)))
{
return safe_multiply(w, v);
}
@@ -1088,10 +1086,13 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
switch (node_->kind) {
case TypeVar_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
+ CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value);
break;
case ParamSpec_kind:
+ CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value);
break;
case TypeVarTuple_kind:
+ CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value);
break;
}
return 1;
@@ -1102,30 +1103,34 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
#undef CALL_SEQ
int
-_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
+_PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize, int ff_features)
{
PyThreadState *tstate;
int starting_recursion_depth;
+ _PyASTOptimizeState state;
+ state.optimize = optimize;
+ state.ff_features = ff_features;
+
/* Setup recursion depth check counters */
tstate = _PyThreadState_GET();
if (!tstate) {
return 0;
}
/* Be careful here to prevent overflow. */
- int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
+ int recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
starting_recursion_depth = recursion_depth;
- state->recursion_depth = starting_recursion_depth;
- state->recursion_limit = C_RECURSION_LIMIT;
+ state.recursion_depth = starting_recursion_depth;
+ state.recursion_limit = Py_C_RECURSION_LIMIT;
- int ret = astfold_mod(mod, arena, state);
+ int ret = astfold_mod(mod, arena, &state);
assert(ret || PyErr_Occurred());
/* Check that the recursion depth counting balanced correctly */
- if (ret && state->recursion_depth != starting_recursion_depth) {
+ if (ret && state.recursion_depth != starting_recursion_depth) {
PyErr_Format(PyExc_SystemError,
"AST optimizer recursion depth mismatch (before=%d, after=%d)",
- starting_recursion_depth, state->recursion_depth);
+ starting_recursion_depth, state.recursion_depth);
return 0;
}