Skip to content

Commit ea94b3b

Browse files
gh-116303: Skip test module dependent tests if test modules are unavailable (#117341)
1 parent 2ec6bb4 commit ea94b3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+256
-124
lines changed

Lib/test/support/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,10 @@ def requires_limited_api(test):
11681168
return unittest.skip('needs _testcapi and _testlimitedcapi modules')(test)
11691169
return test
11701170

1171+
1172+
TEST_MODULES_ENABLED = sysconfig.get_config_var('TEST_MODULES') == 'yes'
1173+
1174+
11711175
def requires_specialization(test):
11721176
return unittest.skipUnless(
11731177
_opcode.ENABLE_SPECIALIZATION, "requires specialization")(test)

Lib/test/test_audit.py

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def test_excepthook(self):
8989
)
9090

9191
def test_unraisablehook(self):
92+
import_helper.import_module("_testcapi")
9293
returncode, events, stderr = self.run_python("test_unraisablehook")
9394
if returncode:
9495
self.fail(stderr)

Lib/test/test_call.py

+44-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from test.support import (cpython_only, is_wasi, requires_limited_api, Py_DEBUG,
3-
set_recursion_limit, skip_on_s390x)
3+
set_recursion_limit, skip_on_s390x, import_helper)
44
try:
55
import _testcapi
66
except ImportError:
@@ -244,6 +244,7 @@ def test_module_not_callable_suggestion(self):
244244
self.assertRaisesRegex(TypeError, msg, mod)
245245

246246

