Debian Codegraph @

extra_tests/ctypes_tests/test_structures.py

raw symbols SCIP
1 from ctypes import *
2
3 import pytest
4 import sys
5 import weakref
6
7
8 def test_subclass_initializer():
9 class POINT(Structure):
10 _fields_ = [("x", c_int), ("y", c_int)]
11
12 class POSITION(POINT):
13 # A subclass without _fields_
14 pass
15 pos = POSITION(1, 2)
16 assert (pos.x, pos.y) == (1, 2)
17 # Try a second time, result may be different (cf. issue1498)
18 pos = POSITION(1, 2)
19 assert (pos.x, pos.y) == (1, 2)
20
21 def test_fields_is_a_tuple():
22 class Person(Structure):
23 _fields_ = (("name", c_char*6),
24 ("age", c_int))
25
26 # short enough
27 p = Person(b"123456", 6)
28 assert p.name == b"123456"
29 assert p.age == 6
30
31 def test___init__():
32 class Person(Structure):
33 _fields_ = (("name", c_char*10),
34 ("age", c_int))
35 def __init__(self, name, surname, age):
36 self.name = name + b' ' + surname
37 self.age = age
38
39 p = Person(b"John", b"Doe", 25)
40 assert p.name == b"John Doe"
41 assert p.age == 25
42
43 def test_setattr():
44 class X(Structure):
45 _fields_ = [("a", c_int)]
46
47 x = X()
48 x.other = 42
49 assert x.other == 42
50
51 def test_withslots():
52 class X(Structure):
53 _fields_ = [("a", c_int * 2)]
54 __slots__ = ['a']
55
56 x = X()
57 x.a = (42, 43)
58 assert tuple(x.a) == (42, 43)
59
60 def test_getattr_recursion():
61 # Structure.__getattr__ used to call itself recursively
62 # and hit the recursion limit.
63 import sys
64 events = []
65
66 def tracefunc(frame, event, arg):
67 funcname = frame.f_code.co_name
68 if 'getattr' in funcname:
69 events.append(funcname)
70
71 oldtrace = sys.settrace(tracefunc)
72 try:
73 class X(Structure):
74 _fields_ = [("a", c_int)]
75
76 assert len(events) < 20
77 finally:
78 sys.settrace(oldtrace)
79 events = None
80
81 def test_large_fields():
82 # make sure that large fields are not "confused" with bitfields
83 # (because the bitfields use the higher bits of the "size" attribute)
84 Array = c_long * 8192
85 class X(Structure):
86 _fields_ = [('items', Array)]
87 obj = X()
88 assert isinstance(obj.items, Array)
89
90 def test_b_base():
91 # _b_base_ used to be None here in PyPy
92 class X(Structure):
93 _fields_ = [('x', c_int)]
94 obj = X()
95 p = pointer(obj)
96 assert p.contents._b_base_ is p
97
98 def test_swapped_bytes():
99 import sys
100
101 for i in [c_short, c_int, c_long, c_longlong,
102 c_float, c_double, c_ushort, c_uint,
103 c_ulong, c_ulonglong]:
104 FIELDS = [
105 ('n', i)
106 ]
107
108 class Native(Structure):
109 _fields_ = FIELDS
110
111 class Big(BigEndianStructure):
112 _fields_ = FIELDS
113
114 class Little(LittleEndianStructure):
115 _fields_ = FIELDS
116
117 def dostruct(c):
118 ba = create_string_buffer(sizeof(c))
119 ms = c.from_buffer(ba)
120 ms.n = 0xff00
121 return repr(ba[:])
122
123 nstruct = dostruct(Native)
124 if sys.byteorder == 'little':
125 assert nstruct == dostruct(Little)
126 assert nstruct != dostruct(Big)
127 assert Big._fields_[0][1] is not i
128 else:
129 assert nstruct == dostruct(Big)
130 assert nstruct != dostruct(Little)
131 assert Little._fields_[0][1] is not i
132
133 def test_from_buffer_copy():
134 from array import array
135
136 class S(Structure):
137 _fields_ = [('i', c_int)]
138 def __init__(self, some, unused, arguments):
139 pass
140 a = array('i', [1234567])
141 s1 = S.from_buffer(a)
142 s2 = S.from_buffer_copy(a)
143 assert s1.i == 1234567
144 assert s2.i == 1234567
145 a[0] = -7654321
146 assert s1.i == -7654321
147 assert s2.i == 1234567
148
149
150 def test_nonfinal_struct():
151 class X(Structure):
152 pass
153 assert sizeof(X) == 0
154 X._fields_ = [("a", c_int),]
155 with pytest.raises(AttributeError):
156 X._fields_ = []
157
158 class X(Structure):
159 pass
160 X()
161 with pytest.raises(AttributeError):
162 X._fields_ = []
163
164 class X(Structure):
165 pass
166 class Y(X):
167 pass
168 with pytest.raises(AttributeError):
169 X._fields_ = []
170 Y.__fields__ = []
171
172
173 def test_structure_overloading_getattr():
174 class X(Structure):
175 _fields_ = [('x', c_int)]
176
177 def __getattr__(self, name):
178 raise AttributeError(name)
179
180 x = X()
181 assert x.x == 0
182
183 def test_duplicate_names():
184 class S(Structure):
185 _fields_ = [('a', c_int),
186 ('b', c_int),
187 ('a', c_byte)]
188 s = S(260, -123)
189 assert sizeof(s) == 3 * sizeof(c_int)
190 assert s.a == 4 # 256 + 4
191 assert s.b == -123
192
193 def test_memoryview():
194 class S(Structure):
195 _fields_ = [('a', c_int16),
196 ('b', c_int16),
197 ]
198
199 S3 = S * 3
200 c_array = (2 * S3)(
201 S3(S(a=0, b=1), S(a=2, b=3), S(a=4, b=5)),
202 S3(S(a=6, b=7), S(a=8, b=9), S(a=10, b=11)),
203 )
204
205 mv = memoryview(c_array)
206 if sys.byteorder == 'little':
207 assert mv.format == 'T{<h:a:<h:b:}'
208 else:
209 assert mv.format == 'T{>h:a:>h:b:}'
210 assert mv.shape == (2, 3)
211 assert mv.itemsize == 4
212
213 def test_weakref():
214 # issue 3883: py_object of weakref
215 class Empty():
216 pass
217
218 e = Empty()
219 r = weakref.ref(e)
220 pr = py_object(r)
221 assert pr.value() is e
222
223 def test_pyobject_of_int():
224 pr = py_object(17)
225 assert pr.value == 17
226
227 def test_memoryview_endian():
228 class LES(LittleEndianStructure):
229 _pack_ = 1
230 _fields_ = [
231 ('a', c_ubyte * 16),
232 ('i', c_uint64)
233 ]
234 c_les = LES()
235 mv = memoryview(c_les)
236 assert mv.format == 'B'
237
238 def test_deepcopy_struct():
239 # issue 3022: missing __new__ on StructureInstanceAutoFree
240 import copy
241
242 class struct_b(Structure):
243 _fields_ = [('value', c_int)]
244
245 class struct_a(Structure):
246 _fields_ = [('first',struct_b)]
247
248 a = struct_a()
249 a.first.value = 12
250 b = copy.deepcopy(a)
251 assert isinstance(b.first, struct_b)
252 assert b.first.value == 12