Debian Codegraph @

extra_tests/ctypes_tests/test_keepalive.py

raw symbols SCIP
1 """ Tests whether various objects land in _objects
2 or not
3 """
4 import pytest
5
6 from ctypes import *
7 import sys
8
9 def test_array_of_pointers():
10 # tests array item assignements & pointer.contents = ...
11 A = POINTER(c_long) * 24
12 a = A()
13 l = c_long(2)
14 p = pointer(l)
15 a[3] = p
16 assert l._objects is None
17 assert p._objects == {'1':l}
18 assert a._objects == {'3':{'1':l}}
19
20 def test_simple_structure_and_pointer():
21 class X(Structure):
22 _fields_ = [('x', POINTER(c_int))]
23
24 x = X()
25 p = POINTER(c_int)()
26 assert x._objects is None
27 assert p._objects is None
28 x.x = p
29 assert p._objects == {}
30 assert len(x._objects) == 1
31 assert x._objects['0'] is p._objects
32
33 def test_simple_structure_and_pointer_with_array():
34 class X(Structure):
35 _fields_ = [('array', POINTER(c_int))]
36
37 x = X()
38 a = (c_int * 3)(1, 2, 3)
39 assert x._objects is None
40 x.array = a
41 assert x._objects['0'] is a
42
43 def test_structure_with_pointers():
44 class X(Structure):
45 _fields_ = [('x', POINTER(c_int)),
46 ('y', POINTER(c_int))]
47
48 x = X()
49 u = c_int(3)
50 p = pointer(u)
51 x.x = p
52 assert x.x._objects is None
53 assert p._objects == {'1': u}
54 assert x._objects == {'0': p._objects}
55
56 w = c_int(4)
57 q = pointer(w)
58 x.y = q
59 assert p._objects == {'1': u}
60 assert q._objects == {'1': w}
61 assert x._objects == {'0': p._objects, '1': q._objects}
62
63 n = POINTER(c_int)()
64 x.x = n
65 x.y = n
66 assert x._objects == {'0': n._objects, '1': n._objects}
67 assert x._objects['0'] is n._objects
68 assert n._objects is not None
69
70 def test_union_with_pointers():
71 class X(Union):
72 _fields_ = [('x', POINTER(c_int)),
73 ('y', POINTER(c_int))]
74
75 x = X()
76 u = c_int(3)
77 p = pointer(u)
78 x.x = p
79 assert x.x._objects is None
80 assert p._objects == {'1': u}
81 assert x._objects == {'0': p._objects}
82
83 # unions works just like structures it seems
84 w = c_int(4)
85 q = pointer(w)
86 x.y = q
87 assert p._objects == {'1': u}
88 assert q._objects == {'1': w}
89 assert x._objects == {'0': p._objects, '1': q._objects}
90
91 n = POINTER(c_int)()
92 x.x = n
93 x.y = n
94 assert x._objects == {'0': n._objects, '1': n._objects}
95 assert x._objects['0'] is n._objects
96 assert n._objects is not None
97
98 def test_pointer_setitem():
99 x = c_int(2)
100 y = c_int(3)
101 p = pointer(x)
102 assert p._objects == {'1':x}
103 p[0] = y
104 assert list(p._objects.keys()) == ['1']
105 assert p._objects['1'].value == 3
106
107 @pytest.mark.pypy_only
108 def test_primitive():
109 assert c_char_p(b"abc")._objects._buffer[0] == b"a"
110 assert c_int(3)._objects is None
111
112 def test_pointer_to_pointer():
113 l = c_long(2)
114 assert l._objects is None
115
116 p1 = pointer(l)
117 assert p1._objects == {'1':l}
118
119 p2 = pointer(p1)
120 assert p2._objects == {'1':p1, '0':{'1':l}}
121
122 def test_cfunc():
123 def f():
124 pass
125 cf = CFUNCTYPE(c_int, c_int)(f)
126 assert cf._objects == {'0':cf}
127
128 def test_cfunc_cast():
129 def f():
130 pass
131 cf = CFUNCTYPE(c_int, c_int)(f)
132 p1 = cast(cf, c_void_p)
133 assert p1._objects == {id(cf): cf, '0': cf}
134
135 def test_array_of_struct_with_pointer():
136 class S(Structure):
137 _fields_ = [('x', c_int)]
138 PS = POINTER(S)
139
140 class Q(Structure):
141 _fields_ = [('p', PS)]
142
143 A = Q*10
144 a=A()
145 s=S()
146 s.x=3
147 a[3].p = pointer(s)
148
149 assert a._objects['0:3']['1'] is s
150
151 def test_array_of_union_with_pointer():
152 class S(Structure):
153 _fields_ = [('x', c_int)]
154 PS = POINTER(S)
155
156 class Q(Union):
157 _fields_ = [('p', PS), ('x', c_int)]
158
159 A = Q*10
160 a=A()
161 s=S()
162 s.x=3
163 a[3].p = pointer(s)
164
165 assert a._objects['0:3']['1'] is s
166
167 def test_struct_with_inlined_array():
168 class S(Structure):
169 _fields_ = [('b', c_int),
170 ('a', POINTER(c_int) * 2)]
171
172 s = S()
173 stuff = c_int(2)
174 s.a[1] = pointer(stuff)
175 assert s._objects == {'1:1': {'1': stuff}}
176
177 def test_union_with_inlined_array():
178 class S(Union):
179 _fields_ = [('b', c_int),
180 ('a', POINTER(c_int) * 2)]
181
182 s = S()
183 stuff = c_int(2)
184 s.a[1] = pointer(stuff)
185 assert s._objects == {'1:1': {'1': stuff}}
186
187 def test_struct_within_struct():
188 class R(Structure):
189 _fields_ = [('p', POINTER(c_int))]
190
191 class S(Structure):
192 _fields_ = [('b', c_int),
193 ('r', R)]
194
195 s = S()
196 stuff = c_int(2)
197 s.r.p = pointer(stuff)
198 assert s._objects == {'0:1': {'1': stuff}}
199
200 r = R()
201 s.r = r
202 # obscure
203 assert s._objects == {'1': {}, '0:1': {'1': stuff}}
204
205 def test_union_within_union():
206 class R(Union):
207 _fields_ = [('p', POINTER(c_int))]
208
209 class S(Union):
210 _fields_ = [('b', c_int),
211 ('r', R)]
212
213 s = S()
214 stuff = c_int(2)
215 s.r.p = pointer(stuff)
216 assert s._objects == {'0:1': {'1': stuff}}
217
218 r = R()
219 s.r = r
220 # obscure
221 assert s._objects == {'1': {}, '0:1': {'1': stuff}}
222
223 def test_c_char_p():
224 n = 2
225 xs = b"hello" * n
226 x = c_char_p(xs)
227 del xs
228 import gc; gc.collect()
229 assert x.value == b'hellohello'
230 assert x._objects == b'hellohello'
231 #
232 class datum(Structure):
233 _fields_ = [
234 ('dptr', c_char_p),
235 ('dsize', c_int),
236 ]
237 class union(Union):
238 _fields_ = [
239 ('dptr', c_char_p),
240 ('dsize', c_int),
241 ]
242 for wrap in [False, True]:
243 n = 2
244 xs = b"hello" * n
245 if wrap:
246 xs = c_char_p(xs)
247 dat = datum()
248 dat.dptr = xs
249 dat.dsize = 15
250 del xs
251 import gc; gc.collect()
252 assert dat.dptr == b"hellohello"
253 assert list(dat._objects.keys()) == ['0']
254
255 xs = b"hello" * n
256 if wrap:
257 xs = c_char_p(xs)
258 dat = union()
259 dat.dptr = xs
260 del xs
261 import gc; gc.collect()
262 assert dat.dptr == b"hellohello"
263 assert list(dat._objects.keys()) == ['0']
264
265
266 def test_cast_addr_wrapped_in_pyfunctype_bug():
267 a = (c_int * 10)(*range(10))
268 p = cast(a, POINTER(c_int))
269 assert p._objects[id(a)] is a