Skip to content

Commit e7507bd

Browse files
authored
gh-105751: test_ctypes: Remove @need_symbol decorator (GH-105798)
Remove the @need_symbol('...') decorator of test.test_ctypes since requested symbols are now always always available in ctypes. Use the @unittest.skipUnless() decorator directly for the two types only available on Windows: * ctypes.WINFUNCTYPE * ctypes.oledll
1 parent 4caa728 commit e7507bd

17 files changed

+10
-66
lines changed

Lib/test/test_ctypes/__init__.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import os
2-
import unittest
32
from test import support
43
from test.support import import_helper
54

65

7-
# skip tests if _ctypes was not built
8-
ctypes = import_helper.import_module('ctypes')
9-
ctypes_symbols = dir(ctypes)
10-
11-
def need_symbol(name):
12-
return unittest.skipUnless(name in ctypes_symbols,
13-
'{!r} is required'.format(name))
6+
# skip tests if the _ctypes extension was not built
7+
import_helper.import_module('ctypes')
148

159
def load_tests(*args):
1610
return support.load_package_tests(os.path.dirname(__file__), *args)

Lib/test/test_ctypes/test_arrays.py

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
c_long, c_ulonglong, c_float, c_double, c_longdouble)
99
from test.support import bigmemtest, _2G
1010

11-
from test.test_ctypes import need_symbol
1211

1312
formats = "bBhHiIlLqQfd"
1413

@@ -130,7 +129,6 @@ def test_from_address(self):
130129
self.assertEqual(sz[1:4:2], b"o")
131130
self.assertEqual(sz.value, b"foo")
132131

133-
@need_symbol('create_unicode_buffer')
134132
def test_from_addressW(self):
135133
p = create_unicode_buffer("foo")
136134
sz = (c_wchar * 3).from_address(addressof(p))

Lib/test/test_ctypes/test_as_parameter.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
c_short, c_int, c_long, c_longlong,
77
c_byte, c_wchar, c_float, c_double,
88
ArgumentError)
9-
from test.test_ctypes import need_symbol
9+
1010

1111
dll = CDLL(_ctypes_test.__file__)
1212

@@ -23,7 +23,6 @@ class BasicWrapTestCase(unittest.TestCase):
2323
def wrap(self, param):
2424
return param
2525

26-
@need_symbol('c_wchar')
2726
def test_wchar_parm(self):
2827
f = dll._testfunc_i_bhilfd
2928
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
@@ -127,9 +126,7 @@ def callback(value):
127126
result = f(self.wrap(-10), self.wrap(cb))
128127
self.assertEqual(result, -18)
129128

130-
@need_symbol('c_longlong')
131129
def test_longlong_callbacks(self):
132-
133130
f = dll._testfunc_callback_q_qf
134131
f.restype = c_longlong
135132

Lib/test/test_ctypes/test_bitfields.py

-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
c_byte, c_ubyte, c_char, c_char_p, c_void_p, c_wchar,
44
c_uint32, c_uint64,
55
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
6-
from test.test_ctypes import need_symbol
76
from test import support
87
import unittest
98
import os
@@ -144,7 +143,6 @@ class Dummy(Structure):
144143
result = self.fail_fields(("a", Dummy, 1))
145144
self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy'))
146145

