mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-122313: Clean up deep recursion guarding code in the compiler (GH-122640)
Add ENTER_RECURSIVE and LEAVE_RECURSIVE macros in ast.c, ast_opt.c and symtable.c. Remove VISIT_QUIT macro in symtable.c. The current recursion depth counter only needs to be updated during normal execution -- all functions should just return an error code if an error occurs.
This commit is contained in:
parent
fe0a28d850
commit
efcd65cd84
3 changed files with 164 additions and 162 deletions
|
@ -15,6 +15,19 @@ typedef struct {
|
|||
int recursion_limit; /* recursion limit */
|
||||
} _PyASTOptimizeState;
|
||||
|
||||
#define ENTER_RECURSIVE(ST) \
|
||||
do { \
|
||||
if (++(ST)->recursion_depth > (ST)->recursion_limit) { \
|
||||
PyErr_SetString(PyExc_RecursionError, \
|
||||
"maximum recursion depth exceeded during compilation"); \
|
||||
return 0; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define LEAVE_RECURSIVE(ST) \
|
||||
do { \
|
||||
--(ST)->recursion_depth; \
|
||||
} while(0)
|
||||
|
||||
static int
|
||||
make_const(expr_ty node, PyObject *val, PyArena *arena)
|
||||
|
@ -708,11 +721,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
static int
|
||||
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
||||
{
|
||||
if (++state->recursion_depth > state->recursion_limit) {
|
||||
PyErr_SetString(PyExc_RecursionError,
|
||||
"maximum recursion depth exceeded during compilation");
|
||||
return 0;
|
||||
}
|
||||
ENTER_RECURSIVE(state);
|
||||
switch (node_->kind) {
|
||||
case BoolOp_kind:
|
||||
CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
|
||||
|
@ -811,7 +820,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
case Name_kind:
|
||||
if (node_->v.Name.ctx == Load &&
|
||||
_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
|
||||
state->recursion_depth--;
|
||||
LEAVE_RECURSIVE(state);
|
||||
return make_const(node_, PyBool_FromLong(!state->optimize), ctx_);
|
||||
}
|
||||
break;
|
||||
|
@ -824,7 +833,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
// No default case, so the compiler will emit a warning if new expression
|
||||
// kinds are added without being handled here
|
||||
}
|
||||
state->recursion_depth--;
|
||||
LEAVE_RECURSIVE(state);;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -871,11 +880,7 @@ astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
static int
|
||||
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
||||
{
|
||||
if (++state->recursion_depth > state->recursion_limit) {
|
||||
PyErr_SetString(PyExc_RecursionError,
|
||||
"maximum recursion depth exceeded during compilation");
|
||||
return 0;
|
||||
}
|
||||
ENTER_RECURSIVE(state);
|
||||
switch (node_->kind) {
|
||||
case FunctionDef_kind:
|
||||
CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params);
|
||||
|
@ -999,7 +1004,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
// No default case, so the compiler will emit a warning if new statement
|
||||
// kinds are added without being handled here
|
||||
}
|
||||
state->recursion_depth--;
|
||||
LEAVE_RECURSIVE(state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1031,11 +1036,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
// Currently, this is really only used to form complex/negative numeric
|
||||
// constants in MatchValue and MatchMapping nodes
|
||||
// We still recurse into all subexpressions and subpatterns anyway
|
||||
if (++state->recursion_depth > state->recursion_limit) {
|
||||
PyErr_SetString(PyExc_RecursionError,
|
||||
"maximum recursion depth exceeded during compilation");
|
||||
return 0;
|
||||
}
|
||||
ENTER_RECURSIVE(state);
|
||||
switch (node_->kind) {
|
||||
case MatchValue_kind:
|
||||
CALL(astfold_expr, expr_ty, node_->v.MatchValue.value);
|
||||
|
@ -1067,7 +1068,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
|
|||
// No default case, so the compiler will emit a warning if new pattern
|
||||
// kinds are added without being handled here
|
||||
}
|
||||
state->recursion_depth--;
|
||||
LEAVE_RECURSIVE(state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue