8000 gh-116303: Skip tests if C recursion limit is unavailable (GH-117368) · python/cpython@ca62ffd · GitHub
[go: up one dir, main page]

Skip to content

Commit ca62ffd

Browse files
gh-116303: Skip tests if C recursion limit is unavailable (GH-117368)
The test suite fetches the C recursion limit from the _testcapi extension module. Test extension modules can be disabled using the --disable-test-modules configure option.
1 parent 9a12f5d commit ca62ffd

14 files changed

+32
-35
lines changed

Lib/test/list_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from functools import cmp_to_key
77

88
from test import seq_tests
9-
from test.support import ALWAYS_EQ, NEVER_EQ, Py_C_RECURSION_LIMIT
9+
from test.support import ALWAYS_EQ, NEVER_EQ, get_c_recursion_limit
1010

1111

1212
class CommonTest(seq_tests.CommonTest):
@@ -61,7 +61,7 @@ def test_repr(self):
6161

6262
def test_repr_deep(self):
6363
a = self.type2test([])
64-
for i in range(Py_C_RECURSION_LIMIT + 1):
64+
for i in range(get_c_recursion_limit() + 1):
6565
a = self.type2test([a])
6666
self.assertRaises(RecursionError, repr, a)
6767

Lib/test/mapping_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# tests common to dict and UserDict
22
import unittest
33
import collections
4-
from test.support import Py_C_RECURSION_LIMIT
4+
from test.support import get_c_recursion_limit
55

66

77
class BasicTestMappingProtocol(unittest.TestCase):
@@ -624,7 +624,7 @@ def __repr__(self):
624624

625625
def test_repr_deep(self):
626626
d = self._empty_mapping()
627-
for i in range(Py_C_RECURSION_LIMIT + 1):
627+
for i in range(get_c_recursion_limit() + 1):
628628
d0 = d
629629
d = self._empty_mapping()
630630
d[1] = d0

Lib/test/support/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"run_with_tz", "PGO", "missing_compiler 628C _executable",
5757
"ALWAYS_EQ", "NEVER_EQ", "LARGEST", "SMALLEST",
5858
"LOOPBACK_TIMEOUT", "INTERNET_TIMEOUT", "SHORT_TIMEOUT", "LONG_TIMEOUT",
59-
"Py_DEBUG", "EXCEEDS_RECURSION_LIMIT", "Py_C_RECURSION_LIMIT",
59+
"Py_DEBUG", "exceeds_recursion_limit", "get_c_recursion_limit",
6060
"skip_on_s390x",
6161
"without_optimizer",
6262
]
@@ -2490,22 +2490,18 @@ def adjust_int_max_str_digits(max_digits):
24902490
sys.set_int_max_str_digits(current)
24912491

24922492

2493-
def _get_c_recursion_limit():
2493+
def get_c_recursion_limit():
24942494
try:
24952495
import _testcapi
24962496
return _testcapi.Py_C_RECURSION_LIMIT
2497-
except (ImportError, AttributeError):
2498-
# Originally taken from Include/cpython/pystate.h .
2499-
if sys.platform == 'win32':
2500-
return 4000
2501-
else:
2502-
return 10000
2497+
except ImportError:
2498+
raise unittest.SkipTest('requires _testcapi')
2499+
25032500

2504-
# The default C recursion limit.
2505-
Py_C_RECURSION_LIMIT = _get_c_recursion_limit()
2501+
def exceeds_recursion_limit():
2502+
"""For recursion tests, easily exceeds default recursion limit."""
2503+
return get_c_recursion_limit() * 3
25062504

2507-
#For recursion tests, easily exceeds default recursion limit
2508-
EXCEEDS_RECURSION_LIMIT = Py_C_RECURSION_LIMIT * 3
25092505

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

Lib/test/test_ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,9 +1153,9 @@ def next(self):
11531153

11541154
@support.cpython_only
11551155
def test_ast_recursion_limit(self):
1156-
fail_depth = support.EXCEEDS_RECURSION_LIMIT
1156+
fail_depth = support.exceeds_recursion_limit()
11571157
crash_depth = 100_000
1158-
success_depth = int(support.Py_C_RECURSION_LIMIT * 0.8)
1158+
success_depth = int(support.get_c_recursion_limit() * 0.8)
11591159
if _testinternalcapi is not None:
11601160
remaining = _testinternalcapi.get_c_recursion_remaining()
11611161
success_depth = min(success_depth, remaining)

Lib/test/test_collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def test_odd_sizes(self):
542542
self.assertEqual(Dot(1)._replace(d=999), (999,))
543543
self.assertEqual(Dot(1)._fields, ('d',))
544544

545-
n = support.EXCEEDS_RECURSION_LIMIT
545+
n = support.exceeds_recursion_limit()
546546
names = list(set(''.join([choice(string.ascii_letters)
547547
for j in range(10)]) for i in range(n)))
548548
n = len(names)

Lib/test/test_compile.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import warnings
1414
from test import support
1515
from test.support import (script_helper, requires_debug_ranges,
16-
requires_specialization, Py_C_RECURSION_LIMIT)
16+
requires_specialization, get_c_recursion_limit)
1717
from test.support.bytecode_helper import instructions_with_positions
1818
from test.support.os_helper import FakePath
1919

@@ -114,7 +114,7 @@ def __getitem__(self, key):
114114

