1 | n/a | import unittest |
---|
2 | n/a | from ctypes import * |
---|
3 | n/a | |
---|
4 | n/a | class AnonTest(unittest.TestCase): |
---|
5 | n/a | |
---|
6 | n/a | def test_anon(self): |
---|
7 | n/a | class ANON(Union): |
---|
8 | n/a | _fields_ = [("a", c_int), |
---|
9 | n/a | ("b", c_int)] |
---|
10 | n/a | |
---|
11 | n/a | class Y(Structure): |
---|
12 | n/a | _fields_ = [("x", c_int), |
---|
13 | n/a | ("_", ANON), |
---|
14 | n/a | ("y", c_int)] |
---|
15 | n/a | _anonymous_ = ["_"] |
---|
16 | n/a | |
---|
17 | n/a | self.assertEqual(Y.a.offset, sizeof(c_int)) |
---|
18 | n/a | self.assertEqual(Y.b.offset, sizeof(c_int)) |
---|
19 | n/a | |
---|
20 | n/a | self.assertEqual(ANON.a.offset, 0) |
---|
21 | n/a | self.assertEqual(ANON.b.offset, 0) |
---|
22 | n/a | |
---|
23 | n/a | def test_anon_nonseq(self): |
---|
24 | n/a | # TypeError: _anonymous_ must be a sequence |
---|
25 | n/a | self.assertRaises(TypeError, |
---|
26 | n/a | lambda: type(Structure)("Name", |
---|
27 | n/a | (Structure,), |
---|
28 | n/a | {"_fields_": [], "_anonymous_": 42})) |
---|
29 | n/a | |
---|
30 | n/a | def test_anon_nonmember(self): |
---|
31 | n/a | # AttributeError: type object 'Name' has no attribute 'x' |
---|
32 | n/a | self.assertRaises(AttributeError, |
---|
33 | n/a | lambda: type(Structure)("Name", |
---|
34 | n/a | (Structure,), |
---|
35 | n/a | {"_fields_": [], |
---|
36 | n/a | "_anonymous_": ["x"]})) |
---|
37 | n/a | |
---|
38 | n/a | def test_nested(self): |
---|
39 | n/a | class ANON_S(Structure): |
---|
40 | n/a | _fields_ = [("a", c_int)] |
---|
41 | n/a | |
---|
42 | n/a | class ANON_U(Union): |
---|
43 | n/a | _fields_ = [("_", ANON_S), |
---|
44 | n/a | ("b", c_int)] |
---|
45 | n/a | _anonymous_ = ["_"] |
---|
46 | n/a | |
---|
47 | n/a | class Y(Structure): |
---|
48 | n/a | _fields_ = [("x", c_int), |
---|
49 | n/a | ("_", ANON_U), |
---|
50 | n/a | ("y", c_int)] |
---|
51 | n/a | _anonymous_ = ["_"] |
---|
52 | n/a | |
---|
53 | n/a | self.assertEqual(Y.x.offset, 0) |
---|
54 | n/a | self.assertEqual(Y.a.offset, sizeof(c_int)) |
---|
55 | n/a | self.assertEqual(Y.b.offset, sizeof(c_int)) |
---|
56 | n/a | self.assertEqual(Y._.offset, sizeof(c_int)) |
---|
57 | n/a | self.assertEqual(Y.y.offset, sizeof(c_int) * 2) |
---|
58 | n/a | |
---|
59 | n/a | if __name__ == "__main__": |
---|
60 | n/a | unittest.main() |
---|