1 | n/a | import unittest |
---|
2 | n/a | from ctypes import * |
---|
3 | n/a | from ctypes.test import need_symbol |
---|
4 | n/a | from struct import calcsize |
---|
5 | n/a | import _testcapi |
---|
6 | n/a | |
---|
7 | n/a | class SubclassesTest(unittest.TestCase): |
---|
8 | n/a | def test_subclass(self): |
---|
9 | n/a | class X(Structure): |
---|
10 | n/a | _fields_ = [("a", c_int)] |
---|
11 | n/a | |
---|
12 | n/a | class Y(X): |
---|
13 | n/a | _fields_ = [("b", c_int)] |
---|
14 | n/a | |
---|
15 | n/a | class Z(X): |
---|
16 | n/a | pass |
---|
17 | n/a | |
---|
18 | n/a | self.assertEqual(sizeof(X), sizeof(c_int)) |
---|
19 | n/a | self.assertEqual(sizeof(Y), sizeof(c_int)*2) |
---|
20 | n/a | self.assertEqual(sizeof(Z), sizeof(c_int)) |
---|
21 | n/a | self.assertEqual(X._fields_, [("a", c_int)]) |
---|
22 | n/a | self.assertEqual(Y._fields_, [("b", c_int)]) |
---|
23 | n/a | self.assertEqual(Z._fields_, [("a", c_int)]) |
---|
24 | n/a | |
---|
25 | n/a | def test_subclass_delayed(self): |
---|
26 | n/a | class X(Structure): |
---|
27 | n/a | pass |
---|
28 | n/a | self.assertEqual(sizeof(X), 0) |
---|
29 | n/a | X._fields_ = [("a", c_int)] |
---|
30 | n/a | |
---|
31 | n/a | class Y(X): |
---|
32 | n/a | pass |
---|
33 | n/a | self.assertEqual(sizeof(Y), sizeof(X)) |
---|
34 | n/a | Y._fields_ = [("b", c_int)] |
---|
35 | n/a | |
---|
36 | n/a | class Z(X): |
---|
37 | n/a | pass |
---|
38 | n/a | |
---|
39 | n/a | self.assertEqual(sizeof(X), sizeof(c_int)) |
---|
40 | n/a | self.assertEqual(sizeof(Y), sizeof(c_int)*2) |
---|
41 | n/a | self.assertEqual(sizeof(Z), sizeof(c_int)) |
---|
42 | n/a | self.assertEqual(X._fields_, [("a", c_int)]) |
---|
43 | n/a | self.assertEqual(Y._fields_, [("b", c_int)]) |
---|
44 | n/a | self.assertEqual(Z._fields_, [("a", c_int)]) |
---|
45 | n/a | |
---|
46 | n/a | class StructureTestCase(unittest.TestCase): |
---|
47 | n/a | formats = {"c": c_char, |
---|
48 | n/a | "b": c_byte, |
---|
49 | n/a | "B": c_ubyte, |
---|
50 | n/a | "h": c_short, |
---|
51 | n/a | "H": c_ushort, |
---|
52 | n/a | "i": c_int, |
---|
53 | n/a | "I": c_uint, |
---|
54 | n/a | "l": c_long, |
---|
55 | n/a | "L": c_ulong, |
---|
56 | n/a | "q": c_longlong, |
---|
57 | n/a | "Q": c_ulonglong, |
---|
58 | n/a | "f": c_float, |
---|
59 | n/a | "d": c_double, |
---|
60 | n/a | } |
---|
61 | n/a | |
---|
62 | n/a | def test_simple_structs(self): |
---|
63 | n/a | for code, tp in self.formats.items(): |
---|
64 | n/a | class X(Structure): |
---|
65 | n/a | _fields_ = [("x", c_char), |
---|
66 | n/a | ("y", tp)] |
---|
67 | n/a | self.assertEqual((sizeof(X), code), |
---|
68 | n/a | (calcsize("c%c0%c" % (code, code)), code)) |
---|
69 | n/a | |
---|
70 | n/a | def test_unions(self): |
---|
71 | n/a | for code, tp in self.formats.items(): |
---|
72 | n/a | class X(Union): |
---|
73 | n/a | _fields_ = [("x", c_char), |
---|
74 | n/a | ("y", tp)] |
---|
75 | n/a | self.assertEqual((sizeof(X), code), |
---|
76 | n/a | (calcsize("%c" % (code)), code)) |
---|
77 | n/a | |
---|
78 | n/a | def test_struct_alignment(self): |
---|
79 | n/a | class X(Structure): |
---|
80 | n/a | _fields_ = [("x", c_char * 3)] |
---|
81 | n/a | self.assertEqual(alignment(X), calcsize("s")) |
---|
82 | n/a | self.assertEqual(sizeof(X), calcsize("3s")) |
---|
83 | n/a | |
---|
84 | n/a | class Y(Structure): |
---|
85 | n/a | _fields_ = [("x", c_char * 3), |
---|
86 | n/a | ("y", c_int)] |
---|
87 | n/a | self.assertEqual(alignment(Y), alignment(c_int)) |
---|
88 | n/a | self.assertEqual(sizeof(Y), calcsize("3si")) |
---|
89 | n/a | |
---|
90 | n/a | class SI(Structure): |
---|
91 | n/a | _fields_ = [("a", X), |
---|
92 | n/a | ("b", Y)] |
---|
93 | n/a | self.assertEqual(alignment(SI), max(alignment(Y), alignment(X))) |
---|
94 | n/a | self.assertEqual(sizeof(SI), calcsize("3s0i 3si 0i")) |
---|
95 | n/a | |
---|
96 | n/a | class IS(Structure): |
---|
97 | n/a | _fields_ = [("b", Y), |
---|
98 | n/a | ("a", X)] |
---|
99 | n/a | |
---|
100 | n/a | self.assertEqual(alignment(SI), max(alignment(X), alignment(Y))) |
---|
101 | n/a | self.assertEqual(sizeof(IS), calcsize("3si 3s 0i")) |
---|
102 | n/a | |
---|
103 | n/a | class XX(Structure): |
---|
104 | n/a | _fields_ = [("a", X), |
---|
105 | n/a | ("b", X)] |
---|
106 | n/a | self.assertEqual(alignment(XX), alignment(X)) |
---|
107 | n/a | self.assertEqual(sizeof(XX), calcsize("3s 3s 0s")) |
---|
108 | n/a | |
---|
109 | n/a | def test_empty(self): |
---|
110 | n/a | # I had problems with these |
---|
111 | n/a | # |
---|
112 | n/a | # Although these are pathological cases: Empty Structures! |
---|
113 | n/a | class X(Structure): |
---|
114 | n/a | _fields_ = [] |
---|
115 | n/a | |
---|
116 | n/a | class Y(Union): |
---|
117 | n/a | _fields_ = [] |
---|
118 | n/a | |
---|
119 | n/a | # Is this really the correct alignment, or should it be 0? |
---|
120 | n/a | self.assertTrue(alignment(X) == alignment(Y) == 1) |
---|
121 | n/a | self.assertTrue(sizeof(X) == sizeof(Y) == 0) |
---|
122 | n/a | |
---|
123 | n/a | class XX(Structure): |
---|
124 | n/a | _fields_ = [("a", X), |
---|
125 | n/a | ("b", X)] |
---|
126 | n/a | |
---|
127 | n/a | self.assertEqual(alignment(XX), 1) |
---|
128 | n/a | self.assertEqual(sizeof(XX), 0) |
---|
129 | n/a | |
---|
130 | n/a | def test_fields(self): |
---|
131 | n/a | # test the offset and size attributes of Structure/Unoin fields. |
---|
132 | n/a | class X(Structure): |
---|
133 | n/a | _fields_ = [("x", c_int), |
---|
134 | n/a | ("y", c_char)] |
---|
135 | n/a | |
---|
136 | n/a | self.assertEqual(X.x.offset, 0) |
---|
137 | n/a | self.assertEqual(X.x.size, sizeof(c_int)) |
---|
138 | n/a | |
---|
139 | n/a | self.assertEqual(X.y.offset, sizeof(c_int)) |
---|
140 | n/a | self.assertEqual(X.y.size, sizeof(c_char)) |
---|
141 | n/a | |
---|
142 | n/a | # readonly |
---|
143 | n/a | self.assertRaises((TypeError, AttributeError), setattr, X.x, "offset", 92) |
---|
144 | n/a | self.assertRaises((TypeError, AttributeError), setattr, X.x, "size", 92) |
---|
145 | n/a | |
---|
146 | n/a | class X(Union): |
---|
147 | n/a | _fields_ = [("x", c_int), |
---|
148 | n/a | ("y", c_char)] |
---|
149 | n/a | |
---|
150 | n/a | self.assertEqual(X.x.offset, 0) |
---|
151 | n/a | self.assertEqual(X.x.size, sizeof(c_int)) |
---|
152 | n/a | |
---|
153 | n/a | self.assertEqual(X.y.offset, 0) |
---|
154 | n/a | self.assertEqual(X.y.size, sizeof(c_char)) |
---|
155 | n/a | |
---|
156 | n/a | # readonly |
---|
157 | n/a | self.assertRaises((TypeError, AttributeError), setattr, X.x, "offset", 92) |
---|
158 | n/a | self.assertRaises((TypeError, AttributeError), setattr, X.x, "size", 92) |
---|
159 | n/a | |
---|
160 | n/a | # XXX Should we check nested data types also? |
---|
161 | n/a | # offset is always relative to the class... |
---|
162 | n/a | |
---|
163 | n/a | def test_packed(self): |
---|
164 | n/a | class X(Structure): |
---|
165 | n/a | _fields_ = [("a", c_byte), |
---|
166 | n/a | ("b", c_longlong)] |
---|
167 | n/a | _pack_ = 1 |
---|
168 | n/a | |
---|
169 | n/a | self.assertEqual(sizeof(X), 9) |
---|
170 | n/a | self.assertEqual(X.b.offset, 1) |
---|
171 | n/a | |
---|
172 | n/a | class X(Structure): |
---|
173 | n/a | _fields_ = [("a", c_byte), |
---|
174 | n/a | ("b", c_longlong)] |
---|
175 | n/a | _pack_ = 2 |
---|
176 | n/a | self.assertEqual(sizeof(X), 10) |
---|
177 | n/a | self.assertEqual(X.b.offset, 2) |
---|
178 | n/a | |
---|
179 | n/a | import struct |
---|
180 | n/a | longlong_size = struct.calcsize("q") |
---|
181 | n/a | longlong_align = struct.calcsize("bq") - longlong_size |
---|
182 | n/a | |
---|
183 | n/a | class X(Structure): |
---|
184 | n/a | _fields_ = [("a", c_byte), |
---|
185 | n/a | ("b", c_longlong)] |
---|
186 | n/a | _pack_ = 4 |
---|
187 | n/a | self.assertEqual(sizeof(X), min(4, longlong_align) + longlong_size) |
---|
188 | n/a | self.assertEqual(X.b.offset, min(4, longlong_align)) |
---|
189 | n/a | |
---|
190 | n/a | class X(Structure): |
---|
191 | n/a | _fields_ = [("a", c_byte), |
---|
192 | n/a | ("b", c_longlong)] |
---|
193 | n/a | _pack_ = 8 |
---|
194 | n/a | |
---|
195 | n/a | self.assertEqual(sizeof(X), min(8, longlong_align) + longlong_size) |
---|
196 | n/a | self.assertEqual(X.b.offset, min(8, longlong_align)) |
---|
197 | n/a | |
---|
198 | n/a | |
---|
199 | n/a | d = {"_fields_": [("a", "b"), |
---|
200 | n/a | ("b", "q")], |
---|
201 | n/a | "_pack_": -1} |
---|
202 | n/a | self.assertRaises(ValueError, type(Structure), "X", (Structure,), d) |
---|
203 | n/a | |
---|
204 | n/a | # Issue 15989 |
---|
205 | n/a | d = {"_fields_": [("a", c_byte)], |
---|
206 | n/a | "_pack_": _testcapi.INT_MAX + 1} |
---|
207 | n/a | self.assertRaises(ValueError, type(Structure), "X", (Structure,), d) |
---|
208 | n/a | d = {"_fields_": [("a", c_byte)], |
---|
209 | n/a | "_pack_": _testcapi.UINT_MAX + 2} |
---|
210 | n/a | self.assertRaises(ValueError, type(Structure), "X", (Structure,), d) |
---|
211 | n/a | |
---|
212 | n/a | def test_initializers(self): |
---|
213 | n/a | class Person(Structure): |
---|
214 | n/a | _fields_ = [("name", c_char*6), |
---|
215 | n/a | ("age", c_int)] |
---|
216 | n/a | |
---|
217 | n/a | self.assertRaises(TypeError, Person, 42) |
---|
218 | n/a | self.assertRaises(ValueError, Person, b"asldkjaslkdjaslkdj") |
---|
219 | n/a | self.assertRaises(TypeError, Person, "Name", "HI") |
---|
220 | n/a | |
---|
221 | n/a | # short enough |
---|
222 | n/a | self.assertEqual(Person(b"12345", 5).name, b"12345") |
---|
223 | n/a | # exact fit |
---|
224 | n/a | self.assertEqual(Person(b"123456", 5).name, b"123456") |
---|
225 | n/a | # too long |
---|
226 | n/a | self.assertRaises(ValueError, Person, b"1234567", 5) |
---|
227 | n/a | |
---|
228 | n/a | def test_conflicting_initializers(self): |
---|
229 | n/a | class POINT(Structure): |
---|
230 | n/a | _fields_ = [("phi", c_float), ("rho", c_float)] |
---|
231 | n/a | # conflicting positional and keyword args |
---|
232 | n/a | self.assertRaisesRegex(TypeError, "phi", POINT, 2, 3, phi=4) |
---|
233 | n/a | self.assertRaisesRegex(TypeError, "rho", POINT, 2, 3, rho=4) |
---|
234 | n/a | |
---|
235 | n/a | # too many initializers |
---|
236 | n/a | self.assertRaises(TypeError, POINT, 2, 3, 4) |
---|
237 | n/a | |
---|
238 | n/a | def test_keyword_initializers(self): |
---|
239 | n/a | class POINT(Structure): |
---|
240 | n/a | _fields_ = [("x", c_int), ("y", c_int)] |
---|
241 | n/a | pt = POINT(1, 2) |
---|
242 | n/a | self.assertEqual((pt.x, pt.y), (1, 2)) |
---|
243 | n/a | |
---|
244 | n/a | pt = POINT(y=2, x=1) |
---|
245 | n/a | self.assertEqual((pt.x, pt.y), (1, 2)) |
---|
246 | n/a | |
---|
247 | n/a | def test_invalid_field_types(self): |
---|
248 | n/a | class POINT(Structure): |
---|
249 | n/a | pass |
---|
250 | n/a | self.assertRaises(TypeError, setattr, POINT, "_fields_", [("x", 1), ("y", 2)]) |
---|
251 | n/a | |
---|
252 | n/a | def test_invalid_name(self): |
---|
253 | n/a | # field name must be string |
---|
254 | n/a | def declare_with_name(name): |
---|
255 | n/a | class S(Structure): |
---|
256 | n/a | _fields_ = [(name, c_int)] |
---|
257 | n/a | |
---|
258 | n/a | self.assertRaises(TypeError, declare_with_name, b"x") |
---|
259 | n/a | |
---|
260 | n/a | def test_intarray_fields(self): |
---|
261 | n/a | class SomeInts(Structure): |
---|
262 | n/a | _fields_ = [("a", c_int * 4)] |
---|
263 | n/a | |
---|
264 | n/a | # can use tuple to initialize array (but not list!) |
---|
265 | n/a | self.assertEqual(SomeInts((1, 2)).a[:], [1, 2, 0, 0]) |
---|
266 | n/a | self.assertEqual(SomeInts((1, 2)).a[::], [1, 2, 0, 0]) |
---|
267 | n/a | self.assertEqual(SomeInts((1, 2)).a[::-1], [0, 0, 2, 1]) |
---|
268 | n/a | self.assertEqual(SomeInts((1, 2)).a[::2], [1, 0]) |
---|
269 | n/a | self.assertEqual(SomeInts((1, 2)).a[1:5:6], [2]) |
---|
270 | n/a | self.assertEqual(SomeInts((1, 2)).a[6:4:-1], []) |
---|
271 | n/a | self.assertEqual(SomeInts((1, 2, 3, 4)).a[:], [1, 2, 3, 4]) |
---|
272 | n/a | self.assertEqual(SomeInts((1, 2, 3, 4)).a[::], [1, 2, 3, 4]) |
---|
273 | n/a | # too long |
---|
274 | n/a | # XXX Should raise ValueError?, not RuntimeError |
---|
275 | n/a | self.assertRaises(RuntimeError, SomeInts, (1, 2, 3, 4, 5)) |
---|
276 | n/a | |
---|
277 | n/a | def test_nested_initializers(self): |
---|
278 | n/a | # test initializing nested structures |
---|
279 | n/a | class Phone(Structure): |
---|
280 | n/a | _fields_ = [("areacode", c_char*6), |
---|
281 | n/a | ("number", c_char*12)] |
---|
282 | n/a | |
---|
283 | n/a | class Person(Structure): |
---|
284 | n/a | _fields_ = [("name", c_char * 12), |
---|
285 | n/a | ("phone", Phone), |
---|
286 | n/a | ("age", c_int)] |
---|
287 | n/a | |
---|
288 | n/a | p = Person(b"Someone", (b"1234", b"5678"), 5) |
---|
289 | n/a | |
---|
290 | n/a | self.assertEqual(p.name, b"Someone") |
---|
291 | n/a | self.assertEqual(p.phone.areacode, b"1234") |
---|
292 | n/a | self.assertEqual(p.phone.number, b"5678") |
---|
293 | n/a | self.assertEqual(p.age, 5) |
---|
294 | n/a | |
---|
295 | n/a | @need_symbol('c_wchar') |
---|
296 | n/a | def test_structures_with_wchar(self): |
---|
297 | n/a | class PersonW(Structure): |
---|
298 | n/a | _fields_ = [("name", c_wchar * 12), |
---|
299 | n/a | ("age", c_int)] |
---|
300 | n/a | |
---|
301 | n/a | p = PersonW("Someone \xe9") |
---|
302 | n/a | self.assertEqual(p.name, "Someone \xe9") |
---|
303 | n/a | |
---|
304 | n/a | self.assertEqual(PersonW("1234567890").name, "1234567890") |
---|
305 | n/a | self.assertEqual(PersonW("12345678901").name, "12345678901") |
---|
306 | n/a | # exact fit |
---|
307 | n/a | self.assertEqual(PersonW("123456789012").name, "123456789012") |
---|
308 | n/a | #too long |
---|
309 | n/a | self.assertRaises(ValueError, PersonW, "1234567890123") |
---|
310 | n/a | |
---|
311 | n/a | def test_init_errors(self): |
---|
312 | n/a | class Phone(Structure): |
---|
313 | n/a | _fields_ = [("areacode", c_char*6), |
---|
314 | n/a | ("number", c_char*12)] |
---|
315 | n/a | |
---|
316 | n/a | class Person(Structure): |
---|
317 | n/a | _fields_ = [("name", c_char * 12), |
---|
318 | n/a | ("phone", Phone), |
---|
319 | n/a | ("age", c_int)] |
---|
320 | n/a | |
---|
321 | n/a | cls, msg = self.get_except(Person, b"Someone", (1, 2)) |
---|
322 | n/a | self.assertEqual(cls, RuntimeError) |
---|
323 | n/a | self.assertEqual(msg, |
---|
324 | n/a | "(Phone) <class 'TypeError'>: " |
---|
325 | n/a | "expected bytes, int found") |
---|
326 | n/a | |
---|
327 | n/a | cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c")) |
---|
328 | n/a | self.assertEqual(cls, RuntimeError) |
---|
329 | n/a | self.assertEqual(msg, |
---|
330 | n/a | "(Phone) <class 'TypeError'>: too many initializers") |
---|
331 | n/a | |
---|
332 | n/a | def test_huge_field_name(self): |
---|
333 | n/a | # issue12881: segfault with large structure field names |
---|
334 | n/a | def create_class(length): |
---|
335 | n/a | class S(Structure): |
---|
336 | n/a | _fields_ = [('x' * length, c_int)] |
---|
337 | n/a | |
---|
338 | n/a | for length in [10 ** i for i in range(0, 8)]: |
---|
339 | n/a | try: |
---|
340 | n/a | create_class(length) |
---|
341 | n/a | except MemoryError: |
---|
342 | n/a | # MemoryErrors are OK, we just don't want to segfault |
---|
343 | n/a | pass |
---|
344 | n/a | |
---|
345 | n/a | def get_except(self, func, *args): |
---|
346 | n/a | try: |
---|
347 | n/a | func(*args) |
---|
348 | n/a | except Exception as detail: |
---|
349 | n/a | return detail.__class__, str(detail) |
---|
350 | n/a | |
---|
351 | n/a | @unittest.skip('test disabled') |
---|
352 | n/a | def test_subclass_creation(self): |
---|
353 | n/a | meta = type(Structure) |
---|
354 | n/a | # same as 'class X(Structure): pass' |
---|
355 | n/a | # fails, since we need either a _fields_ or a _abstract_ attribute |
---|
356 | n/a | cls, msg = self.get_except(meta, "X", (Structure,), {}) |
---|
357 | n/a | self.assertEqual((cls, msg), |
---|
358 | n/a | (AttributeError, "class must define a '_fields_' attribute")) |
---|
359 | n/a | |
---|
360 | n/a | def test_abstract_class(self): |
---|
361 | n/a | class X(Structure): |
---|
362 | n/a | _abstract_ = "something" |
---|
363 | n/a | # try 'X()' |
---|
364 | n/a | cls, msg = self.get_except(eval, "X()", locals()) |
---|
365 | n/a | self.assertEqual((cls, msg), (TypeError, "abstract class")) |
---|
366 | n/a | |
---|
367 | n/a | def test_methods(self): |
---|
368 | n/a | ## class X(Structure): |
---|
369 | n/a | ## _fields_ = [] |
---|
370 | n/a | |
---|
371 | n/a | self.assertIn("in_dll", dir(type(Structure))) |
---|
372 | n/a | self.assertIn("from_address", dir(type(Structure))) |
---|
373 | n/a | self.assertIn("in_dll", dir(type(Structure))) |
---|
374 | n/a | |
---|
375 | n/a | def test_positional_args(self): |
---|
376 | n/a | # see also http://bugs.python.org/issue5042 |
---|
377 | n/a | class W(Structure): |
---|
378 | n/a | _fields_ = [("a", c_int), ("b", c_int)] |
---|
379 | n/a | class X(W): |
---|
380 | n/a | _fields_ = [("c", c_int)] |
---|
381 | n/a | class Y(X): |
---|
382 | n/a | pass |
---|
383 | n/a | class Z(Y): |
---|
384 | n/a | _fields_ = [("d", c_int), ("e", c_int), ("f", c_int)] |
---|
385 | n/a | |
---|
386 | n/a | z = Z(1, 2, 3, 4, 5, 6) |
---|
387 | n/a | self.assertEqual((z.a, z.b, z.c, z.d, z.e, z.f), |
---|
388 | n/a | (1, 2, 3, 4, 5, 6)) |
---|
389 | n/a | z = Z(1) |
---|
390 | n/a | self.assertEqual((z.a, z.b, z.c, z.d, z.e, z.f), |
---|
391 | n/a | (1, 0, 0, 0, 0, 0)) |
---|
392 | n/a | self.assertRaises(TypeError, lambda: Z(1, 2, 3, 4, 5, 6, 7)) |
---|
393 | n/a | |
---|
394 | n/a | class PointerMemberTestCase(unittest.TestCase): |
---|
395 | n/a | |
---|
396 | n/a | def test(self): |
---|
397 | n/a | # a Structure with a POINTER field |
---|
398 | n/a | class S(Structure): |
---|
399 | n/a | _fields_ = [("array", POINTER(c_int))] |
---|
400 | n/a | |
---|
401 | n/a | s = S() |
---|
402 | n/a | # We can assign arrays of the correct type |
---|
403 | n/a | s.array = (c_int * 3)(1, 2, 3) |
---|
404 | n/a | items = [s.array[i] for i in range(3)] |
---|
405 | n/a | self.assertEqual(items, [1, 2, 3]) |
---|
406 | n/a | |
---|
407 | n/a | # The following are bugs, but are included here because the unittests |
---|
408 | n/a | # also describe the current behaviour. |
---|
409 | n/a | # |
---|
410 | n/a | # This fails with SystemError: bad arg to internal function |
---|
411 | n/a | # or with IndexError (with a patch I have) |
---|
412 | n/a | |
---|
413 | n/a | s.array[0] = 42 |
---|
414 | n/a | |
---|
415 | n/a | items = [s.array[i] for i in range(3)] |
---|
416 | n/a | self.assertEqual(items, [42, 2, 3]) |
---|
417 | n/a | |
---|
418 | n/a | s.array[0] = 1 |
---|
419 | n/a | |
---|
420 | n/a | ## s.array[1] = 42 |
---|
421 | n/a | |
---|
422 | n/a | items = [s.array[i] for i in range(3)] |
---|
423 | n/a | self.assertEqual(items, [1, 2, 3]) |
---|
424 | n/a | |
---|
425 | n/a | def test_none_to_pointer_fields(self): |
---|
426 | n/a | class S(Structure): |
---|
427 | n/a | _fields_ = [("x", c_int), |
---|
428 | n/a | ("p", POINTER(c_int))] |
---|
429 | n/a | |
---|
430 | n/a | s = S() |
---|
431 | n/a | s.x = 12345678 |
---|
432 | n/a | s.p = None |
---|
433 | n/a | self.assertEqual(s.x, 12345678) |
---|
434 | n/a | |
---|
435 | n/a | class TestRecursiveStructure(unittest.TestCase): |
---|
436 | n/a | def test_contains_itself(self): |
---|
437 | n/a | class Recursive(Structure): |
---|
438 | n/a | pass |
---|
439 | n/a | |
---|
440 | n/a | try: |
---|
441 | n/a | Recursive._fields_ = [("next", Recursive)] |
---|
442 | n/a | except AttributeError as details: |
---|
443 | n/a | self.assertIn("Structure or union cannot contain itself", |
---|
444 | n/a | str(details)) |
---|
445 | n/a | else: |
---|
446 | n/a | self.fail("Structure or union cannot contain itself") |
---|
447 | n/a | |
---|
448 | n/a | |
---|
449 | n/a | def test_vice_versa(self): |
---|
450 | n/a | class First(Structure): |
---|
451 | n/a | pass |
---|
452 | n/a | class Second(Structure): |
---|
453 | n/a | pass |
---|
454 | n/a | |
---|
455 | n/a | First._fields_ = [("second", Second)] |
---|
456 | n/a | |
---|
457 | n/a | try: |
---|
458 | n/a | Second._fields_ = [("first", First)] |
---|
459 | n/a | except AttributeError as details: |
---|
460 | n/a | self.assertIn("_fields_ is final", str(details)) |
---|
461 | n/a | else: |
---|
462 | n/a | self.fail("AttributeError not raised") |
---|
463 | n/a | |
---|
464 | n/a | if __name__ == '__main__': |
---|
465 | n/a | unittest.main() |
---|