115115
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
116116
def test_extended_arg(self):
117-
repeat = int(Py_C_RECURSION_LIMIT * 0.9)
117+
repeat = int(get_c_recursion_limit() * 0.9)
118118
longexpr = 'x = x or ' + '-x' * repeat
119119
g = {}
120120
code = textwrap.dedent('''
@@ -634,9 +634,10 @@ def test_yet_more_evil_still_undecodable(self):
634634
@unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
635635
def test_compiler_recursion_limit(self):
636636
# Expected limit is Py_C_RECURSION_LIMIT
637-
fail_depth = Py_C_RECURSION_LIMIT + 1
638-
crash_depth = Py_C_RECURSION_LIMIT * 100
639-
success_depth = int(Py_C_RECURSION_LIMIT * 0.8)
637+
limit = get_c_recursion_limit()
638+
fail_depth = limit + 1
639+
crash_depth = limit * 100
640+
success_depth = int(limit * 0.8)
640641

641642
def check_limit(prefix, repeated, mode="single"):
642643
expect_ok = prefix + repeated * success_depth

Lib/test/test_dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import unittest
99
import weakref
1010
from test import support
11-
from test.support import import_helper, Py_C_RECURSION_LIMIT
11+
from test.support import import_helper, get_c_recursion_limit
1212

1313

1414
class DictTest(unittest.TestCase):
@@ -596,7 +596,7 @@ def __repr__(self):
596596

597597
def test_repr_deep(self):
598598
d = {}
599-
for i in range(Py_C_RECURSION_LIMIT + 1):
599+
for i in range(get_c_recursion_limit() + 1):
600600
d = {1: d}
601601
self.assertRaises(RecursionError, repr, d)
602602

Lib/test/test_dictviews.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import copy
33
import pickle
44
import unittest
5-
from test.support import Py_C_RECURSION_LIMIT
5+
from test.support import get_c_recursion_limit
66

77
class DictSetTest(unittest.TestCase):
88

@@ -279,7 +279,7 @@ def test_recursive_repr(self):
279279

280280
def test_deeply_nested_repr(self):
281281
d = {}
282-
for i in range(Py_C_RECURSION_LIMIT//2 + 100):
282+
for i in range(get_c_recursion_limit()//2 + 100):
283283
d = {42: d.values()}
284284
self.assertRaises(RecursionError, repr, d)
285285

Lib/test/test_exception_group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import collections.abc
22
import types
33
import unittest
4-
from test.support import Py_C_RECURSION_LIMIT
4+
from test.support import get_c_recursion_limit
55

66
class TestExceptionGroupTypeHierarchy(unittest.TestCase):
77
def test_exception_group_types(self):
@@ -460,7 +460,7 @@ def test_basics_split_by_predicate__match(self):
460460
class DeepRecursionInSplitAndSubgroup(unittest.TestCase):
461461
def make_deep_eg(self):
462462
e = TypeError(1)
463-
for i in range(Py_C_RECURSION_LIMIT + 1):
463+
for i in range(get_c_recursion_limit() + 1):
464464
e = ExceptionGroup('eg', [e])
465465
return e
466466

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ def gen():
14241424
next(generator)
14251425
recursionlimit = sys.getrecursionlimit()
14261426
try:
1427-
recurse(support.EXCEEDS_RECURSION_LIMIT)
1427+
recurse(support.exceeds_recursion_limit())
14281428
finally:
14291429
sys.setrecursionlimit(recursionlimit)
14301430
print('Done.')

Lib/test/test_functools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ def fib(n):
18671867
return fib(n-1) + fib(n-2)
18681868

18691869
if not support.Py_DEBUG:
1870-
depth = support.Py_C_RECURSION_LIMIT*2//7
1870+
depth = support.get_c_recursion_limit()*2//7
18711871
with support.infinite_recursion():
18721872
fib(depth)
18731873
if self.module == c_functools:

Lib/test/test_isinstance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def blowstack(fxn, arg, compare_to):
352352
# Make sure that calling isinstance with a deeply nested tuple for its
353353
# argument will raise RecursionError eventually.
354354
tuple_arg = (compare_to,)
355-
for cnt in range(support.EXCEEDS_RECURSION_LIMIT):
355+
for cnt in range(support.exceeds_recursion_limit()):
356356
tuple_arg = (tuple_arg,)
357357
fxn(arg, tuple_arg)
358358

Lib/test/test_marshal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_code(self):
118118
def test_many_codeobjects(self):
119119
# Issue2957: bad recursion count on code objects
120120
# more than MAX_MARSHAL_STACK_DEPTH
121-
count = support.EXCEEDS_RECURSION_LIMIT
121+
count = support.exceeds_recursion_limit()
122122
codes = (ExceptionTestCase.test_exceptions.__code__,) * count
123123
marshal.loads(marshal.dumps(codes))
124124

Lib/test/test_sys_settrace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3039,7 +3039,7 @@ def test_trace_unpack_long_sequence(self):
30393039

30403040
def test_trace_lots_of_globals(self):
30413041

3042-
count = min(1000, int(support.Py_C_RECURSION_LIMIT * 0.8))
3042+
count = min(1000, int(support.get_c_recursion_limit() * 0.8))
30433043

30443044
code = """if 1:
30453045
def f():

0 commit comments

Comments
 (0)
0