1 | n/a | from ctypes import * |
---|
2 | n/a | from ctypes.test import need_symbol |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | # IMPORTANT INFO: |
---|
6 | n/a | # |
---|
7 | n/a | # Consider this call: |
---|
8 | n/a | # func.restype = c_char_p |
---|
9 | n/a | # func(c_char_p("123")) |
---|
10 | n/a | # It returns |
---|
11 | n/a | # "123" |
---|
12 | n/a | # |
---|
13 | n/a | # WHY IS THIS SO? |
---|
14 | n/a | # |
---|
15 | n/a | # argument tuple (c_char_p("123"), ) is destroyed after the function |
---|
16 | n/a | # func is called, but NOT before the result is actually built. |
---|
17 | n/a | # |
---|
18 | n/a | # If the arglist would be destroyed BEFORE the result has been built, |
---|
19 | n/a | # the c_char_p("123") object would already have a zero refcount, |
---|
20 | n/a | # and the pointer passed to (and returned by) the function would |
---|
21 | n/a | # probably point to deallocated space. |
---|
22 | n/a | # |
---|
23 | n/a | # In this case, there would have to be an additional reference to the argument... |
---|
24 | n/a | |
---|
25 | n/a | import _ctypes_test |
---|
26 | n/a | testdll = CDLL(_ctypes_test.__file__) |
---|
27 | n/a | |
---|
28 | n/a | # Return machine address `a` as a (possibly long) non-negative integer. |
---|
29 | n/a | # Starting with Python 2.5, id(anything) is always non-negative, and |
---|
30 | n/a | # the ctypes addressof() inherits that via PyLong_FromVoidPtr(). |
---|
31 | n/a | def positive_address(a): |
---|
32 | n/a | if a >= 0: |
---|
33 | n/a | return a |
---|
34 | n/a | # View the bits in `a` as unsigned instead. |
---|
35 | n/a | import struct |
---|
36 | n/a | num_bits = struct.calcsize("P") * 8 # num bits in native machine address |
---|
37 | n/a | a += 1 << num_bits |
---|
38 | n/a | assert a >= 0 |
---|
39 | n/a | return a |
---|
40 | n/a | |
---|
41 | n/a | def c_wbuffer(init): |
---|
42 | n/a | n = len(init) + 1 |
---|
43 | n/a | return (c_wchar * n)(*init) |
---|
44 | n/a | |
---|
45 | n/a | class CharPointersTestCase(unittest.TestCase): |
---|
46 | n/a | |
---|
47 | n/a | def setUp(self): |
---|
48 | n/a | func = testdll._testfunc_p_p |
---|
49 | n/a | func.restype = c_long |
---|
50 | n/a | func.argtypes = None |
---|
51 | n/a | |
---|
52 | n/a | def test_paramflags(self): |
---|
53 | n/a | # function returns c_void_p result, |
---|
54 | n/a | # and has a required parameter named 'input' |
---|
55 | n/a | prototype = CFUNCTYPE(c_void_p, c_void_p) |
---|
56 | n/a | func = prototype(("_testfunc_p_p", testdll), |
---|
57 | n/a | ((1, "input"),)) |
---|
58 | n/a | |
---|
59 | n/a | try: |
---|
60 | n/a | func() |
---|
61 | n/a | except TypeError as details: |
---|
62 | n/a | self.assertEqual(str(details), "required argument 'input' missing") |
---|
63 | n/a | else: |
---|
64 | n/a | self.fail("TypeError not raised") |
---|
65 | n/a | |
---|
66 | n/a | self.assertEqual(func(None), None) |
---|
67 | n/a | self.assertEqual(func(input=None), None) |
---|
68 | n/a | |
---|
69 | n/a | |
---|
70 | n/a | def test_int_pointer_arg(self): |
---|
71 | n/a | func = testdll._testfunc_p_p |
---|
72 | n/a | if sizeof(c_longlong) == sizeof(c_void_p): |
---|
73 | n/a | func.restype = c_longlong |
---|
74 | n/a | else: |
---|
75 | n/a | func.restype = c_long |
---|
76 | n/a | self.assertEqual(0, func(0)) |
---|
77 | n/a | |
---|
78 | n/a | ci = c_int(0) |
---|
79 | n/a | |
---|
80 | n/a | func.argtypes = POINTER(c_int), |
---|
81 | n/a | self.assertEqual(positive_address(addressof(ci)), |
---|
82 | n/a | positive_address(func(byref(ci)))) |
---|
83 | n/a | |
---|
84 | n/a | func.argtypes = c_char_p, |
---|
85 | n/a | self.assertRaises(ArgumentError, func, byref(ci)) |
---|
86 | n/a | |
---|
87 | n/a | func.argtypes = POINTER(c_short), |
---|
88 | n/a | self.assertRaises(ArgumentError, func, byref(ci)) |
---|
89 | n/a | |
---|
90 | n/a | func.argtypes = POINTER(c_double), |
---|
91 | n/a | self.assertRaises(ArgumentError, func, byref(ci)) |
---|
92 | n/a | |
---|
93 | n/a | def test_POINTER_c_char_arg(self): |
---|
94 | n/a | func = testdll._testfunc_p_p |
---|
95 | n/a | func.restype = c_char_p |
---|
96 | n/a | func.argtypes = POINTER(c_char), |
---|
97 | n/a | |
---|
98 | n/a | self.assertEqual(None, func(None)) |
---|
99 | n/a | self.assertEqual(b"123", func(b"123")) |
---|
100 | n/a | self.assertEqual(None, func(c_char_p(None))) |
---|
101 | n/a | self.assertEqual(b"123", func(c_char_p(b"123"))) |
---|
102 | n/a | |
---|
103 | n/a | self.assertEqual(b"123", func(c_buffer(b"123"))) |
---|
104 | n/a | ca = c_char(b"a") |
---|
105 | n/a | self.assertEqual(ord(b"a"), func(pointer(ca))[0]) |
---|
106 | n/a | self.assertEqual(ord(b"a"), func(byref(ca))[0]) |
---|
107 | n/a | |
---|
108 | n/a | def test_c_char_p_arg(self): |
---|
109 | n/a | func = testdll._testfunc_p_p |
---|
110 | n/a | func.restype = c_char_p |
---|
111 | n/a | func.argtypes = c_char_p, |
---|
112 | n/a | |
---|
113 | n/a | self.assertEqual(None, func(None)) |
---|
114 | n/a | self.assertEqual(b"123", func(b"123")) |
---|
115 | n/a | self.assertEqual(None, func(c_char_p(None))) |
---|
116 | n/a | self.assertEqual(b"123", func(c_char_p(b"123"))) |
---|
117 | n/a | |
---|
118 | n/a | self.assertEqual(b"123", func(c_buffer(b"123"))) |
---|
119 | n/a | ca = c_char(b"a") |
---|
120 | n/a | self.assertEqual(ord(b"a"), func(pointer(ca))[0]) |
---|
121 | n/a | self.assertEqual(ord(b"a"), func(byref(ca))[0]) |
---|
122 | n/a | |
---|
123 | n/a | def test_c_void_p_arg(self): |
---|
124 | n/a | func = testdll._testfunc_p_p |
---|
125 | n/a | func.restype = c_char_p |
---|
126 | n/a | func.argtypes = c_void_p, |
---|
127 | n/a | |
---|
128 | n/a | self.assertEqual(None, func(None)) |
---|
129 | n/a | self.assertEqual(b"123", func(b"123")) |
---|
130 | n/a | self.assertEqual(b"123", func(c_char_p(b"123"))) |
---|
131 | n/a | self.assertEqual(None, func(c_char_p(None))) |
---|
132 | n/a | |
---|
133 | n/a | self.assertEqual(b"123", func(c_buffer(b"123"))) |
---|
134 | n/a | ca = c_char(b"a") |
---|
135 | n/a | self.assertEqual(ord(b"a"), func(pointer(ca))[0]) |
---|
136 | n/a | self.assertEqual(ord(b"a"), func(byref(ca))[0]) |
---|
137 | n/a | |
---|
138 | n/a | func(byref(c_int())) |
---|
139 | n/a | func(pointer(c_int())) |
---|
140 | n/a | func((c_int * 3)()) |
---|
141 | n/a | |
---|
142 | n/a | @need_symbol('c_wchar_p') |
---|
143 | n/a | def test_c_void_p_arg_with_c_wchar_p(self): |
---|
144 | n/a | func = testdll._testfunc_p_p |
---|
145 | n/a | func.restype = c_wchar_p |
---|
146 | n/a | func.argtypes = c_void_p, |
---|
147 | n/a | |
---|
148 | n/a | self.assertEqual(None, func(c_wchar_p(None))) |
---|
149 | n/a | self.assertEqual("123", func(c_wchar_p("123"))) |
---|
150 | n/a | |
---|
151 | n/a | def test_instance(self): |
---|
152 | n/a | func = testdll._testfunc_p_p |
---|
153 | n/a | func.restype = c_void_p |
---|
154 | n/a | |
---|
155 | n/a | class X: |
---|
156 | n/a | _as_parameter_ = None |
---|
157 | n/a | |
---|
158 | n/a | func.argtypes = c_void_p, |
---|
159 | n/a | self.assertEqual(None, func(X())) |
---|
160 | n/a | |
---|
161 | n/a | func.argtypes = None |
---|
162 | n/a | self.assertEqual(None, func(X())) |
---|
163 | n/a | |
---|
164 | n/a | @need_symbol('c_wchar') |
---|
165 | n/a | class WCharPointersTestCase(unittest.TestCase): |
---|
166 | n/a | |
---|
167 | n/a | def setUp(self): |
---|
168 | n/a | func = testdll._testfunc_p_p |
---|
169 | n/a | func.restype = c_int |
---|
170 | n/a | func.argtypes = None |
---|
171 | n/a | |
---|
172 | n/a | |
---|
173 | n/a | def test_POINTER_c_wchar_arg(self): |
---|
174 | n/a | func = testdll._testfunc_p_p |
---|
175 | n/a | func.restype = c_wchar_p |
---|
176 | n/a | func.argtypes = POINTER(c_wchar), |
---|
177 | n/a | |
---|
178 | n/a | self.assertEqual(None, func(None)) |
---|
179 | n/a | self.assertEqual("123", func("123")) |
---|
180 | n/a | self.assertEqual(None, func(c_wchar_p(None))) |
---|
181 | n/a | self.assertEqual("123", func(c_wchar_p("123"))) |
---|
182 | n/a | |
---|
183 | n/a | self.assertEqual("123", func(c_wbuffer("123"))) |
---|
184 | n/a | ca = c_wchar("a") |
---|
185 | n/a | self.assertEqual("a", func(pointer(ca))[0]) |
---|
186 | n/a | self.assertEqual("a", func(byref(ca))[0]) |
---|
187 | n/a | |
---|
188 | n/a | def test_c_wchar_p_arg(self): |
---|
189 | n/a | func = testdll._testfunc_p_p |
---|
190 | n/a | func.restype = c_wchar_p |
---|
191 | n/a | func.argtypes = c_wchar_p, |
---|
192 | n/a | |
---|
193 | n/a | c_wchar_p.from_param("123") |
---|
194 | n/a | |
---|
195 | n/a | self.assertEqual(None, func(None)) |
---|
196 | n/a | self.assertEqual("123", func("123")) |
---|
197 | n/a | self.assertEqual(None, func(c_wchar_p(None))) |
---|
198 | n/a | self.assertEqual("123", func(c_wchar_p("123"))) |
---|
199 | n/a | |
---|
200 | n/a | # XXX Currently, these raise TypeErrors, although they shouldn't: |
---|
201 | n/a | self.assertEqual("123", func(c_wbuffer("123"))) |
---|
202 | n/a | ca = c_wchar("a") |
---|
203 | n/a | self.assertEqual("a", func(pointer(ca))[0]) |
---|
204 | n/a | self.assertEqual("a", func(byref(ca))[0]) |
---|
205 | n/a | |
---|
206 | n/a | class ArrayTest(unittest.TestCase): |
---|
207 | n/a | def test(self): |
---|
208 | n/a | func = testdll._testfunc_ai8 |
---|
209 | n/a | func.restype = POINTER(c_int) |
---|
210 | n/a | func.argtypes = c_int * 8, |
---|
211 | n/a | |
---|
212 | n/a | func((c_int * 8)(1, 2, 3, 4, 5, 6, 7, 8)) |
---|
213 | n/a | |
---|
214 | n/a | # This did crash before: |
---|
215 | n/a | |
---|
216 | n/a | def func(): pass |
---|
217 | n/a | CFUNCTYPE(None, c_int * 3)(func) |
---|
218 | n/a | |
---|
219 | n/a | ################################################################ |
---|
220 | n/a | |
---|
221 | n/a | if __name__ == '__main__': |
---|
222 | n/a | unittest.main() |
---|