Skip to content

Commit

Permalink
pythongh-105751: test_ctypes: Remove @need_symbol decorator
Browse files Browse the repository at this point in the history
Remove most @need_symbol('...') decorator since requested symbols are
now always always available. Use directly the @unittest.skipUnless()
decorator for Windows types:

* ctypes.WINFUNCTYPE
* ctypes.oledll
  • Loading branch information
vstinner committed Jun 14, 2023
1 parent d50930a commit 22abe5f
Show file tree
Hide file tree
Showing 17 changed files with 10 additions and 66 deletions.
10 changes: 2 additions & 8 deletions Lib/test/test_ctypes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import os
import unittest
from test import support
from test.support import import_helper


# skip tests if _ctypes was not built
ctypes = import_helper.import_module('ctypes')
ctypes_symbols = dir(ctypes)

def need_symbol(name):
return unittest.skipUnless(name in ctypes_symbols,
'{!r} is required'.format(name))
# skip tests if the _ctypes extension was not built
import_helper.import_module('ctypes')

def load_tests(*args):
return support.load_package_tests(os.path.dirname(__file__), *args)
2 changes: 0 additions & 2 deletions Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
c_long, c_ulonglong, c_float, c_double, c_longdouble)
from test.support import bigmemtest, _2G

from test.test_ctypes import need_symbol

formats = "bBhHiIlLqQfd"

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

@need_symbol('create_unicode_buffer')
def test_from_addressW(self):
p = create_unicode_buffer("foo")
sz = (c_wchar * 3).from_address(addressof(p))
Expand Down
5 changes: 1 addition & 4 deletions Lib/test/test_ctypes/test_as_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
c_short, c_int, c_long, c_longlong,
c_byte, c_wchar, c_float, c_double,
ArgumentError)
from test.test_ctypes import need_symbol


dll = CDLL(_ctypes_test.__file__)

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

@need_symbol('c_wchar')
def test_wchar_parm(self):
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
Expand Down Expand Up @@ -127,9 +126,7 @@ def callback(value):
result = f(self.wrap(-10), self.wrap(cb))
self.assertEqual(result, -18)

@need_symbol('c_longlong')
def test_longlong_callbacks(self):

f = dll._testfunc_callback_q_qf
f.restype = c_longlong

Expand Down
6 changes: 0 additions & 6 deletions Lib/test/test_ctypes/test_bitfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
c_byte, c_ubyte, c_char, c_char_p, c_void_p, c_wchar,
c_uint32, c_uint64,
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
from test.test_ctypes import need_symbol
from test import support
import unittest
import os
Expand Down Expand Up @@ -144,7 +143,6 @@ class Dummy(Structure):
result = self.fail_fields(("a", Dummy, 1))
self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy'))

@need_symbol('c_wchar')
def test_c_wchar(self):
result = self.fail_fields(("a", c_wchar, 1))
self.assertEqual(result,
Expand Down Expand Up @@ -249,7 +247,6 @@ class Y(Structure):
_anonymous_ = ["_"]
_fields_ = [("_", X)]

@need_symbol('c_uint32')
def test_uint32(self):
class X(Structure):
_fields_ = [("a", c_uint32, 32)]
Expand All @@ -259,7 +256,6 @@ class X(Structure):
x.a = 0xFDCBA987
self.assertEqual(x.a, 0xFDCBA987)

@need_symbol('c_uint64')
def test_uint64(self):
class X(Structure):
_fields_ = [("a", c_uint64, 64)]
Expand All @@ -269,7 +265,6 @@ class X(Structure):
x.a = 0xFEDCBA9876543211
self.assertEqual(x.a, 0xFEDCBA9876543211)

@need_symbol('c_uint32')
def test_uint32_swap_little_endian(self):
# Issue #23319
class Little(LittleEndianStructure):
Expand All @@ -283,7 +278,6 @@ class Little(LittleEndianStructure):
x.c = 2
self.assertEqual(b, b'\xef\xcd\xab\x21')

@need_symbol('c_uint32')
def test_uint32_swap_big_endian(self):
# Issue #23319
class Big(BigEndianStructure):
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_ctypes/test_buffers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from ctypes import (create_string_buffer, create_unicode_buffer, sizeof,
c_char, c_wchar)
from test.test_ctypes import need_symbol
import unittest

class StringBufferTestCase(unittest.TestCase):
Expand Down Expand Up @@ -28,7 +27,6 @@ def test_buffer_interface(self):
self.assertEqual(len(bytearray(create_string_buffer(0))), 0)
self.assertEqual(len(bytearray(create_string_buffer(1))), 1)

@need_symbol('c_wchar')
def test_unicode_buffer(self):
b = create_unicode_buffer(32)
self.assertEqual(len(b), 32)
Expand All @@ -48,7 +46,6 @@ def test_unicode_buffer(self):

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

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

@need_symbol('c_wchar')
def test_create_unicode_buffer_non_bmp(self):
expected = 5 if sizeof(c_wchar) == 2 else 3
for s in '\U00010000\U00100000', '\U00010000\U0010ffff':
Expand Down
11 changes: 5 additions & 6 deletions Lib/test/test_ctypes/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
c_short, c_ushort, c_int, c_uint,
c_long, c_longlong, c_ulonglong, c_ulong,
c_float, c_double, c_longdouble, py_object)
from test.test_ctypes import need_symbol
from _ctypes import CTYPES_MAX_ARGCOUNT
import _ctypes_test


