Skip to content

Commit

Permalink
Backport copying recursion limit from C to test.support
Browse files Browse the repository at this point in the history
Partially backport Python 3.13 changes that expose the C recursion
limit as _testcapi.Py_C_RECURSION_LIMIT and use them in test.support
rather than (incompletely) duplicating the values from header.  This
should fix test failures on platforms that were not correctly covered
in the Python code (e.g. SPARC).

The code is roughly based on parts of the following upstream commits:

45e09f9 pythonGH-112215: Increase C recursion limit for non debug builds (pythonGH-113397)
7e135a4 pythongh-111520: Integrate the Tier 2 interpreter in the Tier 1 interpreter (python#111428)
b0edf3b pythonGH-91079: Rename C_RECURSION_LIMIT to Py_C_RECURSION_LIMIT (python#108507)

Signed-off-by: Michał Górny <mgorny@gentoo.org>
  • Loading branch information
mgorny committed Nov 26, 2024
1 parent ad3daea commit f53bdc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
29 changes: 10 additions & 19 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2375,26 +2375,17 @@ def adjust_int_max_str_digits(max_digits):
finally:
sys.set_int_max_str_digits(current)

#For recursion tests, easily exceeds default recursion limit
EXCEEDS_RECURSION_LIMIT = 5000
def _get_c_recursion_limit():
try:
import _testcapi
return _testcapi.Py_C_RECURSION_LIMIT
except ImportError:
return 8000

# The default C recursion limit (from Include/cpython/pystate.h).
if Py_DEBUG:
if is_wasi:
C_RECURSION_LIMIT = 150
else:
C_RECURSION_LIMIT = 500
else:
if is_wasi:
C_RECURSION_LIMIT = 500
elif hasattr(os, 'uname') and os.uname().machine == 's390x':
C_RECURSION_LIMIT = 800
elif sys.platform.startswith('win'):
C_RECURSION_LIMIT = 3000
elif check_sanitizer(address=True):
C_RECURSION_LIMIT = 4000
else:
C_RECURSION_LIMIT = 10000
C_RECURSION_LIMIT = _get_c_recursion_limit()

#For recursion tests, easily exceeds default recursion limit
EXCEEDS_RECURSION_LIMIT = C_RECURSION_LIMIT * 3

#Windows doesn't have os.uname() but it doesn't support s390x.
skip_on_s390x = unittest.skipIf(hasattr(os, 'uname') and os.uname().machine == 's390x',
Expand Down
2 changes: 2 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3954,6 +3954,8 @@ PyInit__testcapi(void)
PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);

PyModule_AddIntConstant(m, "the_number_three", 3);
#define Py_C_RECURSION_LIMIT C_RECURSION_LIMIT
PyModule_AddIntMacro(m, Py_C_RECURSION_LIMIT);

if (PyModule_AddIntMacro(m, Py_single_input)) {
return NULL;
Expand Down

0 comments on commit f53bdc7

Please sign in to comment.