Skip to content

Commit 4897476

Browse files
committed
Use C recursion limit to determine AST recursion limits.
1 parent e29a4cb commit 4897476

6 files changed

Lines changed: 11 additions & 27 deletions

File tree

‎Lib/test/test_compile.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ def __getitem__(self, key):
111111

112112
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
113113
def test_extended_arg(self):
114-
# default: 1000 * 2.5 = 2500 repetitions
115-
repeat = int(sys.getrecursionlimit() * 2.5)
114+
repeat = 2000
116115
longexpr = 'x = x or ' + '-x' * repeat
117116
g = {}
118117
code = '''

‎Parser/asdl_c.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,19 +1380,16 @@ class PartingShots(StaticVisitor):
13801380
return NULL;
13811381
}
13821382
1383-
int recursion_limit = Py_GetRecursionLimit();
13841383
int starting_recursion_depth;
13851384
/* Be careful here to prevent overflow. */
13861385
int COMPILER_STACK_FRAME_SCALE = 3;
13871386
PyThreadState *tstate = _PyThreadState_GET();
13881387
if (!tstate) {
13891388
return 0;
13901389
}
1391-
state->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
1392-
recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
1390+
state->recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
13931391
int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
1394-
starting_recursion_depth = (recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
1395-
recursion_depth * COMPILER_STACK_FRAME_SCALE : recursion_depth;
1392+
starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
13961393
state->recursion_depth = starting_recursion_depth;
13971394
13981395
PyObject *result = ast2obj_mod(state, t);

‎Python/Python-ast.c‎

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Python/ast.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,6 @@ _PyAST_Validate(mod_ty mod)
975975
int res = -1;
976976
struct validator state;
977977
PyThreadState *tstate;
978-
int recursion_limit = Py_GetRecursionLimit();
979978
int starting_recursion_depth;
980979

981980
/* Setup recursion depth check counters */
@@ -985,11 +984,9 @@ _PyAST_Validate(mod_ty mod)
985984
}
986985
/* Be careful here to prevent overflow. */
987986
int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
988-
starting_recursion_depth = (recursion_depth< INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
989-
recursion_depth * COMPILER_STACK_FRAME_SCALE : recursion_depth;
987+
starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
990988
state.recursion_depth = starting_recursion_depth;
991-
state.recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
992-
recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
989+
state.recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
993990

994991
switch (mod->kind) {
995992
case Module_kind:

‎Python/ast_opt.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,6 @@ int
10801080
_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
10811081
{
10821082
PyThreadState *tstate;
1083-
int recursion_limit = Py_GetRecursionLimit();
10841083
int starting_recursion_depth;
10851084

10861085
/* Setup recursion depth check counters */
@@ -1090,11 +1089,9 @@ _PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
10901089
}
10911090
/* Be careful here to prevent overflow. */
10921091
int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
1093-
starting_recursion_depth = (recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
1094-
recursion_depth * COMPILER_STACK_FRAME_SCALE : recursion_depth;
1092+
starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
10951093
state->recursion_depth = starting_recursion_depth;
1096-
state->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
1097-
recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
1094+
state->recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
10981095

10991096
int ret = astfold_mod(mod, arena, state);
11001097
assert(ret || PyErr_Occurred());

‎Python/symtable.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
278278
asdl_stmt_seq *seq;
279279
int i;
280280
PyThreadState *tstate;
281-
int recursion_limit = Py_GetRecursionLimit();
282281
int starting_recursion_depth;
283282

284283
if (st == NULL)
@@ -299,11 +298,9 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
299298
}
300299
/* Be careful here to prevent overflow. */
301300
int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
302-
starting_recursion_depth = (recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
303-
recursion_depth * COMPILER_STACK_FRAME_SCALE : recursion_depth;
301+
starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
304302
st->recursion_depth = starting_recursion_depth;
305-
st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
306-
recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
303+
st->recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
307304

308305
/* Make the initial symbol information gathering pass */
309306
if (!symtable_enter_block(st, &_Py_ID(top), ModuleBlock, (void *)mod, 0, 0, 0, 0)) {

0 commit comments

Comments
 (0)