Skip to content

Commit cdc6df4

Browse files
Use import_helper iso. requires_limited_api
1 parent a9c6f62 commit cdc6df4

38 files changed

+159
-128
lines changed

Lib/test/support/bytecode_helper.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
import dis
55
import io
6-
from _testinternalcapi import compiler_codegen, optimize_cfg, assemble_code_object
6+
from test.support import import_helper
77

88
_UNSPECIFIED = object()
99

@@ -136,20 +136,23 @@ def complete_insts_info(self, insts):
136136
class CodegenTestCase(CompilationStepTestCase):
137137

138138
def generate_code(self, ast):
139-
insts, _ = compiler_codegen(ast, "my_file.py", 0)
139+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
140+
insts, _ = _testinternalcapi.compiler_codegen(ast, "my_file.py", 0)
140141
return insts
141142

142143

143144
class CfgOptimizationTestCase(CompilationStepTestCase):
144145

145146
def get_optimized(self, insts, consts, nlocals=0):
147+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
146148
insts = self.normalize_insts(insts)
147149
insts = self.complete_insts_info(insts)
148-
insts = optimize_cfg(insts, consts, nlocals)
150+
insts = _testinternalcapi.optimize_cfg(insts, consts, nlocals)
149151
return insts, consts
150152

151153
class AssemblerTestCase(CompilationStepTestCase):
152154

153155
def get_code_object(self, filename, insts, metadata):
154-
co = assemble_code_object(filename, insts, metadata)
156+
_testinternalcapi = import_helper.import_module("_testinternalcapi")
157+
co = _testinternalcapi.assemble_code_object(filename, insts, metadata)
155158
return co

Lib/test/test__xxsubinterpreters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import threading
88
import unittest
99

10-
import _testinternalcapi
1110
from test import support
1211
from test.support import import_helper
1312
from test.support import os_helper
1413
from test.support import script_helper
1514

1615

16+
_testinternalcapi = import_helper.import_module('_testinternalcapi')
1717
interpreters = import_helper.import_module('_xxsubinterpreters')
1818
from _xxsubinterpreters import InterpreterNotFoundError
1919

Lib/test/test_builtin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
4848

4949

50+
raise unittest.SkipTest("FAIL")
51+
52+
5053
class Squares:
5154

5255
def __init__(self, max):

Lib/test/test_call.py

Lines changed: 43 additions & 37 deletions
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:
@@ -240,6 +240,7 @@ def test_module_not_callable_suggestion(self):
240240
self.assertRaisesRegex(TypeError, msg, mod)
241241

242242

243+
@unittest.skipIf(_testcapi is None, "requires _testcapi")
243244
class TestCallingConventions(unittest.TestCase):
244245
"""Test calling using various C calling conventions (METH_*) from Python
245246
@@ -437,6 +438,7 @@ def static_method():
437438

438439
NULL_OR_EMPTY = object()
439440

441+
440442
class FastCallTests(unittest.TestCase):
441443
"""Test calling using various callables from C
442444
"""
@@ -480,42 +482,44 @@ class FastCallTests(unittest.TestCase):
480482
]
481483

482484
# Add all the calling conventions and variants of C callables
483-
_instance = _testcapi.MethInstance()
484-
for obj, expected_self in (
485-
(_testcapi, _testcapi), # module-level function
486-
(_instance, _instance), # bound method
487-
(_testcapi.MethClass, _testcapi.MethClass), # class method on class
488-
(_testcapi.MethClass(), _testcapi.MethClass), # class method on inst.
489-
(_testcapi.MethStatic, None), # static method
490-
):
491-
CALLS_POSARGS.extend([
492-
(obj.meth_varargs, (1, 2), (expected_self, (1, 2))),
493-
(obj.meth_varargs_keywords,
494-
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
495-
(obj.meth_fastcall, (1, 2), (expected_self, (1, 2))),
496-
(obj.meth_fastcall, (), (expected_self, ())),
497-
(obj.meth_fastcall_keywords,
498-
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)),
499-
(obj.meth_fastcall_keywords,
500-
(), (expected_self, (), NULL_OR_EMPTY)),
501-
(obj.meth_noargs, (), expected_self),
502-
(obj.meth_o, (123, ), (expected_self, 123)),
503-
])
504-
505-
CALLS_KWARGS.extend([
506-
(obj.meth_varargs_keywords,
507-
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
508-
(obj.meth_varargs_keywords,
509-
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
510-
(obj.meth_varargs_keywords,
511-
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
512-
(obj.meth_fastcall_keywords,
513-
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})),
514-
(obj.meth_fastcall_keywords,
515-
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})),
516-
(obj.meth_fastcall_keywords,
517-
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)),
518-
])
485+
def setUp(self):
486+
_testcapi = import_helper.import_module("_testcapi")
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+
self.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+
self.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+
])
519523

520524
def check_result(self, result, expected):
521525
if isinstance(expected, tuple) and expected[-1] is NULL_OR_EMPTY:
@@ -606,6 +610,7 @@ def testfunction_kw(self, *, kw):
606610
ADAPTIVE_WARMUP_DELAY = 2
607611

608612

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

611616
def test_method_descriptor_flag(self):
@@ -1019,6 +1024,7 @@ class TestRecursion(unittest.TestCase):
10191024
@skip_on_s390x
10201025
@unittest.skipIf(is_wasi and Py_DEBUG, "requires deep stack")
10211026
def test_super_deep(self):
1027+
_testcapi = import_helper.import_module("_testcapi")
10221028

10231029
def recurse(n):
10241030
if n:

Lib/test/test_capi/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2-
from test.support import load_package_tests
2+
from test.support import load_package_tests, import_helper
3+
4+
import_helper.import_module("_testcapi")
35

46
def load_tests(*args):
57
return load_package_tests(os.path.dirname(__file__), *args)

Lib/test/test_code.py

Lines changed: 3 additions & 4 deletions
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_ctypes/test_as_parameter.py

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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)

0 commit comments

Comments
 (0)