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

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

#countcontent
1n/afrom ctypes import *
2n/aimport unittest
3n/a
4n/aclass SimpleTestCase(unittest.TestCase):
5n/a def test_cint(self):
6n/a x = c_int()
7n/a self.assertEqual(x._objects, None)
8n/a x.value = 42
9n/a self.assertEqual(x._objects, None)
10n/a x = c_int(99)
11n/a self.assertEqual(x._objects, None)
12n/a
13n/a def test_ccharp(self):
14n/a x = c_char_p()
15n/a self.assertEqual(x._objects, None)
16n/a x.value = b"abc"
17n/a self.assertEqual(x._objects, b"abc")
18n/a x = c_char_p(b"spam")
19n/a self.assertEqual(x._objects, b"spam")
20n/a
21n/aclass StructureTestCase(unittest.TestCase):
22n/a def test_cint_struct(self):
23n/a class X(Structure):
24n/a _fields_ = [("a", c_int),
25n/a ("b", c_int)]
26n/a
27n/a x = X()
28n/a self.assertEqual(x._objects, None)
29n/a x.a = 42
30n/a x.b = 99
31n/a self.assertEqual(x._objects, None)
32n/a
33n/a def test_ccharp_struct(self):
34n/a class X(Structure):
35n/a _fields_ = [("a", c_char_p),
36n/a ("b", c_char_p)]
37n/a x = X()
38n/a self.assertEqual(x._objects, None)
39n/a
40n/a x.a = b"spam"
41n/a x.b = b"foo"
42n/a self.assertEqual(x._objects, {"0": b"spam", "1": b"foo"})
43n/a
44n/a def test_struct_struct(self):
45n/a class POINT(Structure):
46n/a _fields_ = [("x", c_int), ("y", c_int)]
47n/a class RECT(Structure):
48n/a _fields_ = [("ul", POINT), ("lr", POINT)]
49n/a
50n/a r = RECT()
51n/a r.ul.x = 0
52n/a r.ul.y = 1
53n/a r.lr.x = 2
54n/a r.lr.y = 3
55n/a self.assertEqual(r._objects, None)
56n/a
57n/a r = RECT()
58n/a pt = POINT(1, 2)
59n/a r.ul = pt
60n/a self.assertEqual(r._objects, {'0': {}})
61n/a r.ul.x = 22
62n/a r.ul.y = 44
63n/a self.assertEqual(r._objects, {'0': {}})
64n/a r.lr = POINT()
65n/a self.assertEqual(r._objects, {'0': {}, '1': {}})
66n/a
67n/aclass ArrayTestCase(unittest.TestCase):
68n/a def test_cint_array(self):
69n/a INTARR = c_int * 3
70n/a
71n/a ia = INTARR()
72n/a self.assertEqual(ia._objects, None)
73n/a ia[0] = 1
74n/a ia[1] = 2
75n/a ia[2] = 3
76n/a self.assertEqual(ia._objects, None)
77n/a
78n/a class X(Structure):
79n/a _fields_ = [("x", c_int),
80n/a ("a", INTARR)]
81n/a
82n/a x = X()
83n/a x.x = 1000
84n/a x.a[0] = 42
85n/a x.a[1] = 96
86n/a self.assertEqual(x._objects, None)
87n/a x.a = ia
88n/a self.assertEqual(x._objects, {'1': {}})
89n/a
90n/aclass PointerTestCase(unittest.TestCase):
91n/a def test_p_cint(self):
92n/a i = c_int(42)
93n/a x = pointer(i)
94n/a self.assertEqual(x._objects, {'1': i})
95n/a
96n/aclass DeletePointerTestCase(unittest.TestCase):
97n/a @unittest.skip('test disabled')
98n/a def test_X(self):
99n/a class X(Structure):
100n/a _fields_ = [("p", POINTER(c_char_p))]
101n/a x = X()
102n/a i = c_char_p("abc def")
103n/a from sys import getrefcount as grc
104n/a print("2?", grc(i))
105n/a x.p = pointer(i)
106n/a print("3?", grc(i))
107n/a for i in range(320):
108n/a c_int(99)
109n/a x.p[0]
110n/a print(x.p[0])
111n/a## del x
112n/a## print "2?", grc(i)
113n/a## del i
114n/a import gc
115n/a gc.collect()
116n/a for i in range(320):
117n/a c_int(99)
118n/a x.p[0]
119n/a print(x.p[0])
120n/a print(x.p.contents)
121n/a## print x._objects
122n/a
123n/a x.p[0] = "spam spam"
124n/a## print x.p[0]
125n/a print("+" * 42)
126n/a print(x._objects)
127n/a
128n/aclass PointerToStructure(unittest.TestCase):
129n/a def test(self):
130n/a class POINT(Structure):
131n/a _fields_ = [("x", c_int), ("y", c_int)]
132n/a class RECT(Structure):
133n/a _fields_ = [("a", POINTER(POINT)),
134n/a ("b", POINTER(POINT))]
135n/a r = RECT()
136n/a p1 = POINT(1, 2)
137n/a
138n/a r.a = pointer(p1)
139n/a r.b = pointer(p1)
140n/a## from pprint import pprint as pp
141n/a## pp(p1._objects)
142n/a## pp(r._objects)
143n/a
144n/a r.a[0].x = 42
145n/a r.a[0].y = 99
146n/a
147n/a # to avoid leaking when tests are run several times
148n/a # clean up the types left in the cache.
149n/a from ctypes import _pointer_type_cache
150n/a del _pointer_type_cache[POINT]
151n/a
152n/aif __name__ == "__main__":
153n/a unittest.main()