Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-116303: Skip test module dependent tests if test modules are unavailable #117341

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,10 @@ def requires_limited_api(test):
return unittest.skip('needs _testcapi and _testlimitedcapi modules')(test)
return test


TEST_MODULES_ENABLED = sysconfig.get_config_var('TEST_MODULES') == 'yes'


def requires_specialization(test):
return unittest.skipUnless(
_opcode.ENABLE_SPECIALIZATION, "requires specialization")(test)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def test_excepthook(self):
)

def test_unraisablehook(self):
import_helper.import_module("_testcapi")
returncode, events, stderr = self.run_python("test_unraisablehook")
if returncode:
self.fail(stderr)
Expand Down
81 changes: 44 additions & 37 deletions Lib/test/test_call.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from test.support import (cpython_only, is_wasi, requires_limited_api, Py_DEBUG,
set_recursion_limit, skip_on_s390x)
set_recursion_limit, skip_on_s390x, import_helper)
try:
import _testcapi
except ImportError:
Expand Down Expand Up @@ -244,6 +244,7 @@ def test_module_not_callable_suggestion(self):
self.assertRaisesRegex(TypeError, msg, mod)


@unittest.skipIf(_testcapi is None, "requires _testcapi")
class TestCallingConventions(unittest.TestCase):
"""Test calling using various C calling conventions (METH_*) from Python

Expand Down Expand Up @@ -441,6 +442,7 @@ def static_method():

NULL_OR_EMPTY = object()


class FastCallTests(unittest.TestCase):
"""Test calling using various callables from C
"""
Expand Down Expand Up @@ -484,49 +486,51 @@ class FastCallTests(unittest.TestCase):
]

# Add all the calling conventions and variants of C callables
_instance = _testcapi.MethInstance()
for obj, expected_self in (
(_testcapi, _testcapi), # module-level function
(_instance, _instance), # bound method
(_testcapi.MethClass, _testcapi.MethClass), # class method on class
(_testcapi.MethClass(), _testcapi.MethClass), # class method on inst.
(_testcapi.MethStatic, None), # static method
):
CALLS_POSARGS.extend([
(obj.meth_varargs, (1, 2), (expected_self, (1, 2))),
(obj.meth_varargs_keywords,
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
(obj.meth_fastcall, (1, 2), (expected_self, (1, 2))),
(obj.meth_fastcall, (), (expected_self, ())),
(obj.meth_fastcall_keywords,
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
(obj.meth_fastcall_keywords,
(), (expected_self, (), NULL_OR_EMPTY)),
(obj.meth_noargs, (), expected_self),
(obj.meth_o, (123, ), (expected_self, 123)),
])

CALLS_KWARGS.extend([
(obj.meth_varargs_keywords,
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
(obj.meth_varargs_keywords,
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
(obj.meth_varargs_keywords,
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
(obj.meth_fastcall_keywords,
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
(obj.meth_fastcall_keywords,
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
(obj.meth_fastcall_keywords,
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
])
if _testcapi:
_instance = _testcapi.MethInstance()
for obj, expected_self in (
(_testcapi, _testcapi), # module-level function
(_instance, _instance), # bound method
(_testcapi.MethClass, _testcapi.MethClass), # class method on class
(_testcapi.MethClass(), _testcapi.MethClass), # class method on inst.
(_testcapi.MethStatic, None), # static method
):
CALLS_POSARGS.extend([
(obj.meth_varargs, (1, 2), (expected_self, (1, 2))),
(obj.meth_varargs_keywords,
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
(obj.meth_fastcall, (1, 2), (expected_self, (1, 2))),
(obj.meth_fastcall, (), (expected_self, ())),
(obj.meth_fastcall_keywords,
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
(obj.meth_fastcall_keywords,
(), (expected_self, (), NULL_OR_EMPTY)),
(obj.meth_noargs, (), expected_self),
(obj.meth_o, (123, ), (expected_self, 123)),
])

CALLS_KWARGS.extend([
(obj.meth_varargs_keywords,
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
(obj.meth_varargs_keywords,
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
(obj.meth_varargs_keywords,
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
(obj.meth_fastcall_keywords,
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
(obj.meth_fastcall_keywords,
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
(obj.meth_fastcall_keywords,
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
])

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

@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_vectorcall_dict(self):
# Test PyObject_VectorcallDict()

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

@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_vectorcall(self):
# Test PyObject_Vectorcall()

Expand Down Expand Up @@ -610,6 +615,7 @@ def testfunction_kw(self, *, kw):
ADAPTIVE_WARMUP_DELAY = 2


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

def test_method_descriptor_flag(self):
Expand Down Expand Up @@ -1022,6 +1028,7 @@ class TestRecursion(unittest.TestCase):

@skip_on_s390x
@unittest.skipIf(is_wasi and Py_DEBUG, "requires deep stack")
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_super_deep(self):

def recurse(n):
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_capi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import os
import unittest
from test.support import load_package_tests
from test.support import TEST_MODULES_ENABLED


if not TEST_MODULES_ENABLED:
raise unittest.SkipTest("requires test modules")


def load_tests(*args):
return load_package_tests(os.path.dirname(__file__), *args)
7 changes: 3 additions & 4 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@
check_impl_detail, requires_debug_ranges,
gc_collect)
from test.support.script_helper import assert_python_ok
from test.support import threading_helper
from test.support.bytecode_helper import (BytecodeTestCase,
instructions_with_positions)
from test.support import threading_helper, import_helper
from test.support.bytecode_helper import instructions_with_positions
from opcode import opmap, opname
COPY_FREE_VARS = opmap['COPY_FREE_VARS']

Expand Down Expand Up @@ -176,7 +175,7 @@ class CodeTest(unittest.TestCase):

@cpython_only
def test_newempty(self):
import _testcapi
_testcapi = import_helper.import_module("_testcapi")
co = _testcapi.code_newempty("filename", "funcname", 15)
self.assertEqual(co.co_filename, "filename")
self.assertEqual(co.co_name, "funcname")
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from test.support import import_helper
from test.support import warnings_helper
from test.support.script_helper import assert_python_ok
try:
import _testcapi
except ImportError:
_testcapi = None


class AsyncYieldFrom:
Expand Down Expand Up @@ -2445,6 +2449,7 @@ def test_unawaited_warning_during_shutdown(self):


@support.cpython_only
@unittest.skipIf(_testcapi is None, "requires _testcapi")
class CAPITest(unittest.TestCase):

def test_tp_await_1(self):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_as_parameter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import _ctypes_test
import ctypes
import unittest
from ctypes import (Structure, CDLL, CFUNCTYPE,
POINTER, pointer, byref,
c_short, c_int, c_long, c_longlong,
c_byte, c_wchar, c_float, c_double,
ArgumentError)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


dll = CDLL(_ctypes_test.__file__)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_bitfields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ctypes_test
import os
import unittest
from ctypes import (CDLL, Structure, sizeof, POINTER, byref, alignment,
Expand All @@ -7,6 +6,8 @@
c_uint32, c_uint64,
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
from test import support
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


class BITS(Structure):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ctypes_test
import ctypes
import functools
import gc
Expand All @@ -14,6 +13,8 @@
c_float, c_double, c_longdouble, py_object)
from ctypes.util import find_library
from test import support
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


class Callbacks(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_cfuncs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import _ctypes_test
import ctypes
import unittest
from ctypes import (CDLL,
c_byte, c_ubyte, c_char,
c_short, c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong, c_ulonglong,
c_float, c_double, c_longdouble)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


class CFunctions(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_checkretval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import _ctypes_test
import ctypes
import unittest
from ctypes import CDLL, c_int
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


class CHECKED(c_int):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_funcptr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _ctypes_test
import ctypes
import unittest
from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr,
c_void_p, c_char_p, c_char, c_int, c_uint, c_long)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")
from ._support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
Py_TPFLAGS_IMMUTABLETYPE)

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ctypes_test
import ctypes
import sys
import unittest
Expand All @@ -7,6 +6,8 @@
c_char, c_wchar, c_byte, c_char_p, c_wchar_p,
c_short, c_int, c_long, c_longlong, c_void_p,
c_float, c_double, c_longdouble)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")
from _ctypes import _Pointer, _SimpleCData


Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_libc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _ctypes_test
import math
import unittest
from ctypes import (CDLL, CFUNCTYPE, POINTER, create_string_buffer, sizeof,
c_void_p, c_char, c_int, c_double, c_size_t)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


lib = CDLL(_ctypes_test.__file__)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_loading.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ctypes
import _ctypes_test
import ctypes
import os
import shutil
Expand All @@ -10,6 +9,7 @@
from ctypes import CDLL, cdll, addressof, c_void_p, c_char_p
from ctypes.util import find_library
from test.support import import_helper, os_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


libc_name = None
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_parameters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ctypes_test
import unittest
import test.support
from ctypes import (CDLL, PyDLL, ArgumentError,
Expand All @@ -14,6 +13,8 @@
c_long, c_ulong,
c_longlong, c_ulonglong,
c_float, c_double, c_longdouble)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


class SimpleTypesTestCase(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_pickling.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import _ctypes_test
import pickle
import unittest
from ctypes import (CDLL, Structure, CFUNCTYPE, pointer,
c_void_p, c_char_p, c_wchar_p,
c_char, c_wchar, c_int, c_double)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


dll = CDLL(_ctypes_test.__file__)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_pointers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ctypes_test
import array
import ctypes
import sys
Expand All @@ -10,6 +9,8 @@
c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong, c_ulonglong,
c_float, c_double)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")
from ._support import (_CData, PyCPointerType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
Py_TPFLAGS_IMMUTABLETYPE)

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_prototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
#
# In this case, there would have to be an additional reference to the argument...

import _ctypes_test
import unittest
from ctypes import (CDLL, CFUNCTYPE, POINTER, ArgumentError,
pointer, byref, sizeof, addressof, create_string_buffer,
c_void_p, c_char_p, c_wchar_p, c_char, c_wchar,
c_short, c_int, c_long, c_longlong, c_double)
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


testdll = CDLL(_ctypes_test.__file__)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_refcounts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import _ctypes_test
import ctypes
import gc
import sys
import unittest
from test import support
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


MyCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_returnfuncptrs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ctypes_test
import unittest
from ctypes import CDLL, CFUNCTYPE, ArgumentError, c_char_p, c_void_p, c_char
from test.support import import_helper
_ctypes_test = import_helper.import_module("_ctypes_test")


class ReturnFuncPtrTestCase(unittest.TestCase):
Expand Down