147-
@need_symbol('c_wchar')
148146
def test_c_wchar(self):
149147
result = self.fail_fields(("a", c_wchar, 1))
150148
self.assertEqual(result,
@@ -249,7 +247,6 @@ class Y(Structure):
249247
_anonymous_ = ["_"]
250248
_fields_ = [("_", X)]
251249

252-
@need_symbol('c_uint32')
253250
def test_uint32(self):
254251
class X(Structure):
255252
_fields_ = [("a", c_uint32, 32)]
@@ -259,7 +256,6 @@ class X(Structure):
259256
x.a = 0xFDCBA987
260257
self.assertEqual(x.a, 0xFDCBA987)
261258

262-
@need_symbol('c_uint64')
263259
def test_uint64(self):
264260
class X(Structure):
265261
_fields_ = [("a", c_uint64, 64)]
@@ -269,7 +265,6 @@ class X(Structure):
269265
x.a = 0xFEDCBA9876543211
270266
self.assertEqual(x.a, 0xFEDCBA9876543211)
271267

272-
@need_symbol('c_uint32')
273268
def test_uint32_swap_little_endian(self):
274269
# Issue #23319
275270
class Little(LittleEndianStructure):
@@ -283,7 +278,6 @@ class Little(LittleEndianStructure):
283278
x.c = 2
284279
self.assertEqual(b, b'\xef\xcd\xab\x21')
285280

286-
@need_symbol('c_uint32')
287281
def test_uint32_swap_big_endian(self):
288282
# Issue #23319
289283
class Big(BigEndianStructure):

Lib/test/test_ctypes/test_buffers.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from ctypes import (create_string_buffer, create_unicode_buffer, sizeof,
22
c_char, c_wchar)
3-
from test.test_ctypes import need_symbol
43
import unittest
54

65
class StringBufferTestCase(unittest.TestCase):
@@ -28,7 +27,6 @@ def test_buffer_interface(self):
2827
self.assertEqual(len(bytearray(create_string_buffer(0))), 0)
2928
self.assertEqual(len(bytearray(create_string_buffer(1))), 1)
3029

31-
@need_symbol('c_wchar')
3230
def test_unicode_buffer(self):
3331
b = create_unicode_buffer(32)
3432
self.assertEqual(len(b), 32)
@@ -48,7 +46,6 @@ def test_unicode_buffer(self):
4846

4947
self.assertRaises(TypeError, create_unicode_buffer, b"abc")
5048

51-
@need_symbol('c_wchar')
5249
def test_unicode_conversion(self):
5350
b = create_unicode_buffer("abc")
5451
self.assertEqual(len(b), 4) # trailing nul char
@@ -61,7 +58,6 @@ def test_unicode_conversion(self):
6158
self.assertEqual(b[::2], "ac")
6259
self.assertEqual(b[::5], "a")
6360

64-
@need_symbol('c_wchar')
6561
def test_create_unicode_buffer_non_bmp(self):
6662
expected = 5 if sizeof(c_wchar) == 2 else 3
6763
for s in '\U00010000\U00100000', '\U00010000\U0010ffff':

Lib/test/test_ctypes/test_callbacks.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
c_short, c_ushort, c_int, c_uint,
1010
c_long, c_longlong, c_ulonglong, c_ulong,
1111
c_float, c_double, c_longdouble, py_object)
12-
from test.test_ctypes import need_symbol
1312
from _ctypes import CTYPES_MAX_ARGCOUNT
1413
import _ctypes_test
1514

15+
1616
class Callbacks(unittest.TestCase):
1717
functype = CFUNCTYPE
1818

@@ -71,12 +71,10 @@ def test_long(self):
7171
def test_ulong(self):
7272
self.check_type(c_ulong, 42)
7373

74-
@need_symbol('c_longlong')
7574
def test_longlong(self):
7675
self.check_type(c_longlong, 42)
7776
self.check_type(c_longlong, -42)
7877

79-
@need_symbol('c_ulonglong')
8078
def test_ulonglong(self):
8179
self.check_type(c_ulonglong, 42)
8280

@@ -90,7 +88,6 @@ def test_double(self):
9088
self.check_type(c_double, 3.14)
9189
self.check_type(c_double, -3.14)
9290

93-
@need_symbol('c_longdouble')
9491
def test_longdouble(self):
9592
self.check_type(c_longdouble, 3.14)
9693
self.check_type(c_longdouble, -3.14)
@@ -156,7 +153,8 @@ def __del__(self):
156153
gc.collect()
157154
CFUNCTYPE(None)(lambda x=Nasty(): None)
158155

159-
@need_symbol('WINFUNCTYPE')
156+
@unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'),
157+
'ctypes.WINFUNCTYPE is required')
160158
def test_i38748_stackCorruption(self):
161159
callback_funcType = ctypes.WINFUNCTYPE(c_long, c_long, c_longlong)
162160
@callback_funcType
@@ -214,7 +212,8 @@ def cmp_func(a, b):
214212
libc.qsort(array, len(array), sizeof(c_int), cmp_func)
215213
self.assertEqual(array[:], [1, 5, 7, 33, 99])
216214

217-
@need_symbol('WINFUNCTYPE')
215+
@unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'),
216+
'ctypes.WINFUNCTYPE is required')
218217
def test_issue_8959_b(self):
219218
from ctypes.wintypes import BOOL, HWND, LPARAM
220219
global windowCount