class Callbacks(unittest.TestCase):
functype = CFUNCTYPE

Expand Down Expand Up @@ -71,12 +71,10 @@ def test_long(self):
def test_ulong(self):
self.check_type(c_ulong, 42)

@need_symbol('c_longlong')
def test_longlong(self):
self.check_type(c_longlong, 42)
self.check_type(c_longlong, -42)

@need_symbol('c_ulonglong')
def test_ulonglong(self):
self.check_type(c_ulonglong, 42)

Expand All @@ -90,7 +88,6 @@ def test_double(self):
self.check_type(c_double, 3.14)
self.check_type(c_double, -3.14)

@need_symbol('c_longdouble')
def test_longdouble(self):
self.check_type(c_longdouble, 3.14)
self.check_type(c_longdouble, -3.14)
Expand Down Expand Up @@ -156,7 +153,8 @@ def __del__(self):
gc.collect()
CFUNCTYPE(None)(lambda x=Nasty(): None)

@need_symbol('WINFUNCTYPE')
@unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'),
'ctypes.WINFUNCTYPE is required')
def test_i38748_stackCorruption(self):
callback_funcType = ctypes.WINFUNCTYPE(c_long, c_long, c_longlong)
@callback_funcType
Expand Down Expand Up @@ -214,7 +212,8 @@ def cmp_func(a, b):
libc.qsort(array, len(array), sizeof(c_int), cmp_func)
self.assertEqual(array[:], [1, 5, 7, 33, 99])

@need_symbol('WINFUNCTYPE')
@unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'),
'ctypes.WINFUNCTYPE is required')
def test_issue_8959_b(self):
from ctypes.wintypes import BOOL, HWND, LPARAM
global windowCount
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_ctypes/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from ctypes import (Structure, Union, POINTER, cast, sizeof, addressof,
c_void_p, c_char_p, c_wchar_p,
c_byte, c_short, c_int)
from test.test_ctypes import need_symbol

class Test(unittest.TestCase):

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

@need_symbol('c_wchar_p')
def test_wchar_p(self):
s = c_wchar_p("hiho")
self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
Expand Down
7 changes: 0 additions & 7 deletions Lib/test/test_ctypes/test_cfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
c_short, c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong, c_ulonglong,
c_float, c_double, c_longdouble)
from test.test_ctypes import need_symbol
import _ctypes_test


Expand Down Expand Up @@ -113,28 +112,24 @@ def test_ulong_plus(self):
self.assertEqual(self._dll.tf_bL(b' ', 4294967295), 1431655765)
self.assertEqual(self.U(), 4294967295)

@need_symbol('c_longlong')
def test_longlong(self):
self._dll.tf_q.restype = c_longlong
self._dll.tf_q.argtypes = (c_longlong, )
self.assertEqual(self._dll.tf_q(-9223372036854775806), -3074457345618258602)
self.assertEqual(self.S(), -9223372036854775806)

@need_symbol('c_longlong')
def test_longlong_plus(self):
self._dll.tf_bq.restype = c_longlong
self._dll.tf_bq.argtypes = (c_byte, c_longlong)
self.assertEqual(self._dll.tf_bq(0, -9223372036854775806), -3074457345618258602)
self.assertEqual(self.S(), -9223372036854775806)

@need_symbol('c_ulonglong')
def test_ulonglong(self):
self._dll.tf_Q.restype = c_ulonglong
self._dll.tf_Q.argtypes = (c_ulonglong, )
self.assertEqual(self._dll.tf_Q(18446744073709551615), 6148914691236517205)
self.assertEqual(self.U(), 18446744073709551615)

@need_symbol('c_ulonglong')
def test_ulonglong_plus(self):
self._dll.tf_bQ.restype = c_ulonglong
self._dll.tf_bQ.argtypes = (c_byte, c_ulonglong)
Expand Down Expand Up @@ -165,14 +160,12 @@ def test_double_plus(self):
self.assertEqual(self._dll.tf_bd(0, 42.), 14.)
self.assertEqual(self.S(), 42)

