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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/a
4n/a################################################################
5n/a#
6n/a# The incomplete pointer example from the tutorial
7n/a#
8n/a
9n/aclass MyTestCase(unittest.TestCase):
10n/a
11n/a def test_incomplete_example(self):
12n/a lpcell = POINTER("cell")
13n/a class cell(Structure):
14n/a _fields_ = [("name", c_char_p),
15n/a ("next", lpcell)]
16n/a
17n/a SetPointerType(lpcell, cell)
18n/a
19n/a c1 = cell()
20n/a c1.name = b"foo"
21n/a c2 = cell()
22n/a c2.name = b"bar"
23n/a
24n/a c1.next = pointer(c2)
25n/a c2.next = pointer(c1)
26n/a
27n/a p = c1
28n/a
29n/a result = []
30n/a for i in range(8):
31n/a result.append(p.name)
32n/a p = p.next[0]
33n/a self.assertEqual(result, [b"foo", b"bar"] * 4)
34n/a
35n/a # to not leak references, we must clean _pointer_type_cache
36n/a from ctypes import _pointer_type_cache
37n/a del _pointer_type_cache[cell]
38n/a
39n/a################################################################
40n/a
41n/aif __name__ == '__main__':
42n/a unittest.main()