File tree Expand file tree Collapse file tree 5 files changed +37
-5
lines changed
Filter options
Misc/NEWS.d/next/Core and Builtins Expand file tree Collapse file tree 5 files changed +37
-5
lines changed
Original file line number Diff line number Diff line change @@ -214,7 +214,11 @@ struct _ts {
214
214
215
215
};
216
216
217
- #ifdef __wasi__
217
+ #ifdef Py_DEBUG
218
+ // A debug build is likely built with low optimization level which implies
219
+ // higher stack memory usage than a release build: use a lower limit.
220
+ # define Py_C_RECURSION_LIMIT 500
221
+ #elif defined(__wasi__ )
218
222
// WASI has limited call stack. Python's recursion limit depends on code
219
223
// layout, optimization, and WASI runtime. Wasmtime can handle about 700
220
224
// recursions, sometimes less. 500 is a more conservative limit.
Original file line number Diff line number Diff line change 12
12
import weakref
13
13
from functools import partial
14
14
from textwrap import dedent
15
+ try :
16
+ import _testinternalcapi
17
+ except ImportError :
18
+ _testinternalcapi = None
15
19
16
20
from test import support
17
21
from test .support .import_helper import import_fresh_module
@@ -1118,12 +1122,14 @@ def next(self):
1118
1122
return self
1119
1123
enum ._test_simple_enum (_Precedence , ast ._Precedence )
1120
1124
1121
- @unittest .skipIf (support .is_wasi , "exhausts limited stack on WASI" )
1122
1125
@support .cpython_only
1123
1126
def test_ast_recursion_limit (self ):
1124
1127
fail_depth = support .EXCEEDS_RECURSION_LIMIT
1125
1128
crash_depth = 100_000
1126
1129
success_depth = 1200
1130
+ if _testinternalcapi is not None :
1131
+ remaining = _testinternalcapi .get_c_recursion_remaining ()
1132
+ success_depth = min (success_depth , remaining )
1127
1133
1128
1134
def check_limit (prefix , repeated ):
1129
1135
expect_ok = prefix + repeated * success_depth
Original file line number Diff line number Diff line change 14
14
import textwrap
15
15
import subprocess
16
16
import warnings
17
+ try :
18
+ import _testinternalcapi
19
+ except ImportError :
20
+ _testinternalcapi = None
17
21
18
22
support .requires_working_socket (module = True )
19
23
@@ -3033,16 +3037,21 @@ def test_trace_unpack_long_sequence(self):
3033
3037
self .assertEqual (counts , {'call' : 1 , 'line' : 301 , 'return' : 1 })
3034
3038
3035
3039
def test_trace_lots_of_globals (self ):
3040
+ count = 1000
3041
+ if _testinternalcapi is not None :
3042
+ remaining = _testinternalcapi .get_c_recursion_remaining ()
3043
+ count = min (count , remaining )
3044
+
3036
3045
code = """if 1:
3037
3046
def f():
3038
3047
return (
3039
3048
{}
3040
3049
)
3041
- """ .format ("\n +\n " .join (f"var{ i } \n " for i in range (1000 )))
3042
- ns = {f"var{ i } " : i for i in range (1000 )}
3050
+ """ .format ("\n +\n " .join (f"var{ i } \n " for i in range (count )))
3051
+ ns = {f"var{ i } " : i for i in range (count )}
3043
3052
exec (code , ns )
3044
3053
counts = self .count_traces (ns ["f" ])
3045
- self .assertEqual (counts , {'call' : 1 , 'line' : 2000 , 'return' : 1 })
3054
+ self .assertEqual (counts , {'call' : 1 , 'line' : count * 2 , 'return' : 1 })
3046
3055
3047
3056
3048
3057
class TestEdgeCases (unittest .TestCase ):
Original file line number Diff line number Diff line change
1
+ When Python is built in debug mode, set the C recursion limit to 500 instead
2
+ of 1500. A debug build is likely built with low optimization level which
3
+ implies higher stack memory usage than a release build. Patch by Victor
4
+ Stinner.
Original file line number Diff line number Diff line change @@ -109,6 +109,14 @@ get_recursion_depth(PyObject *self, PyObject *Py_UNUSED(args))
109
109
}
110
110
111
111
112
+ static PyObject *
113
+ get_c_recursion_remaining (PyObject * self , PyObject * Py_UNUSED (args ))
114
+ {
115
+ PyThreadState * tstate = _PyThreadState_GET ();
116
+ return PyLong_FromLong (tstate -> c_recursion_remaining );
117
+ }
118
+
119
+
112
120
static PyObject *
113
121
test_bswap (PyObject * self , PyObject * Py_UNUSED (args ))
114
122
{
@@ -1611,6 +1619,7 @@ perf_trampoline_set_persist_after_fork(PyObject *self, PyObject *args)
1611
1619
static PyMethodDef module_functions [] = {
1612
1620
{"get_configs" , get_configs , METH_NOARGS },
1613
1621
{"get_recursion_depth" , get_recursion_depth , METH_NOARGS },
1622
+ {"get_c_recursion_remaining" , get_c_recursion_remaining , METH_NOARGS },
1614
1623
{"test_bswap" , test_bswap , METH_NOARGS },
1615
1624
{"test_popcount" , test_popcount , METH_NOARGS },
1616
1625
{"test_bit_length" , test_bit_length , METH_NOARGS },
You can’t perform that action at this time.
0 commit comments