@need_symbol('c_longdouble')
def test_longdouble(self):
self._dll.tf_D.restype = c_longdouble
self._dll.tf_D.argtypes = (c_longdouble,)
self.assertEqual(self._dll.tf_D(42.), 14.)
self.assertEqual(self.S(), 42)

@need_symbol('c_longdouble')
def test_longdouble_plus(self):
self._dll.tf_bD.restype = c_longdouble
self._dll.tf_bD.argtypes = (c_byte, c_longdouble)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_ctypes/test_checkretval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ctypes
import unittest
from ctypes import CDLL, c_int
from test.test_ctypes import need_symbol


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

@need_symbol('oledll')
@unittest.skipUnless(hasattr(ctypes, 'oledll'),
'ctypes.oledll is required')
def test_oledll(self):
oleaut32 = ctypes.oledll.oleaut32
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)
Expand Down
7 changes: 0 additions & 7 deletions Lib/test/test_ctypes/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
c_char, c_wchar, c_byte, c_char_p,
c_short, c_int, c_long, c_longlong,
c_float, c_double, c_longdouble)
from test.test_ctypes import need_symbol
import sys, unittest

try:
Expand Down Expand Up @@ -75,8 +74,6 @@ def callback(*args):
"argument 1: TypeError: one character bytes, "
"bytearray or integer expected")


@need_symbol('c_wchar')
def test_wchar_parm(self):
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
Expand All @@ -96,7 +93,6 @@ def test_wchar_parm(self):
"argument 2: TypeError: one character unicode string "
"expected")

@need_symbol('c_wchar')
def test_wchar_result(self):
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
Expand Down Expand Up @@ -162,7 +158,6 @@ def test_doubleresult(self):
self.assertEqual(result, -21)
self.assertEqual(type(result), float)

@need_symbol('c_longdouble')
def test_longdoubleresult(self):
f = dll._testfunc_D_bhilfD
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]
Expand All @@ -175,7 +170,6 @@ def test_longdoubleresult(self):
self.assertEqual(result, -21)
self.assertEqual(type(result), float)

@need_symbol('c_longlong')
def test_longlongresult(self):
f = dll._testfunc_q_bhilfd
f.restype = c_longlong
Expand Down Expand Up @@ -304,7 +298,6 @@ def callback(value):
result = f(-10, cb)
self.assertEqual(result, -18)

@need_symbol('c_longlong')
def test_longlong_callbacks(self):

f = dll._testfunc_callback_q_qf
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_ctypes/test_memfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
create_unicode_buffer, wstring_at,
memmove, memset,
c_char_p, c_byte, c_ubyte, c_wchar)
from test.test_ctypes import need_symbol

class MemFunctionsTest(unittest.TestCase):
@unittest.skip('test disabled')
Expand Down Expand Up @@ -67,7 +66,6 @@ def test_string_at(self):
self.assertEqual(string_at(b"foo bar", 7), b"foo bar")
self.assertEqual(string_at(b"foo bar", 3), b"foo")

@need_symbol('create_unicode_buffer')
def test_wstring_at(self):
p = create_unicode_buffer("Hello, World")
a = create_unicode_buffer(1000000)
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_ctypes/test_parameters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from test.test_ctypes import need_symbol
import test.support

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

@need_symbol('c_wchar_p')
def test_subclasses_c_wchar_p(self):
from ctypes import c_wchar_p

Expand Down Expand Up @@ -66,7 +64,6 @@ def test_cstrings(self):
a = c_char_p(b"123")
self.assertIs(c_char_p.from_param(a), a)

@need_symbol('c_wchar_p')
def test_cw_strings(self):
from ctypes import c_wchar_p

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

@need_symbol('c_wchar')
def test_c_wchar(self):
from ctypes import c_wchar

Expand Down
3 changes: 0 additions & 3 deletions Lib/test/test_ctypes/test_prototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pointer, byref, sizeof, addressof,
c_void_p, c_char_p, c_wchar_p, c_char, c_wchar, c_buffer,
c_short, c_int, c_long, c_longlong, c_double)
from test.test_ctypes import need_symbol
import unittest

# IMPORTANT INFO:
Expand Down Expand Up @@ -142,7 +141,6 @@ def test_c_void_p_arg(self):
func(pointer(c_int()))
func((c_int * 3)())

@need_symbol('c_wchar_p')
def test_c_void_p_arg_with_c_wchar_p(self):
func = testdll._testfunc_p_p
func.restype = c_wchar_p
Expand All @@ -164,7 +162,6 @@ class X:
func.argtypes = None
self.assertEqual(None, func(X()))

@need_symbol('c_wchar')
class WCharPointersTestCase(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit 22abe5f

Please sign in to comment.