Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] gh-108851: Fix support.get_recursion_available() for USE_STACKCHECK #110127

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,16 @@ def get_recursion_available():
"""
limit = sys.getrecursionlimit()
depth = get_recursion_depth()
return limit - depth

try:
from _testcapi import USE_STACKCHECK
except ImportError:
USE_STACKCHECK = False

if USE_STACKCHECK:
return max(limit - depth - 1, 0)
else:
return limit - depth

@contextlib.contextmanager
def set_recursion_limit(limit):
Expand Down
17 changes: 13 additions & 4 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ def test_get_recursion_depth(self):
code = textwrap.dedent("""
from test import support
import sys
try:
from _testcapi import USE_STACKCHECK
except ImportError:
USE_STACKCHECK = False

def check(cond):
if not cond:
Expand All @@ -728,19 +732,24 @@ def test_recursive(depth, limit):
check(get_depth == depth)
test_recursive(depth + 1, limit)

if USE_STACKCHECK:
# f-string consumes 2 frames and -1 for USE_STACKCHECK
IGNORE = 3
else:
# f-string consumes 2 frames
IGNORE = 2

# depth up to 25
with support.infinite_recursion(max_depth=25):
limit = sys.getrecursionlimit()
print(f"test with sys.getrecursionlimit()={limit}")
# Use limit-2 since f-string seems to consume 2 frames.
test_recursive(2, limit - 2)
test_recursive(2, limit - IGNORE)

# depth up to 500
with support.infinite_recursion(max_depth=500):
limit = sys.getrecursionlimit()
print(f"test with sys.getrecursionlimit()={limit}")
# limit-2 since f-string seems to consume 2 frames
test_recursive(2, limit - 2)
test_recursive(2, limit - IGNORE)
""")
script_helper.assert_python_ok("-c", code)

Expand Down
10 changes: 8 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8203,8 +8203,14 @@ PyInit__testcapi(void)
#else
v = Py_False;
#endif
Py_INCREF(v);
PyModule_AddObject(m, "WITH_PYMALLOC", v);
PyModule_AddObject(m, "WITH_PYMALLOC", Py_NewRef(v));

#ifdef USE_STACKCHECK
v = Py_True;
#else
v = Py_False;
#endif
PyModule_AddObject(m, "USE_STACKCHECK", Py_NewRef(v));

TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
Py_INCREF(TestError);
Expand Down
Loading