]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-108851: Fix support.get_recursion_available() for USE_STACKCHECK (#110127)
authorVictor Stinner <vstinner@python.org>
Fri, 29 Sep 2023 21:54:46 +0000 (23:54 +0200)
committerGitHub <noreply@github.com>
Fri, 29 Sep 2023 21:54:46 +0000 (23:54 +0200)
Add _testcapi.USE_STACKCHECK.

USE_STACKCHECK on using on Windows 32-bit.

Lib/test/support/__init__.py
Lib/test/test_support.py
Modules/_testcapimodule.c

index 2c6b22fdee5a21a7cd1d84e741f0593a4e2fc74f..2e6518cf241f14ebffc85f54cc97f2874dbf7ae2 100644 (file)
@@ -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):
index 9894c2647d7c93db9e0d3fbe9446fda07f9dcea8..2efdbd22d90e2e9259ca276d97570a02941f879c 100644 (file)
@@ -704,6 +704,10 @@ class TestSupport(unittest.TestCase):
         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:
@@ -728,19 +732,24 @@ class TestSupport(unittest.TestCase):
                 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)
 
index 5c00b48001a9190e127930f9cf6d5a2f5772861f..2f1801f781017d159ddf6a04ecd3ce512de0f3a8 100644 (file)
@@ -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);