Lib/test/test_ctypes/test_cast.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from ctypes import (Structure, Union, POINTER, cast, sizeof, addressof,
44
c_void_p, c_char_p, c_wchar_p,
55
c_byte, c_short, c_int)
6-
from test.test_ctypes import need_symbol
76

87
class Test(unittest.TestCase):
98

@@ -78,7 +77,6 @@ def test_char_p(self):
7877
self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
7978
b"hiho")
8079

81-
@need_symbol('c_wchar_p')
8280
def test_wchar_p(self):
8381
s = c_wchar_p("hiho")
8482
self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,

Lib/test/test_ctypes/test_cfuncs.py

-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
c_short, c_ushort, c_int, c_uint,
66
c_long, c_ulong, c_longlong, c_ulonglong,
77
c_float, c_double, c_longdouble)
8-
from test.test_ctypes import need_symbol
98
import _ctypes_test
109

1110

@@ -113,28 +112,24 @@ def test_ulong_plus(self):
113112
self.assertEqual(self._dll.tf_bL(b' ', 4294967295), 1431655765)
114113
self.assertEqual(self.U(), 4294967295)
115114

116-
@need_symbol('c_longlong')
117115
def test_longlong(self):
118116
self._dll.tf_q.restype = c_longlong
119117
self._dll.tf_q.argtypes = (c_longlong, )
120118
self.assertEqual(self._dll.tf_q(-9223372036854775806), -3074457345618258602)
121119
self.assertEqual(self.S(), -9223372036854775806)
122120

123-
@need_symbol('c_longlong')
124121
def test_longlong_plus(self):
125122
self._dll.tf_bq.restype = c_longlong
126123
self._dll.tf_bq.argtypes = (c_byte, c_longlong)
127124
self.assertEqual(self._dll.tf_bq(0, -9223372036854775806), -3074457345618258602)
128125
self.assertEqual(self.S(), -9223372036854775806)
129126

130-
@need_symbol('c_ulonglong')
131127
def test_ulonglong(self):
132128
self._dll.tf_Q.restype = c_ulonglong
133129
self._dll.tf_Q.argtypes = (c_ulonglong, )
134130
self.assertEqual(self._dll.tf_Q(18446744073709551615), 6148914691236517205)
135131
self.assertEqual(self.U(), 18446744073709551615)
136132

137-
@need_symbol('c_ulonglong')
138133
def test_ulonglong_plus(self):
139134
self._dll.tf_bQ.restype = c_ulonglong
140135
self._dll.tf_bQ.argtypes = (c_byte, c_ulonglong)
@@ -165,14 +160,12 @@ def test_double_plus(self):
165160
self.assertEqual(self._dll.tf_bd(0, 42.), 14.)
166161
self.assertEqual(self.S(), 42)
167162

168-
@need_symbol('c_longdouble')
169163
def test_longdouble(self):
170164
self._dll.tf_D.restype = c_longdouble
171165
self._dll.tf_D.argtypes = (c_longdouble,)
172166
self.assertEqual(self._dll.tf_D(42.), 14.)
173167
self.assertEqual(self.S(), 42)
174168

175-
@need_symbol('c_longdouble')
176169
def test_longdouble_plus(self):
177170
self._dll.tf_bD.restype = c_longdouble
178171
self._dll.tf_bD.argtypes = (c_byte, c_longdouble)

Lib/test/test_ctypes/test_checkretval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ctypes
22
import unittest
33
from ctypes import CDLL, c_int
4-
from test.test_ctypes import need_symbol
54

65

76
class CHECKED(c_int):
@@ -28,7 +27,8 @@ def test_checkretval(self):
2827
del dll._testfunc_p_p.restype
2928
self.assertEqual(42, dll._testfunc_p_p(42))
3029

31-
@need_symbol('oledll')
30+
@unittest.skipUnless(hasattr(ctypes, 'oledll'),
31+
'ctypes.oledll is required')
3232
def test_oledll(self):
3333
oleaut32 = ctypes.oledll.oleaut32
3434
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)

Lib/test/test_ctypes/test_functions.py

-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
c_char, c_wchar, c_byte, c_char_p,
1212
c_short, c_int, c_long, c_longlong,
1313
c_float, c_double, c_longdouble)
14-
from test.test_ctypes import need_symbol
1514
import sys, unittest
1615

1716
try:
@@ -75,8 +74,6 @@ def callback(*args):
7574
"argument 1: TypeError: one character bytes, "
7675
"bytearray or integer expected")
7776

