Debian Codegraph @

Lib/test/test_ctypes/test_pointers.py

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