1 | n/a | import unittest, sys |
---|
2 | n/a | |
---|
3 | n/a | from ctypes import * |
---|
4 | n/a | import _ctypes_test |
---|
5 | n/a | |
---|
6 | n/a | ctype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, |
---|
7 | n/a | c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float] |
---|
8 | n/a | python_types = [int, int, int, int, int, int, |
---|
9 | n/a | int, int, int, int, float, float] |
---|
10 | n/a | |
---|
11 | n/a | class PointersTestCase(unittest.TestCase): |
---|
12 | n/a | |
---|
13 | n/a | def test_pointer_crash(self): |
---|
14 | n/a | |
---|
15 | n/a | class A(POINTER(c_ulong)): |
---|
16 | n/a | pass |
---|
17 | n/a | |
---|
18 | n/a | POINTER(c_ulong)(c_ulong(22)) |
---|
19 | n/a | # Pointer can't set contents: has no _type_ |
---|
20 | n/a | self.assertRaises(TypeError, A, c_ulong(33)) |
---|
21 | n/a | |
---|
22 | n/a | def test_pass_pointers(self): |
---|
23 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
24 | n/a | func = dll._testfunc_p_p |
---|
25 | n/a | if sizeof(c_longlong) == sizeof(c_void_p): |
---|
26 | n/a | func.restype = c_longlong |
---|
27 | n/a | else: |
---|
28 | n/a | func.restype = c_long |
---|
29 | n/a | |
---|
30 | n/a | i = c_int(12345678) |
---|
31 | n/a | ## func.argtypes = (POINTER(c_int),) |
---|
32 | n/a | address = func(byref(i)) |
---|
33 | n/a | self.assertEqual(c_int.from_address(address).value, 12345678) |
---|
34 | n/a | |
---|
35 | n/a | func.restype = POINTER(c_int) |
---|
36 | n/a | res = func(pointer(i)) |
---|
37 | n/a | self.assertEqual(res.contents.value, 12345678) |
---|
38 | n/a | self.assertEqual(res[0], 12345678) |
---|
39 | n/a | |
---|
40 | n/a | def test_change_pointers(self): |
---|
41 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
42 | n/a | func = dll._testfunc_p_p |
---|
43 | n/a | |
---|
44 | n/a | i = c_int(87654) |
---|
45 | n/a | func.restype = POINTER(c_int) |
---|
46 | n/a | func.argtypes = (POINTER(c_int),) |
---|
47 | n/a | |
---|
48 | n/a | res = func(pointer(i)) |
---|
49 | n/a | self.assertEqual(res[0], 87654) |
---|
50 | n/a | self.assertEqual(res.contents.value, 87654) |
---|
51 | n/a | |
---|
52 | n/a | # C code: *res = 54345 |
---|
53 | n/a | res[0] = 54345 |
---|
54 | n/a | self.assertEqual(i.value, 54345) |
---|
55 | n/a | |
---|
56 | n/a | # C code: |
---|
57 | n/a | # int x = 12321; |
---|
58 | n/a | # res = &x |
---|
59 | n/a | x = c_int(12321) |
---|
60 | n/a | res.contents = x |
---|
61 | n/a | self.assertEqual(i.value, 54345) |
---|
62 | n/a | |
---|
63 | n/a | x.value = -99 |
---|
64 | n/a | self.assertEqual(res.contents.value, -99) |
---|
65 | n/a | |
---|
66 | n/a | def test_callbacks_with_pointers(self): |
---|
67 | n/a | # a function type receiving a pointer |
---|
68 | n/a | PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int)) |
---|
69 | n/a | |
---|
70 | n/a | self.result = [] |
---|
71 | n/a | |
---|
72 | n/a | def func(arg): |
---|
73 | n/a | for i in range(10): |
---|
74 | n/a | ## print arg[i], |
---|
75 | n/a | self.result.append(arg[i]) |
---|
76 | n/a | ## print |
---|
77 | n/a | return 0 |
---|
78 | n/a | callback = PROTOTYPE(func) |
---|
79 | n/a | |
---|
80 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
81 | n/a | # This function expects a function pointer, |
---|
82 | n/a | # and calls this with an integer pointer as parameter. |
---|
83 | n/a | # The int pointer points to a table containing the numbers 1..10 |
---|
84 | n/a | doit = dll._testfunc_callback_with_pointer |
---|
85 | n/a | |
---|
86 | n/a | ## i = c_int(42) |
---|
87 | n/a | ## callback(byref(i)) |
---|
88 | n/a | ## self.assertEqual(i.value, 84) |
---|
89 | n/a | |
---|
90 | n/a | doit(callback) |
---|
91 | n/a | ## print self.result |
---|
92 | n/a | doit(callback) |
---|
93 | n/a | ## print self.result |
---|
94 | n/a | |
---|
95 | n/a | def test_basics(self): |
---|
96 | n/a | from operator import delitem |
---|
97 | n/a | for ct, pt in zip(ctype_types, python_types): |
---|
98 | n/a | i = ct(42) |
---|
99 | n/a | p = pointer(i) |
---|
100 | n/a | ## print type(p.contents), ct |
---|
101 | n/a | self.assertIs(type(p.contents), ct) |
---|
102 | n/a | # p.contents is the same as p[0] |
---|
103 | n/a | ## print p.contents |
---|
104 | n/a | ## self.assertEqual(p.contents, 42) |
---|
105 | n/a | ## self.assertEqual(p[0], 42) |
---|
106 | n/a | |
---|
107 | n/a | self.assertRaises(TypeError, delitem, p, 0) |
---|
108 | n/a | |
---|
109 | n/a | def test_from_address(self): |
---|
110 | n/a | from array import array |
---|
111 | n/a | a = array('i', [100, 200, 300, 400, 500]) |
---|
112 | n/a | addr = a.buffer_info()[0] |
---|
113 | n/a | |
---|
114 | n/a | p = POINTER(POINTER(c_int)) |
---|
115 | n/a | ## print dir(p) |
---|
116 | n/a | ## print p.from_address |
---|
117 | n/a | ## print p.from_address(addr)[0][0] |
---|
118 | n/a | |
---|
119 | n/a | def test_other(self): |
---|
120 | n/a | class Table(Structure): |
---|
121 | n/a | _fields_ = [("a", c_int), |
---|
122 | n/a | ("b", c_int), |
---|
123 | n/a | ("c", c_int)] |
---|
124 | n/a | |
---|
125 | n/a | pt = pointer(Table(1, 2, 3)) |
---|
126 | n/a | |
---|
127 | n/a | self.assertEqual(pt.contents.a, 1) |
---|
128 | n/a | self.assertEqual(pt.contents.b, 2) |
---|
129 | n/a | self.assertEqual(pt.contents.c, 3) |
---|
130 | n/a | |
---|
131 | n/a | pt.contents.c = 33 |
---|
132 | n/a | |
---|
133 | n/a | from ctypes import _pointer_type_cache |
---|
134 | n/a | del _pointer_type_cache[Table] |
---|
135 | n/a | |
---|
136 | n/a | def test_basic(self): |
---|
137 | n/a | p = pointer(c_int(42)) |
---|
138 | n/a | # Although a pointer can be indexed, it has no length |
---|
139 | n/a | self.assertRaises(TypeError, len, p) |
---|
140 | n/a | self.assertEqual(p[0], 42) |
---|
141 | n/a | self.assertEqual(p[0:1], [42]) |
---|
142 | n/a | self.assertEqual(p.contents.value, 42) |
---|
143 | n/a | |
---|
144 | n/a | def test_charpp(self): |
---|
145 | n/a | """Test that a character pointer-to-pointer is correctly passed""" |
---|
146 | n/a | dll = CDLL(_ctypes_test.__file__) |
---|
147 | n/a | func = dll._testfunc_c_p_p |
---|
148 | n/a | func.restype = c_char_p |
---|
149 | n/a | argv = (c_char_p * 2)() |
---|
150 | n/a | argc = c_int( 2 ) |
---|
151 | n/a | argv[0] = b'hello' |
---|
152 | n/a | argv[1] = b'world' |
---|
153 | n/a | result = func( byref(argc), argv ) |
---|
154 | n/a | self.assertEqual(result, b'world') |
---|
155 | n/a | |
---|
156 | n/a | def test_bug_1467852(self): |
---|
157 | n/a | # http://sourceforge.net/tracker/?func=detail&atid=532154&aid=1467852&group_id=71702 |
---|
158 | n/a | x = c_int(5) |
---|
159 | n/a | dummy = [] |
---|
160 | n/a | for i in range(32000): |
---|
161 | n/a | dummy.append(c_int(i)) |
---|
162 | n/a | y = c_int(6) |
---|
163 | n/a | p = pointer(x) |
---|
164 | n/a | pp = pointer(p) |
---|
165 | n/a | q = pointer(y) |
---|
166 | n/a | pp[0] = q # <== |
---|
167 | n/a | self.assertEqual(p[0], 6) |
---|
168 | n/a | def test_c_void_p(self): |
---|
169 | n/a | # http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470 |
---|
170 | n/a | if sizeof(c_void_p) == 4: |
---|
171 | n/a | self.assertEqual(c_void_p(0xFFFFFFFF).value, |
---|
172 | n/a | c_void_p(-1).value) |
---|
173 | n/a | self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value, |
---|
174 | n/a | c_void_p(-1).value) |
---|
175 | n/a | elif sizeof(c_void_p) == 8: |
---|
176 | n/a | self.assertEqual(c_void_p(0xFFFFFFFF).value, |
---|
177 | n/a | 0xFFFFFFFF) |
---|
178 | n/a | self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value, |
---|
179 | n/a | c_void_p(-1).value) |
---|
180 | n/a | self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFF).value, |
---|
181 | n/a | c_void_p(-1).value) |
---|
182 | n/a | |
---|
183 | n/a | self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted |
---|
184 | n/a | self.assertRaises(TypeError, c_void_p, object()) # nor other objects |
---|
185 | n/a | |
---|
186 | n/a | def test_pointers_bool(self): |
---|
187 | n/a | # NULL pointers have a boolean False value, non-NULL pointers True. |
---|
188 | n/a | self.assertEqual(bool(POINTER(c_int)()), False) |
---|
189 | n/a | self.assertEqual(bool(pointer(c_int())), True) |
---|
190 | n/a | |
---|
191 | n/a | self.assertEqual(bool(CFUNCTYPE(None)(0)), False) |
---|
192 | n/a | self.assertEqual(bool(CFUNCTYPE(None)(42)), True) |
---|
193 | n/a | |
---|
194 | n/a | # COM methods are boolean True: |
---|
195 | n/a | if sys.platform == "win32": |
---|
196 | n/a | mth = WINFUNCTYPE(None)(42, "name", (), None) |
---|
197 | n/a | self.assertEqual(bool(mth), True) |
---|
198 | n/a | |
---|
199 | n/a | def test_pointer_type_name(self): |
---|
200 | n/a | LargeNamedType = type('T' * 2 ** 25, (Structure,), {}) |
---|
201 | n/a | self.assertTrue(POINTER(LargeNamedType)) |
---|
202 | n/a | |
---|
203 | n/a | # to not leak references, we must clean _pointer_type_cache |
---|
204 | n/a | from ctypes import _pointer_type_cache |
---|
205 | n/a | del _pointer_type_cache[LargeNamedType] |
---|
206 | n/a | |
---|
207 | n/a | def test_pointer_type_str_name(self): |
---|
208 | n/a | large_string = 'T' * 2 ** 25 |
---|
209 | n/a | P = POINTER(large_string) |
---|
210 | n/a | self.assertTrue(P) |
---|
211 | n/a | |
---|
212 | n/a | # to not leak references, we must clean _pointer_type_cache |
---|
213 | n/a | from ctypes import _pointer_type_cache |
---|
214 | n/a | del _pointer_type_cache[id(P)] |
---|
215 | n/a | |
---|
216 | n/a | |
---|
217 | n/a | if __name__ == '__main__': |
---|
218 | n/a | unittest.main() |
---|