78-
79-
@need_symbol('c_wchar')
8077
def test_wchar_parm(self):
8178
f = dll._testfunc_i_bhilfd
8279
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
@@ -96,7 +93,6 @@ def test_wchar_parm(self):
9693
"argument 2: TypeError: one character unicode string "
9794
"expected")
9895

99-
@need_symbol('c_wchar')
10096
def test_wchar_result(self):
10197
f = dll._testfunc_i_bhilfd
10298
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
@@ -162,7 +158,6 @@ def test_doubleresult(self):
162158
self.assertEqual(result, -21)
163159
self.assertEqual(type(result), float)
164160

165-
@need_symbol('c_longdouble')
166161
def test_longdoubleresult(self):
167162
f = dll._testfunc_D_bhilfD
168163
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]
@@ -175,7 +170,6 @@ def test_longdoubleresult(self):
175170
self.assertEqual(result, -21)
176171
self.assertEqual(type(result), float)
177172

178-
@need_symbol('c_longlong')
179173
def test_longlongresult(self):
180174
f = dll._testfunc_q_bhilfd
181175
f.restype = c_longlong
@@ -304,7 +298,6 @@ def callback(value):
304298
result = f(-10, cb)
305299
self.assertEqual(result, -18)
306300

307-
@need_symbol('c_longlong')
308301
def test_longlong_callbacks(self):
309302

310303
f = dll._testfunc_callback_q_qf

Lib/test/test_ctypes/test_memfunctions.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
create_unicode_buffer, wstring_at,
77
memmove, memset,
88
c_char_p, c_byte, c_ubyte, c_wchar)
9-
from test.test_ctypes import need_symbol
109

1110
class MemFunctionsTest(unittest.TestCase):
1211
@unittest.skip('test disabled')
@@ -67,7 +66,6 @@ def test_string_at(self):
6766
self.assertEqual(string_at(b"foo bar", 7), b"foo bar")
6867
self.assertEqual(string_at(b"foo bar", 3), b"foo")
6968

70-
@need_symbol('create_unicode_buffer')
7169
def test_wstring_at(self):
7270
p = create_unicode_buffer("Hello, World")
7371
a = create_unicode_buffer(1000000)

Lib/test/test_ctypes/test_parameters.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
from test.test_ctypes import need_symbol
32
import test.support
43

54
class SimpleTypesTestCase(unittest.TestCase):
@@ -36,7 +35,6 @@ def from_param(cls, value):
3635
self.assertEqual(CVOIDP.from_param("abc"), "abcabc")
3736
self.assertEqual(CCHARP.from_param("abc"), "abcabcabcabc")
3837

39-
@need_symbol('c_wchar_p')
4038
def test_subclasses_c_wchar_p(self):
4139
from ctypes import c_wchar_p
4240

@@ -66,7 +64,6 @@ def test_cstrings(self):
6664
a = c_char_p(b"123")
6765
self.assertIs(c_char_p.from_param(a), a)
6866

69-
@need_symbol('c_wchar_p')
7067
def test_cw_strings(self):
7168
from ctypes import c_wchar_p
7269

@@ -86,7 +83,6 @@ def test_c_char(self):
8683
self.assertEqual(str(cm.exception),
8784
"one character bytes, bytearray or integer expected")
8885

89-
@need_symbol('c_wchar')
9086
def test_c_wchar(self):
9187
from ctypes import c_wchar
9288

Lib/test/test_ctypes/test_prototypes.py

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
pointer, byref, sizeof, addressof,
33
c_void_p, c_char_p, c_wchar_p, c_char, c_wchar, c_buffer,
44
c_short, c_int, c_long, c_longlong, c_double)
5-
from test.test_ctypes import need_symbol
65
import unittest
76

87
# IMPORTANT INFO:
@@ -142,7 +141,6 @@ def test_c_void_p_arg(self):
142141
func(pointer(c_int()))
143142
func((c_int * 3)())
144143

145-
@need_symbol('c_wchar_p')
146144
def test_c_void_p_arg_with_c_wchar_p(self):
147145
func = testdll._testfunc_p_p
148146
func.restype = c_wchar_p
@@ -164,7 +162,6 @@ class X:
164162
func.argtypes = None
165163
self.assertEqual(None, func(X()))
166164

167-
@need_symbol('c_wchar')
168165
class WCharPointersTestCase(unittest.TestCase):
169166

170167
def setUp(self):

0 commit comments

Comments
 (0)