Skip to content

Commit 190e8fb

Browse files
authored
[3.11] gh-108851: Fix support.get_recursion_available() for USE_STACKCHECK (#110127)
Add _testcapi.USE_STACKCHECK. USE_STACKCHECK on using on Windows 32-bit.
1 parent a95e8cf commit 190e8fb

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Lib/test/support/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2243,7 +2243,16 @@ def get_recursion_available():
22432243
"""
22442244
limit = sys.getrecursionlimit()
22452245
depth = get_recursion_depth()
2246-
return limit - depth
2246+
2247+
try:
2248+
from _testcapi import USE_STACKCHECK
2249+
except ImportError:
2250+
USE_STACKCHECK = False
2251+
2252+
if USE_STACKCHECK:
2253+
return max(limit - depth - 1, 0)
2254+
else:
2255+
return limit - depth
22472256

22482257
@contextlib.contextmanager
22492258
def set_recursion_limit(limit):

Lib/test/test_support.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,10 @@ def test_get_recursion_depth(self):
704704
code = textwrap.dedent("""
705705
from test import support
706706
import sys
707+
try:
708+
from _testcapi import USE_STACKCHECK
709+
except ImportError:
710+
USE_STACKCHECK = False
707711
708712
def check(cond):
709713
if not cond:
@@ -728,19 +732,24 @@ def test_recursive(depth, limit):
728732
check(get_depth == depth)
729733
test_recursive(depth + 1, limit)
730734
735+
if USE_STACKCHECK:
736+
# f-string consumes 2 frames and -1 for USE_STACKCHECK
737+
IGNORE = 3
738+
else:
739+
# f-string consumes 2 frames
740+
IGNORE = 2
741+
731742
# depth up to 25
732743
with support.infinite_recursion(max_depth=25):
733744
limit = sys.getrecursionlimit()
734745
print(f"test with sys.getrecursionlimit()={limit}")
735-
# Use limit-2 since f-string seems to consume 2 frames.
736-
test_recursive(2, limit - 2)
746+
test_recursive(2, limit - IGNORE)
737747
738748
# depth up to 500
739749
with support.infinite_recursion(max_depth=500):
740750
limit = sys.getrecursionlimit()
741751
print(f"test with sys.getrecursionlimit()={limit}")
742-
# limit-2 since f-string seems to consume 2 frames
743-
test_recursive(2, limit - 2)
752+
test_recursive(2, limit - IGNORE)
744753
""")
745754
script_helper.assert_python_ok("-c", code)
746755

Modules/_testcapimodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8203,8 +8203,14 @@ PyInit__testcapi(void)
82038203
#else
82048204
v = Py_False;
82058205
#endif
8206-
Py_INCREF(v);
8207-
PyModule_AddObject(m, "WITH_PYMALLOC", v);
8206+
PyModule_AddObject(m, "WITH_PYMALLOC", Py_NewRef(v));
8207+
8208+
#ifdef USE_STACKCHECK
8209+
v = Py_True;
8210+
#else
8211+
v = Py_False;
8212+
#endif
8213+
PyModule_AddObject(m, "USE_STACKCHECK", Py_NewRef(v));
82088214

82098215
TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
82108216
Py_INCREF(TestError);

0 commit comments

Comments
 (0)