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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/aimport re, sys
4n/a
5n/aif sys.byteorder == "little":
6n/a THIS_ENDIAN = "<"
7n/a OTHER_ENDIAN = ">"
8n/aelse:
9n/a THIS_ENDIAN = ">"
10n/a OTHER_ENDIAN = "<"
11n/a
12n/adef normalize(format):
13n/a # Remove current endian specifier and white space from a format
14n/a # string
15n/a if format is None:
16n/a return ""
17n/a format = format.replace(OTHER_ENDIAN, THIS_ENDIAN)
18n/a return re.sub(r"\s", "", format)
19n/a
20n/aclass Test(unittest.TestCase):
21n/a
22n/a def test_native_types(self):
23n/a for tp, fmt, shape, itemtp in native_types:
24n/a ob = tp()
25n/a v = memoryview(ob)
26n/a try:
27n/a self.assertEqual(normalize(v.format), normalize(fmt))
28n/a if shape:
29n/a self.assertEqual(len(v), shape[0])
30n/a else:
31n/a self.assertEqual(len(v) * sizeof(itemtp), sizeof(ob))
32n/a self.assertEqual(v.itemsize, sizeof(itemtp))
33n/a self.assertEqual(v.shape, shape)
34n/a # XXX Issue #12851: PyCData_NewGetBuffer() must provide strides
35n/a # if requested. memoryview currently reconstructs missing
36n/a # stride information, so this assert will fail.
37n/a # self.assertEqual(v.strides, ())
38n/a
39n/a # they are always read/write
40n/a self.assertFalse(v.readonly)
41n/a
42n/a if v.shape:
43n/a n = 1
44n/a for dim in v.shape:
45n/a n = n * dim
46n/a self.assertEqual(n * v.itemsize, len(v.tobytes()))
47n/a except:
48n/a # so that we can see the failing type
49n/a print(tp)
50n/a raise
51n/a
52n/a def test_endian_types(self):
53n/a for tp, fmt, shape, itemtp in endian_types:
54n/a ob = tp()
55n/a v = memoryview(ob)
56n/a try:
57n/a self.assertEqual(v.format, fmt)
58n/a if shape:
59n/a self.assertEqual(len(v), shape[0])
60n/a else:
61n/a self.assertEqual(len(v) * sizeof(itemtp), sizeof(ob))
62n/a self.assertEqual(v.itemsize, sizeof(itemtp))
63n/a self.assertEqual(v.shape, shape)
64n/a # XXX Issue #12851
65n/a # self.assertEqual(v.strides, ())
66n/a
67n/a # they are always read/write
68n/a self.assertFalse(v.readonly)
69n/a
70n/a if v.shape:
71n/a n = 1
72n/a for dim in v.shape:
73n/a n = n * dim
74n/a self.assertEqual(n, len(v))
75n/a except:
76n/a # so that we can see the failing type
77n/a print(tp)
78n/a raise
79n/a
80n/a# define some structure classes
81n/a
82n/aclass Point(Structure):
83n/a _fields_ = [("x", c_long), ("y", c_long)]
84n/a
85n/aclass PackedPoint(Structure):
86n/a _pack_ = 2
87n/a _fields_ = [("x", c_long), ("y", c_long)]
88n/a
89n/aclass Point2(Structure):
90n/a pass
91n/aPoint2._fields_ = [("x", c_long), ("y", c_long)]
92n/a
93n/aclass EmptyStruct(Structure):
94n/a _fields_ = []
95n/a
96n/aclass aUnion(Union):
97n/a _fields_ = [("a", c_int)]
98n/a
99n/aclass StructWithArrays(Structure):
100n/a _fields_ = [("x", c_long * 3 * 2), ("y", Point * 4)]
101n/a
102n/aclass Incomplete(Structure):
103n/a pass
104n/a
105n/aclass Complete(Structure):
106n/a pass
107n/aPComplete = POINTER(Complete)
108n/aComplete._fields_ = [("a", c_long)]
109n/a
110n/a################################################################
111n/a#
112n/a# This table contains format strings as they look on little endian
113n/a# machines. The test replaces '<' with '>' on big endian machines.
114n/a#
115n/anative_types = [
116n/a # type format shape calc itemsize
117n/a
118n/a ## simple types
119n/a
120n/a (c_char, "<c", (), c_char),
121n/a (c_byte, "<b", (), c_byte),
122n/a (c_ubyte, "<B", (), c_ubyte),
123n/a (c_short, "<h", (), c_short),
124n/a (c_ushort, "<H", (), c_ushort),
125n/a
126n/a # c_int and c_uint may be aliases to c_long
127n/a #(c_int, "<i", (), c_int),
128n/a #(c_uint, "<I", (), c_uint),
129n/a
130n/a (c_long, "<l", (), c_long),
131n/a (c_ulong, "<L", (), c_ulong),
132n/a
133n/a # c_longlong and c_ulonglong are aliases on 64-bit platforms
134n/a #(c_longlong, "<q", None, c_longlong),
135n/a #(c_ulonglong, "<Q", None, c_ulonglong),
136n/a
137n/a (c_float, "<f", (), c_float),
138n/a (c_double, "<d", (), c_double),
139n/a # c_longdouble may be an alias to c_double
140n/a
141n/a (c_bool, "<?", (), c_bool),
142n/a (py_object, "<O", (), py_object),
143n/a
144n/a ## pointers
145n/a
146n/a (POINTER(c_byte), "&<b", (), POINTER(c_byte)),
147n/a (POINTER(POINTER(c_long)), "&&<l", (), POINTER(POINTER(c_long))),
148n/a
149n/a ## arrays and pointers
150n/a
151n/a (c_double * 4, "<d", (4,), c_double),
152n/a (c_float * 4 * 3 * 2, "<f", (2,3,4), c_float),
153n/a (POINTER(c_short) * 2, "&<h", (2,), POINTER(c_short)),
154n/a (POINTER(c_short) * 2 * 3, "&<h", (3,2,), POINTER(c_short)),
155n/a (POINTER(c_short * 2), "&(2)<h", (), POINTER(c_short)),
156n/a
157n/a ## structures and unions
158n/a
159n/a (Point, "T{<l:x:<l:y:}", (), Point),
160n/a # packed structures do not implement the pep
161n/a (PackedPoint, "B", (), PackedPoint),
162n/a (Point2, "T{<l:x:<l:y:}", (), Point2),
163n/a (EmptyStruct, "T{}", (), EmptyStruct),
164n/a # the pep does't support unions
165n/a (aUnion, "B", (), aUnion),
166n/a # structure with sub-arrays
167n/a (StructWithArrays, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}", (), StructWithArrays),
168n/a (StructWithArrays * 3, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}", (3,), StructWithArrays),
169n/a
170n/a ## pointer to incomplete structure
171n/a (Incomplete, "B", (), Incomplete),
172n/a (POINTER(Incomplete), "&B", (), POINTER(Incomplete)),
173n/a
174n/a # 'Complete' is a structure that starts incomplete, but is completed after the
175n/a # pointer type to it has been created.
176n/a (Complete, "T{<l:a:}", (), Complete),
177n/a # Unfortunately the pointer format string is not fixed...
178n/a (POINTER(Complete), "&B", (), POINTER(Complete)),
179n/a
180n/a ## other
181n/a
182n/a # function signatures are not implemented
183n/a (CFUNCTYPE(None), "X{}", (), CFUNCTYPE(None)),
184n/a
185n/a ]
186n/a
187n/aclass BEPoint(BigEndianStructure):
188n/a _fields_ = [("x", c_long), ("y", c_long)]
189n/a
190n/aclass LEPoint(LittleEndianStructure):
191n/a _fields_ = [("x", c_long), ("y", c_long)]
192n/a
193n/a################################################################
194n/a#
195n/a# This table contains format strings as they really look, on both big
196n/a# and little endian machines.
197n/a#
198n/aendian_types = [
199n/a (BEPoint, "T{>l:x:>l:y:}", (), BEPoint),
200n/a (LEPoint, "T{<l:x:<l:y:}", (), LEPoint),
201n/a (POINTER(BEPoint), "&T{>l:x:>l:y:}", (), POINTER(BEPoint)),
202n/a (POINTER(LEPoint), "&T{<l:x:<l:y:}", (), POINTER(LEPoint)),
203n/a ]
204n/a
205n/aif __name__ == "__main__":
206n/a unittest.main()