ยปCore Development>Code coverage>Lib/ctypes/test/test_struct_fields.py

Python code coverage for Lib/ctypes/test/test_struct_fields.py

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