Skip to content

Commit 07fb2b4

Browse files
committed
pythonGH-112215: Increase C recursion limit for non debug builds (pythonGH-113397)
1 parent 4259acd commit 07fb2b4

File tree

7 files changed

+35
-14
lines changed

7 files changed

+35
-14
lines changed

Include/cpython/pystate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ struct _ts {
254254
#ifndef C_RECURSION_LIMIT
255255
# ifdef __wasi__
256256
# define C_RECURSION_LIMIT 500
257+
#elif defined(__s390x__)
258+
# define Py_C_RECURSION_LIMIT 1200
257259
# else
258260
// This value is duplicated in Lib/test/support/__init__.py
259-
# define C_RECURSION_LIMIT 1500
261+
# define Py_C_RECURSION_LIMIT 8000
260262
# endif
261263
#endif
262264

Lib/test/support/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,11 +2113,6 @@ def set_recursion_limit(limit):
21132113
sys.setrecursionlimit(original_limit)
21142114

21152115
def infinite_recursion(max_depth=100):
2116-
"""Set a lower limit for tests that interact with infinite recursions
2117-
(e.g test_ast.ASTHelpers_Test.test_recursion_direct) since on some
2118-
debug windows builds, due to not enough functions being inlined the
2119-
stack size might not handle the default recursion limit (1000). See
2120-
bpo-11105 for details."""
21212116
if max_depth < 3:
21222117
raise ValueError("max_depth must be at least 3, got {max_depth}")
21232118
depth = get_recursion_depth()
@@ -2356,11 +2351,18 @@ def adjust_int_max_str_digits(max_digits):
23562351
finally:
23572352
sys.set_int_max_str_digits(current)
23582353

2359-
#For recursion tests, easily exceeds default recursion limit
2360-
EXCEEDS_RECURSION_LIMIT = 5000
23612354

2355+
def _get_c_recursion_limit():
2356+
try:
2357+
import _testcapi
2358+
return _testcapi.Py_C_RECURSION_LIMIT
2359+
except (ImportError, AttributeError):
2360+
# Originally taken from Include/cpython/pystate.h .
23622361
# The default C recursion limit (from Include/cpython/pystate.h).
2363-
C_RECURSION_LIMIT = 1500
2362+
C_RECURSION_LIMIT = 8000
2363+
2364+
#For recursion tests, easily exceeds default recursion limit
2365+
EXCEEDS_RECURSION_LIMIT = Py_C_RECURSION_LIMIT * 3
23642366

23652367
#Windows doesn't have os.uname() but it doesn't support s390x.
23662368
skip_on_s390x = unittest.skipIf(hasattr(os, 'uname') and os.uname().machine == 's390x',

Lib/test/test_functools.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,20 @@ def orig(): ...
18621862
self.assertEqual(str(Signature.from_callable(lru.cache_info)), '()')
18631863
self.assertEqual(str(Signature.from_callable(lru.cache_clear)), '()')
18641864

1865+
@support.skip_on_s390x
1866+
@unittest.skipIf(support.is_wasi, "WASI has limited C stack")
1867+
def test_lru_recursion(self):
1868+
1869+
@self.module.lru_cache
1870+
def fib(n):
1871+
if n <= 1:
1872+
return n
1873+
return fib(n-1) + fib(n-2)
1874+
1875+
if not support.Py_DEBUG:
1876+
with support.infinite_recursion():
1877+
fib(2500)
1878+
18651879

18661880
@py_functools.lru_cache()
18671881
def py_cached_func(x, y):

Lib/test/test_json/test_recursion.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def test_highly_nested_objects_encoding(self):
8585
for x in range(100000):
8686
l, d = [l], {'k':d}
8787
with self.assertRaises(RecursionError):
88-
with support.infinite_recursion():
88+
with support.infinite_recursion(5000):
8989
self.dumps(l)
9090
with self.assertRaises(RecursionError):
91-
with support.infinite_recursion():
91+
with support.infinite_recursion(5000):
9292
self.dumps(d)
9393

9494
def test_endless_recursion(self):
@@ -99,7 +99,7 @@ def default(self, o):
9999
return [o]
100100

101101
with self.assertRaises(RecursionError):
102-
with support.infinite_recursion():
102+
with support.infinite_recursion(1000):
103103
EndlessJSONEncoder(check_circular=False).encode(5j)
104104

105105

Lib/test/test_support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def recursive_function(depth):
633633
if depth:
634634
recursive_function(depth - 1)
635635

636-
for max_depth in (5, 25, 250):
636+
for max_depth in (5, 25, 250, 2500):
637637
with support.infinite_recursion(max_depth):
638638
available = support.get_recursion_available()
639639

Lib/test/test_xml_etree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,7 @@ def __eq__(self, o):
25352535
e.extend([ET.Element('bar')])
25362536
self.assertRaises(ValueError, e.remove, X('baz'))
25372537

2538-
@support.infinite_recursion(25)
2538+
@support.infinite_recursion()
25392539
def test_recursive_repr(self):
25402540
# Issue #25455
25412541
e = ET.Element('foo')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Increase the C recursion limit by a factor of 3 for non-debug builds, except
2+
for webassembly and s390 platforms which are unchanged. This mitigates some
3+
regressions in 3.12 with deep recursion mixing builtin (C) and Python code.

0 commit comments

Comments
 (0)