247+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
247248
class TestCallingConventions(unittest.TestCase):
248249
"""Test calling using various C calling conventions (METH_*) from Python
249250
@@ -441,6 +442,7 @@ def static_method():
441442

442443
NULL_OR_EMPTY = object()
443444

445+
444446
class FastCallTests(unittest.TestCase):
445447
"""Test calling using various callables from C
446448
"""
@@ -484,49 +486,51 @@ class FastCallTests(unittest.TestCase):
484486
]
485487

486488
# Add all the calling conventions and variants of C callables
487-
_instance = _testcapi.MethInstance()
488-
for obj, expected_self in (
489-
(_testcapi, _testcapi), # module-level function
490-
(_instance, _instance), # bound method
491-
(_testcapi.MethClass, _testcapi.MethClass), # class method on class
492-
(_testcapi.MethClass(), _testcapi.MethClass), # class method on inst.
493-
(_testcapi.MethStatic, None), # static method
494-
):
495-
CALLS_POSARGS.extend([
496-
(obj.meth_varargs, (1, 2), (expected_self, (1, 2))),
497-
(obj.meth_varargs_keywords,
498-
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
499-
(obj.meth_fastcall, (1, 2), (expected_self, (1, 2))),
500-
(obj.meth_fastcall, (), (expected_self, ())),
501-
(obj.meth_fastcall_keywords,
502-
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
503-
(obj.meth_fastcall_keywords,
504-
(), (expected_self, (), NULL_OR_EMPTY)),
505-
(obj.meth_noargs, (), expected_self),
506-
(obj.meth_o, (123, ), (expected_self, 123)),
507-
])
508-
509-
CALLS_KWARGS.extend([
510-
(obj.meth_varargs_keywords,
511-
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
512-
(obj.meth_varargs_keywords,
513-
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
514-
(obj.meth_varargs_keywords,
515-
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
516-
(obj.meth_fastcall_keywords,
517-
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
518-
(obj.meth_fastcall_keywords,
519-
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
520-
(obj.meth_fastcall_keywords,
521-
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
522-
])
489+
if _testcapi:
490+
_instance = _testcapi.MethInstance()
491+
for obj, expected_self in (
492+
(_testcapi, _testcapi), # module-level function
493+
(_instance, _instance), # bound method
494+
(_testcapi.MethClass, _testcapi.MethClass), # class method on class
495+
(_testcapi.MethClass(), _testcapi.MethClass), # class method on inst.
496+
(_testcapi.MethStatic, None), # static method
497+
):
498+
CALLS_POSARGS.extend([
499+
(obj.meth_varargs, (1, 2), (expected_self, (1, 2))),
500+
(obj.meth_varargs_keywords,
501+
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
502+
(obj.meth_fastcall, (1, 2), (expected_self, (1, 2))),
503+
(obj.meth_fastcall, (), (expected_self, ())),
504+
(obj.meth_fastcall_keywords,
505+
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
506+
(obj.meth_fastcall_keywords,
507+
(), (expected_self, (), NULL_OR_EMPTY)),
508+
(obj.meth_noargs, (), expected_self),
509+
(obj.meth_o, (123, ), (expected_self, 123)),
510+
])
511+
512+
CALLS_KWARGS.extend([
513+
(obj.meth_varargs_keywords,
514+
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
515+
(obj.meth_varargs_keywords,
516+
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
517+
(obj.meth_varargs_keywords,
518+
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
519+
(obj.meth_fastcall_keywords,
520+
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
521+
(obj.meth_fastcall_keywords,
522+
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
523+
(obj.meth_fastcall_keywords,
524+
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
525+
])
523526

524527
def check_result(self, result, expected):
525528
if isinstance(expected, tuple) and expected[-1] is NULL_OR_EMPTY:
526529
if result[-1] in ({}, None):
527530
expected = (*expected[:-1], result[-1])
528531
self.assertEqual(result, expected)
529532

533+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
530534
def test_vectorcall_dict(self):
531535
# Test PyObject_VectorcallDict()
532536

@@ -546,6 +550,7 @@ def test_vectorcall_dict(self):
546550
result = _testcapi.pyobject_fastcalldict(func, args, kwargs)
547551
self.check_result(result, expected)
548552

553+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
549554
def test_vectorcall(self):
550555
# Test PyObject_Vectorcall()
551556

@@ -610,6 +615,7 @@ def testfunction_kw(self, *, kw):
610615
ADAPTIVE_WARMUP_DELAY = 2
611616

612617

618+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
613619
class TestPEP590(unittest.TestCase):
614620

615621
def test_method_descriptor_flag(self):
@@ -1022,6 +1028,7 @@ class TestRecursion(unittest.TestCase):
10221028

10231029
@skip_on_s390x
10241030
@unittest.skipIf(is_wasi and Py_DEBUG, "requires deep stack")
1031+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
10251032
def test_super_deep(self):
10261033

10271034
def recurse(n):

Lib/test/test_capi/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import os
2+
import unittest
23
from test.support import load_package_tests
4+
from test.support import TEST_MODULES_ENABLED
5+
6+
7+
if not TEST_MODULES_ENABLED:
8+
raise unittest.SkipTest("requires test modules")
9+
310

411
def load_tests(*args):
512
return load_package_tests(os.path.dirname(__file__), *args)

Lib/test/test_code.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@
143143
check_impl_detail, requires_debug_ranges,
144144
gc_collect)
145145
from test.support.script_helper import assert_python_ok
146-
from test.support import threading_helper
147-
from test.support.bytecode_helper import (BytecodeTestCase,
148-
instructions_with_positions)
146+
from test.support import threading_helper, import_helper
147+
from test.support.bytecode_helper import instructions_with_positions
149148
from opcode import opmap, opname
150149
COPY_FREE_VARS = opmap['COPY_FREE_VARS']
151150

@@ -176,7 +175,7 @@ class CodeTest(unittest.TestCase):
176175

177176
@cpython_only
178177
def test_newempty(self):
179-
import _testcapi
178+
_testcapi = import_helper.import_module("_testcapi")
180179
co = _testcapi.code_newempty("filename", "funcname", 15)
181180
self.assertEqual(co.co_filename, "filename")
182181
self.assertEqual(co.co_name, "funcname")

Lib/test/test_coroutines.py

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from test.support import import_helper
1212
from test.support import warnings_helper
1313
from test.support.script_helper import assert_python_ok
14+
try:
15+
import _testcapi
16+
except ImportError:
17+
_testcapi = None
1418

1519

1620
class AsyncYieldFrom:
@@ -2445,6 +2449,7 @@ def test_unawaited_warning_during_shutdown(self):
24452449

24462450

24472451
@support.cpython_only
2452+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
24482453
class CAPITest(unittest.TestCase):
24492454

24502455
def test_tp_await_1(self):

Lib/test/test_ctypes/test_as_parameter.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import _ctypes_test
21
import ctypes
32
import unittest
43
from ctypes import (Structure, CDLL, CFUNCTYPE,
54
POINTER, pointer, byref,
65
c_short, c_int, c_long, c_longlong,
76
c_byte, c_wchar, c_float, c_double,
87
ArgumentError)
8+
from test.support import import_helper
9+
_ctypes_test = import_helper.import_module("_ctypes_test")
910

1011

1112
dll = CDLL(_ctypes_test.__file__)

Lib/test/test_ctypes/test_bitfields.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ctypes_test
21
import os
32
import unittest
43
from ctypes import (CDLL, Structure, sizeof, POINTER, byref, alignment,
@@ -7,6 +6,8 @@
76
c_uint32, c_uint64,
87
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
98
from test import support
9+
from test.support import import_helper
10+
_ctypes_test = import_helper.import_module("_ctypes_test")
1011

1112

1213
class BITS(Structure):

Lib/test/test_ctypes/test_callbacks.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ctypes_test
21
import ctypes
32
import functools
43
import gc
@@ -14,6 +13,8 @@
1413
c_float, c_double, c_longdouble, py_object)
1514
from ctypes.util import find_library
1615
from test import support
16+
from test.support import import_helper
17+
_ctypes_test = import_helper.import_module("_ctypes_test")
1718

1819

1920
class Callbacks(unittest.TestCase):

Lib/test/test_ctypes/test_cfuncs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import _ctypes_test
21
import ctypes
32
import unittest
43
from ctypes import (CDLL,
54
c_byte, c_ubyte, c_char,
65
c_short, c_ushort, c_int, c_uint,
76
c_long, c_ulong, c_longlong, c_ulonglong,
87
c_float, c_double, c_longdouble)
8+
from test.support import import_helper
9+
_ctypes_test = import_helper.import_module("_ctypes_test")
910

1011

1112
class CFunctions(unittest.TestCase):

Lib/test/test_ctypes/test_checkretval.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import _ctypes_test
21
import ctypes
32
import unittest
43
from ctypes import CDLL, c_int
4+
from test.support import import_helper
5+
_ctypes_test = import_helper.import_module("_ctypes_test")
56

67

78
class CHECKED(c_int):

Lib/test/test_ctypes/test_funcptr.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import _ctypes_test
21
import ctypes
32
import unittest
43
from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr,
54
c_void_p, c_char_p, c_char, c_int, c_uint, c_long)
5+
from test.support import import_helper
6+
_ctypes_test = import_helper.import_module("_ctypes_test")
67
from ._support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
78
Py_TPFLAGS_IMMUTABLETYPE)
89

Lib/test/test_ctypes/test_functions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ctypes_test
21
import ctypes
32
import sys
43
import unittest
@@ -7,6 +6,8 @@
76
c_char, c_wchar, c_byte, c_char_p, c_wchar_p,
87
c_short, c_int, c_long, c_longlong, c_void_p,
98
c_float, c_double, c_longdouble)
9+
from test.support import import_helper
10+
_ctypes_test = import_helper.import_module("_ctypes_test")
1011
from _ctypes import _Pointer, _SimpleCData
1112

1213

Lib/test/test_ctypes/test_libc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import _ctypes_test
21
import math
32
import unittest
43
from ctypes import (CDLL, CFUNCTYPE, POINTER, create_string_buffer, sizeof,
54
c_void_p, c_char, c_int, c_double, c_size_t)
5+
from test.support import import_helper
6+
_ctypes_test = import_helper.import_module("_ctypes_test")
67

78

89
lib = CDLL(_ctypes_test.__file__)

Lib/test/test_ctypes/test_loading.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _ctypes
2-
import _ctypes_test
32
import ctypes
43
import os
54
import shutil
@@ -10,6 +9,7 @@
109
from ctypes import CDLL, cdll, addressof, c_void_p, c_char_p
1110
from ctypes.util import find_library
1211
from test.support import import_helper, os_helper
12+
_ctypes_test = import_helper.import_module("_ctypes_test")
1313

1414

1515
libc_name = None

Lib/test/test_ctypes/test_parameters.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ctypes_test
21
import unittest
32
import test.support
43
from ctypes import (CDLL, PyDLL, ArgumentError,
@@ -14,6 +13,8 @@
1413
c_long, c_ulong,
1514
c_longlong, c_ulonglong,
1615
c_float, c_double, c_longdouble)
16+
from test.support import import_helper
17+
_ctypes_test = import_helper.import_module("_ctypes_test")
1718

1819

1920
class SimpleTypesTestCase(unittest.TestCase):

Lib/test/test_ctypes/test_pickling.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import _ctypes_test
21
import pickle
32
import unittest
43
from ctypes import (CDLL, Structure, CFUNCTYPE, pointer,
54
c_void_p, c_char_p, c_wchar_p,
65
c_char, c_wchar, c_int, c_double)
6+
from test.support import import_helper
7+
_ctypes_test = import_helper.import_module("_ctypes_test")
78

89

910
dll = CDLL(_ctypes_test.__file__)

Lib/test/test_ctypes/test_pointers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ctypes_test
21
import array
32
import ctypes
43
import sys
@@ -10,6 +9,8 @@
109
c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
1110
c_long, c_ulong, c_longlong, c_ulonglong,
1211
c_float, c_double)
12+
from test.support import import_helper
13+
_ctypes_test = import_helper.import_module("_ctypes_test")
1314
from ._support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
1415
Py_TPFLAGS_IMMUTABLETYPE)
1516

Lib/test/test_ctypes/test_prototypes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
#
1919
# In this case, there would have to be an additional reference to the argument...
2020

21-
import _ctypes_test
2221
import unittest
2322
from ctypes import (CDLL, CFUNCTYPE, POINTER, ArgumentError,
2423
pointer, byref, sizeof, addressof, create_string_buffer,
2524
c_void_p, c_char_p, c_wchar_p, c_char, c_wchar,
2625
c_short, c_int, c_long, c_longlong, c_double)
26+
from test.support import import_helper
27+
_ctypes_test = import_helper.import_module("_ctypes_test")
2728

2829

2930
testdll = CDLL(_ctypes_test.__file__)

Lib/test/test_ctypes/test_refcounts.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import _ctypes_test
21
import ctypes
32
import gc
43
import sys
54
import unittest
65
from test import support
6+
from test.support import import_helper
7+
_ctypes_test = import_helper.import_module("_ctypes_test")
78

89

910
MyCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)

Lib/test/test_ctypes/test_returnfuncptrs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import _ctypes_test
21
import unittest
32
from ctypes import CDLL, CFUNCTYPE, ArgumentError, c_char_p, c_void_p, c_char
3+
from test.support import import_helper
4+
_ctypes_test = import_helper.import_module("_ctypes_test")
45

56

67
class ReturnFuncPtrTestCase(unittest.TestCase):

0 commit comments

Comments
 (0)