7
# func.restype = c_char_p
8
# func(c_char_p("123"))
14
# argument tuple (c_char_p("123"), ) is destroyed after the function
15
# func is called, but NOT before the result is actually built.
17
# If the arglist would be destroyed BEFORE the result has been built,
18
# the c_char_p("123") object would already have a zero refcount,
19
# and the pointer passed to (and returned by) the function would
20
# probably point to deallocated space.
22
# In this case, there would have to be an additional reference to the argument...
25
testdll = CDLL(_ctypes_test.__file__)
27
# Return machine address `a` as a (possibly long) non-negative integer.
28
# Starting with Python 2.5, id(anything) is always non-negative, and
29
# the ctypes addressof() inherits that via PyLong_FromVoidPtr().
30
def positive_address(a):
33
# View the bits in `a` as unsigned instead.
35
num_bits = struct.calcsize("P") * 8 # num bits in native machine address
42
return (c_wchar * n)(*init)
44
class CharPointersTestCase(unittest.TestCase):
47
func = testdll._testfunc_p_p
51
def test_paramflags(self):
52
# function returns c_void_p result,
53
# and has a required parameter named 'input'
54
prototype = CFUNCTYPE(c_void_p, c_void_p)
55
func = prototype(("_testfunc_p_p", testdll),
60
except TypeError as details:
61
self.assertEqual(str(details), "required argument 'input' missing")
63
self.fail("TypeError not raised")
65
self.assertEqual(func(None), None)
66
self.assertEqual(func(input=None), None)
69
def test_int_pointer_arg(self):
70
func = testdll._testfunc_p_p
72
self.assertEqual(0, func(0))
76
func.argtypes = POINTER(c_int),
77
self.assertEqual(positive_address(addressof(ci)),
78
positive_address(func(byref(ci))))
80
func.argtypes = c_char_p,
81
self.assertRaises(ArgumentError, func, byref(ci))
83
func.argtypes = POINTER(c_short),
84
self.assertRaises(ArgumentError, func, byref(ci))
86
func.argtypes = POINTER(c_double),
87
self.assertRaises(ArgumentError, func, byref(ci))
89
def test_POINTER_c_char_arg(self):
90
func = testdll._testfunc_p_p
91
func.restype = c_char_p
92
func.argtypes = POINTER(c_char),
94
self.assertEqual(None, func(None))
95
self.assertEqual(b"123", func(b"123"))
96
self.assertEqual(None, func(c_char_p(None)))
97
self.assertEqual(b"123", func(c_char_p(b"123")))
99
self.assertEqual(b"123", func(c_buffer(b"123")))
101
self.assertEqual(ord(b"a"), func(pointer(ca))[0])
102
self.assertEqual(ord(b"a"), func(byref(ca))[0])
104
def test_c_char_p_arg(self):
105
func = testdll._testfunc_p_p
106
func.restype = c_char_p
107
func.argtypes = c_char_p,
109
self.assertEqual(None, func(None))
110
self.assertEqual(b"123", func(b"123"))
111
self.assertEqual(None, func(c_char_p(None)))
112
self.assertEqual(b"123", func(c_char_p(b"123")))
114
self.assertEqual(b"123", func(c_buffer(b"123")))
116
self.assertEqual(ord(b"a"), func(pointer(ca))[0])
117
self.assertEqual(ord(b"a"), func(byref(ca))[0])
119
def test_c_void_p_arg(self):
120
func = testdll._testfunc_p_p
121
func.restype = c_char_p
122
func.argtypes = c_void_p,
124
self.assertEqual(None, func(None))
125
self.assertEqual(b"123", func(b"123"))
126
self.assertEqual(b"123", func(c_char_p(b"123")))
127
self.assertEqual(None, func(c_char_p(None)))
129
self.assertEqual(b"123", func(c_buffer(b"123")))
131
self.assertEqual(ord(b"a"), func(pointer(ca))[0])
132
self.assertEqual(ord(b"a"), func(byref(ca))[0])
135
func(pointer(c_int()))
139
func.restype = c_wchar_p
143
self.assertEqual(None, func(c_wchar_p(None)))
144
self.assertEqual("123", func(c_wchar_p("123")))
146
def test_instance(self):
147
func = testdll._testfunc_p_p
148
func.restype = c_void_p
151
_as_parameter_ = None
153
func.argtypes = c_void_p,
154
self.assertEqual(None, func(X()))
157
self.assertEqual(None, func(X()))
164
class WCharPointersTestCase(unittest.TestCase):
167
func = testdll._testfunc_p_p
172
def test_POINTER_c_wchar_arg(self):
173
func = testdll._testfunc_p_p
174
func.restype = c_wchar_p
175
func.argtypes = POINTER(c_wchar),
177
self.assertEqual(None, func(None))
178
self.assertEqual("123", func("123"))
179
self.assertEqual(None, func(c_wchar_p(None)))
180
self.assertEqual("123", func(c_wchar_p("123")))
182
self.assertEqual("123", func(c_wbuffer("123")))
184
self.assertEqual("a", func(pointer(ca))[0])
185
self.assertEqual("a", func(byref(ca))[0])
187
def test_c_wchar_p_arg(self):
188
func = testdll._testfunc_p_p
189
func.restype = c_wchar_p
190
func.argtypes = c_wchar_p,
192
c_wchar_p.from_param("123")
194
self.assertEqual(None, func(None))
195
self.assertEqual("123", func("123"))
196
self.assertEqual(None, func(c_wchar_p(None)))
197
self.assertEqual("123", func(c_wchar_p("123")))
199
# XXX Currently, these raise TypeErrors, although they shouldn't:
200
self.assertEqual("123", func(c_wbuffer("123")))
202
self.assertEqual("a", func(pointer(ca))[0])
203
self.assertEqual("a", func(byref(ca))[0])
205
class ArrayTest(unittest.TestCase):
207
func = testdll._testfunc_ai8
208
func.restype = POINTER(c_int)
209
func.argtypes = c_int * 8,
211
func((c_int * 8)(1, 2, 3, 4, 5, 6, 7, 8))
213
# This did crash before:
216
CFUNCTYPE(None, c_int * 3)(func)
218
################################################################
220
if __name__ == '__main__':