1 | n/a | import unittest |
---|
2 | n/a | from ctypes import * |
---|
3 | n/a | |
---|
4 | n/a | class StructFieldsTestCase(unittest.TestCase): |
---|
5 | n/a | # Structure/Union classes must get 'finalized' sooner or |
---|
6 | n/a | # later, when one of these things happen: |
---|
7 | n/a | # |
---|
8 | n/a | # 1. _fields_ is set. |
---|
9 | n/a | # 2. An instance is created. |
---|
10 | n/a | # 3. The type is used as field of another Structure/Union. |
---|
11 | n/a | # 4. The type is subclassed |
---|
12 | n/a | # |
---|
13 | n/a | # When they are finalized, assigning _fields_ is no longer allowed. |
---|
14 | n/a | |
---|
15 | n/a | def test_1_A(self): |
---|
16 | n/a | class X(Structure): |
---|
17 | n/a | pass |
---|
18 | n/a | self.assertEqual(sizeof(X), 0) # not finalized |
---|
19 | n/a | X._fields_ = [] # finalized |
---|
20 | n/a | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
---|
21 | n/a | |
---|
22 | n/a | def test_1_B(self): |
---|
23 | n/a | class X(Structure): |
---|
24 | n/a | _fields_ = [] # finalized |
---|
25 | n/a | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
---|
26 | n/a | |
---|
27 | n/a | def test_2(self): |
---|
28 | n/a | class X(Structure): |
---|
29 | n/a | pass |
---|
30 | n/a | X() |
---|
31 | n/a | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
---|
32 | n/a | |
---|
33 | n/a | def test_3(self): |
---|
34 | n/a | class X(Structure): |
---|
35 | n/a | pass |
---|
36 | n/a | class Y(Structure): |
---|
37 | n/a | _fields_ = [("x", X)] # finalizes X |
---|
38 | n/a | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
---|
39 | n/a | |
---|
40 | n/a | def test_4(self): |
---|
41 | n/a | class X(Structure): |
---|
42 | n/a | pass |
---|
43 | n/a | class Y(X): |
---|
44 | n/a | pass |
---|
45 | n/a | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
---|
46 | n/a | Y._fields_ = [] |
---|
47 | n/a | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
---|
48 | n/a | |
---|
49 | n/a | if __name__ == "__main__": |
---|
50 | n/a | unittest.main() |
---|