| 1 |
import ctypes |
| 2 |
import unittest |
| 3 |
from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr, |
| 4 |
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") |
| 7 |
from ._support import (_CData, PyCFuncPtrType, Py_TPFLAGS_DISALLOW_INSTANTIATION, |
| 8 |
Py_TPFLAGS_IMMUTABLETYPE, StructCheckMixin) |
| 9 |
|
| 10 |
|
| 11 |
try: |
| 12 |
WINFUNCTYPE = ctypes.WINFUNCTYPE |
| 13 |
except AttributeError: |
| 14 |
# fake to enable this test on Linux |
| 15 |
WINFUNCTYPE = CFUNCTYPE |
| 16 |
|
| 17 |
lib = CDLL(_ctypes_test.__file__) |
| 18 |
|
| 19 |
|
| 20 |
class CFuncPtrTestCase(unittest.TestCase, StructCheckMixin): |
| 21 |
def test_inheritance_hierarchy(self): |
| 22 |
self.assertEqual(_CFuncPtr.mro(), [_CFuncPtr, _CData, object]) |
| 23 |
|
| 24 |
self.assertEqual(PyCFuncPtrType.__name__, "PyCFuncPtrType") |
| 25 |
self.assertEqual(type(PyCFuncPtrType), type) |
| 26 |
|
| 27 |
def test_type_flags(self): |
| 28 |
for cls in _CFuncPtr, PyCFuncPtrType: |
| 29 |
with self.subTest(cls=cls): |
| 30 |
self.assertTrue(_CFuncPtr.__flags__ & Py_TPFLAGS_IMMUTABLETYPE) |
| 31 |
self.assertFalse(_CFuncPtr.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION) |
| 32 |
|
| 33 |
def test_metaclass_details(self): |
| 34 |
# Cannot call the metaclass __init__ more than once |
| 35 |
CdeclCallback = CFUNCTYPE(c_int, c_int, c_int) |
| 36 |
with self.assertRaisesRegex(SystemError, "already initialized"): |
| 37 |
PyCFuncPtrType.__init__(CdeclCallback, 'ptr', (), {}) |
| 38 |
|
| 39 |
def test_basic(self): |
| 40 |
X = WINFUNCTYPE(c_int, c_int, c_int) |
| 41 |
|
| 42 |
def func(*args): |
| 43 |
return len(args) |
| 44 |
|
| 45 |
x = X(func) |
| 46 |
self.assertEqual(x.restype, c_int) |
| 47 |
self.assertEqual(x.argtypes, (c_int, c_int)) |
| 48 |
self.assertEqual(sizeof(x), sizeof(c_void_p)) |
| 49 |
self.assertEqual(sizeof(X), sizeof(c_void_p)) |
| 50 |
|
| 51 |
def test_first(self): |
| 52 |
StdCallback = WINFUNCTYPE(c_int, c_int, c_int) |
| 53 |
CdeclCallback = CFUNCTYPE(c_int, c_int, c_int) |
| 54 |
|
| 55 |
def func(a, b): |
| 56 |
return a + b |
| 57 |
|
| 58 |
s = StdCallback(func) |
| 59 |
c = CdeclCallback(func) |
| 60 |
|
| 61 |
self.assertEqual(s(1, 2), 3) |
| 62 |
self.assertEqual(c(1, 2), 3) |
| 63 |
# The following no longer raises a TypeError - it is now |
| 64 |
# possible, as in C, to call cdecl functions with more parameters. |
| 65 |
#self.assertRaises(TypeError, c, 1, 2, 3) |
| 66 |
self.assertEqual(c(1, 2, 3, 4, 5, 6), 3) |
| 67 |
if WINFUNCTYPE is not CFUNCTYPE: |
| 68 |
self.assertRaises(TypeError, s, 1, 2, 3) |
| 69 |
|
| 70 |
def test_structures(self): |
| 71 |
WNDPROC = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int) |
| 72 |
|
| 73 |
def wndproc(hwnd, msg, wParam, lParam): |
| 74 |
return hwnd + msg + wParam + lParam |
| 75 |
|
| 76 |
HINSTANCE = c_int |
| 77 |
HICON = c_int |
| 78 |
HCURSOR = c_int |
| 79 |
LPCTSTR = c_char_p |
| 80 |
|
| 81 |
class WNDCLASS(Structure): |
| 82 |
_fields_ = [("style", c_uint), |
| 83 |
("lpfnWndProc", WNDPROC), |
| 84 |
("cbClsExtra", c_int), |
| 85 |
("cbWndExtra", c_int), |
| 86 |
("hInstance", HINSTANCE), |
| 87 |
("hIcon", HICON), |
| 88 |
("hCursor", HCURSOR), |
| 89 |
("lpszMenuName", LPCTSTR), |
| 90 |
("lpszClassName", LPCTSTR)] |
| 91 |
self.check_struct(WNDCLASS) |
| 92 |
|
| 93 |
wndclass = WNDCLASS() |
| 94 |
wndclass.lpfnWndProc = WNDPROC(wndproc) |
| 95 |
|
| 96 |
WNDPROC_2 = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int) |
| 97 |
|
| 98 |
self.assertIs(WNDPROC, WNDPROC_2) |
| 99 |
self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10) |
| 100 |
|
| 101 |
f = wndclass.lpfnWndProc |
| 102 |
|
| 103 |
del wndclass |
| 104 |
del wndproc |
| 105 |
|
| 106 |
self.assertEqual(f(10, 11, 12, 13), 46) |
| 107 |
|
| 108 |
def test_dllfunctions(self): |
| 109 |
strchr = lib.my_strchr |
| 110 |
strchr.restype = c_char_p |
| 111 |
strchr.argtypes = (c_char_p, c_char) |
| 112 |
self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi") |
| 113 |
self.assertEqual(strchr(b"abcdefghi", b"x"), None) |
| 114 |
|
| 115 |
strtok = lib.my_strtok |
| 116 |
strtok.restype = c_char_p |
| 117 |
|
| 118 |
def c_string(init): |
| 119 |
size = len(init) + 1 |
| 120 |
return (c_char*size)(*init) |
| 121 |
|
| 122 |
s = b"a\nb\nc" |
| 123 |
b = c_string(s) |
| 124 |
|
| 125 |
self.assertEqual(strtok(b, b"\n"), b"a") |
| 126 |
self.assertEqual(strtok(None, b"\n"), b"b") |
| 127 |
self.assertEqual(strtok(None, b"\n"), b"c") |
| 128 |
self.assertEqual(strtok(None, b"\n"), None) |
| 129 |
|
| 130 |
def test_abstract(self): |
| 131 |
self.assertRaises(TypeError, _CFuncPtr, 13, "name", 42, "iid") |
| 132 |
|
| 133 |
|
| 134 |
if __name__ == '__main__': |
| 135 |
unittest.main() |