Debian Codegraph @

Lib/test/test_ctypes/test_prototypes.py

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