1 | n/a | import unittest |
---|
2 | n/a | from ctypes import * |
---|
3 | n/a | |
---|
4 | n/a | try: |
---|
5 | n/a | WINFUNCTYPE |
---|
6 | n/a | except NameError: |
---|
7 | n/a | # fake to enable this test on Linux |
---|
8 | n/a | WINFUNCTYPE = CFUNCTYPE |
---|
9 | n/a | |
---|
10 | n/a | import _ctypes_test |
---|
11 | n/a | lib = CDLL(_ctypes_test.__file__) |
---|
12 | n/a | |
---|
13 | n/a | class CFuncPtrTestCase(unittest.TestCase): |
---|
14 | n/a | def test_basic(self): |
---|
15 | n/a | X = WINFUNCTYPE(c_int, c_int, c_int) |
---|
16 | n/a | |
---|
17 | n/a | def func(*args): |
---|
18 | n/a | return len(args) |
---|
19 | n/a | |
---|
20 | n/a | x = X(func) |
---|
21 | n/a | self.assertEqual(x.restype, c_int) |
---|
22 | n/a | self.assertEqual(x.argtypes, (c_int, c_int)) |
---|
23 | n/a | self.assertEqual(sizeof(x), sizeof(c_voidp)) |
---|
24 | n/a | self.assertEqual(sizeof(X), sizeof(c_voidp)) |
---|
25 | n/a | |
---|
26 | n/a | def test_first(self): |
---|
27 | n/a | StdCallback = WINFUNCTYPE(c_int, c_int, c_int) |
---|
28 | n/a | CdeclCallback = CFUNCTYPE(c_int, c_int, c_int) |
---|
29 | n/a | |
---|
30 | n/a | def func(a, b): |
---|
31 | n/a | return a + b |
---|
32 | n/a | |
---|
33 | n/a | s = StdCallback(func) |
---|
34 | n/a | c = CdeclCallback(func) |
---|
35 | n/a | |
---|
36 | n/a | self.assertEqual(s(1, 2), 3) |
---|
37 | n/a | self.assertEqual(c(1, 2), 3) |
---|
38 | n/a | # The following no longer raises a TypeError - it is now |
---|
39 | n/a | # possible, as in C, to call cdecl functions with more parameters. |
---|
40 | n/a | #self.assertRaises(TypeError, c, 1, 2, 3) |
---|
41 | n/a | self.assertEqual(c(1, 2, 3, 4, 5, 6), 3) |
---|
42 | n/a | if not WINFUNCTYPE is CFUNCTYPE: |
---|
43 | n/a | self.assertRaises(TypeError, s, 1, 2, 3) |
---|
44 | n/a | |
---|
45 | n/a | def test_structures(self): |
---|
46 | n/a | WNDPROC = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int) |
---|
47 | n/a | |
---|
48 | n/a | def wndproc(hwnd, msg, wParam, lParam): |
---|
49 | n/a | return hwnd + msg + wParam + lParam |
---|
50 | n/a | |
---|
51 | n/a | HINSTANCE = c_int |
---|
52 | n/a | HICON = c_int |
---|
53 | n/a | HCURSOR = c_int |
---|
54 | n/a | LPCTSTR = c_char_p |
---|
55 | n/a | |
---|
56 | n/a | class WNDCLASS(Structure): |
---|
57 | n/a | _fields_ = [("style", c_uint), |
---|
58 | n/a | ("lpfnWndProc", WNDPROC), |
---|
59 | n/a | ("cbClsExtra", c_int), |
---|
60 | n/a | ("cbWndExtra", c_int), |
---|
61 | n/a | ("hInstance", HINSTANCE), |
---|
62 | n/a | ("hIcon", HICON), |
---|
63 | n/a | ("hCursor", HCURSOR), |
---|
64 | n/a | ("lpszMenuName", LPCTSTR), |
---|
65 | n/a | ("lpszClassName", LPCTSTR)] |
---|
66 | n/a | |
---|
67 | n/a | wndclass = WNDCLASS() |
---|
68 | n/a | wndclass.lpfnWndProc = WNDPROC(wndproc) |
---|
69 | n/a | |
---|
70 | n/a | WNDPROC_2 = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int) |
---|
71 | n/a | |
---|
72 | n/a | # This is no longer true, now that WINFUNCTYPE caches created types internally. |
---|
73 | n/a | ## # CFuncPtr subclasses are compared by identity, so this raises a TypeError: |
---|
74 | n/a | ## self.assertRaises(TypeError, setattr, wndclass, |
---|
75 | n/a | ## "lpfnWndProc", WNDPROC_2(wndproc)) |
---|
76 | n/a | # instead: |
---|
77 | n/a | |
---|
78 | n/a | self.assertIs(WNDPROC, WNDPROC_2) |
---|
79 | n/a | # 'wndclass.lpfnWndProc' leaks 94 references. Why? |
---|
80 | n/a | self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10) |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | f = wndclass.lpfnWndProc |
---|
84 | n/a | |
---|
85 | n/a | del wndclass |
---|
86 | n/a | del wndproc |
---|
87 | n/a | |
---|
88 | n/a | self.assertEqual(f(10, 11, 12, 13), 46) |
---|
89 | n/a | |
---|
90 | n/a | def test_dllfunctions(self): |
---|
91 | n/a | |
---|
92 | n/a | def NoNullHandle(value): |
---|
93 | n/a | if not value: |
---|
94 | n/a | raise WinError() |
---|
95 | n/a | return value |
---|
96 | n/a | |
---|
97 | n/a | strchr = lib.my_strchr |
---|
98 | n/a | strchr.restype = c_char_p |
---|
99 | n/a | strchr.argtypes = (c_char_p, c_char) |
---|
100 | n/a | self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi") |
---|
101 | n/a | self.assertEqual(strchr(b"abcdefghi", b"x"), None) |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | strtok = lib.my_strtok |
---|
105 | n/a | strtok.restype = c_char_p |
---|
106 | n/a | # Neither of this does work: strtok changes the buffer it is passed |
---|
107 | n/a | ## strtok.argtypes = (c_char_p, c_char_p) |
---|
108 | n/a | ## strtok.argtypes = (c_string, c_char_p) |
---|
109 | n/a | |
---|
110 | n/a | def c_string(init): |
---|
111 | n/a | size = len(init) + 1 |
---|
112 | n/a | return (c_char*size)(*init) |
---|
113 | n/a | |
---|
114 | n/a | s = b"a\nb\nc" |
---|
115 | n/a | b = c_string(s) |
---|
116 | n/a | |
---|
117 | n/a | ## b = (c_char * (len(s)+1))() |
---|
118 | n/a | ## b.value = s |
---|
119 | n/a | |
---|
120 | n/a | ## b = c_string(s) |
---|
121 | n/a | self.assertEqual(strtok(b, b"\n"), b"a") |
---|
122 | n/a | self.assertEqual(strtok(None, b"\n"), b"b") |
---|
123 | n/a | self.assertEqual(strtok(None, b"\n"), b"c") |
---|
124 | n/a | self.assertEqual(strtok(None, b"\n"), None) |
---|
125 | n/a | |
---|
126 | n/a | if __name__ == '__main__': |
---|
127 | n/a | unittest.main() |
---|