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

# Python code coverage for Lib/ctypes/test/test_init.py

#countcontent
1n/afrom ctypes import *
2n/aimport unittest
3n/a
4n/aclass X(Structure):
5n/a _fields_ = [("a", c_int),
6n/a ("b", c_int)]
7n/a new_was_called = False
8n/a
9n/a def __new__(cls):
10n/a result = super().__new__(cls)
11n/a result.new_was_called = True
12n/a return result
13n/a
14n/a def __init__(self):
15n/a self.a = 9
16n/a self.b = 12
17n/a
18n/aclass Y(Structure):
19n/a _fields_ = [("x", X)]
20n/a
21n/a
22n/aclass InitTest(unittest.TestCase):
23n/a def test_get(self):
24n/a # make sure the only accessing a nested structure
25n/a # doesn't call the structure's __new__ and __init__
26n/a y = Y()
27n/a self.assertEqual((y.x.a, y.x.b), (0, 0))
28n/a self.assertEqual(y.x.new_was_called, False)
29n/a
30n/a # But explicitly creating an X structure calls __new__ and __init__, of course.
31n/a x = X()
32n/a self.assertEqual((x.a, x.b), (9, 12))
33n/a self.assertEqual(x.new_was_called, True)
34n/a
35n/a y.x = x
36n/a self.assertEqual((y.x.a, y.x.b), (9, 12))
37n/a self.assertEqual(y.x.new_was_called, False)
38n/a
39n/aif __name__ == "__main__":
40n/a unittest.main()