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

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

#countcontent
1n/aimport sys, unittest, struct, math, ctypes
2n/afrom binascii import hexlify
3n/a
4n/afrom ctypes import *
5n/a
6n/adef bin(s):
7n/a return hexlify(memoryview(s)).decode().upper()
8n/a
9n/a# Each *simple* type that supports different byte orders has an
10n/a# __ctype_be__ attribute that specifies the same type in BIG ENDIAN
11n/a# byte order, and a __ctype_le__ attribute that is the same type in
12n/a# LITTLE ENDIAN byte order.
13n/a#
14n/a# For Structures and Unions, these types are created on demand.
15n/a
16n/aclass Test(unittest.TestCase):
17n/a @unittest.skip('test disabled')
18n/a def test_X(self):
19n/a print(sys.byteorder, file=sys.stderr)
20n/a for i in range(32):
21n/a bits = BITS()
22n/a setattr(bits, "i%s" % i, 1)
23n/a dump(bits)
24n/a
25n/a def test_slots(self):
26n/a class BigPoint(BigEndianStructure):
27n/a __slots__ = ()
28n/a _fields_ = [("x", c_int), ("y", c_int)]
29n/a
30n/a class LowPoint(LittleEndianStructure):
31n/a __slots__ = ()
32n/a _fields_ = [("x", c_int), ("y", c_int)]
33n/a
34n/a big = BigPoint()
35n/a little = LowPoint()
36n/a big.x = 4
37n/a big.y = 2
38n/a little.x = 2
39n/a little.y = 4
40n/a with self.assertRaises(AttributeError):
41n/a big.z = 42
42n/a with self.assertRaises(AttributeError):
43n/a little.z = 24
44n/a
45n/a def test_endian_short(self):
46n/a if sys.byteorder == "little":
47n/a self.assertIs(c_short.__ctype_le__, c_short)
48n/a self.assertIs(c_short.__ctype_be__.__ctype_le__, c_short)
49n/a else:
50n/a self.assertIs(c_short.__ctype_be__, c_short)
51n/a self.assertIs(c_short.__ctype_le__.__ctype_be__, c_short)
52n/a s = c_short.__ctype_be__(0x1234)
53n/a self.assertEqual(bin(struct.pack(">h", 0x1234)), "1234")
54n/a self.assertEqual(bin(s), "1234")
55n/a self.assertEqual(s.value, 0x1234)
56n/a
57n/a s = c_short.__ctype_le__(0x1234)
58n/a self.assertEqual(bin(struct.pack("<h", 0x1234)), "3412")
59n/a self.assertEqual(bin(s), "3412")
60n/a self.assertEqual(s.value, 0x1234)
61n/a
62n/a s = c_ushort.__ctype_be__(0x1234)
63n/a self.assertEqual(bin(struct.pack(">h", 0x1234)), "1234")
64n/a self.assertEqual(bin(s), "1234")
65n/a self.assertEqual(s.value, 0x1234)
66n/a
67n/a s = c_ushort.__ctype_le__(0x1234)
68n/a self.assertEqual(bin(struct.pack("<h", 0x1234)), "3412")
69n/a self.assertEqual(bin(s), "3412")
70n/a self.assertEqual(s.value, 0x1234)
71n/a
72n/a def test_endian_int(self):
73n/a if sys.byteorder == "little":
74n/a self.assertIs(c_int.__ctype_le__, c_int)
75n/a self.assertIs(c_int.__ctype_be__.__ctype_le__, c_int)
76n/a else:
77n/a self.assertIs(c_int.__ctype_be__, c_int)
78n/a self.assertIs(c_int.__ctype_le__.__ctype_be__, c_int)
79n/a
80n/a s = c_int.__ctype_be__(0x12345678)
81n/a self.assertEqual(bin(struct.pack(">i", 0x12345678)), "12345678")
82n/a self.assertEqual(bin(s), "12345678")
83n/a self.assertEqual(s.value, 0x12345678)
84n/a
85n/a s = c_int.__ctype_le__(0x12345678)
86n/a self.assertEqual(bin(struct.pack("<i", 0x12345678)), "78563412")
87n/a self.assertEqual(bin(s), "78563412")
88n/a self.assertEqual(s.value, 0x12345678)
89n/a
90n/a s = c_uint.__ctype_be__(0x12345678)
91n/a self.assertEqual(bin(struct.pack(">I", 0x12345678)), "12345678")
92n/a self.assertEqual(bin(s), "12345678")
93n/a self.assertEqual(s.value, 0x12345678)
94n/a
95n/a s = c_uint.__ctype_le__(0x12345678)
96n/a self.assertEqual(bin(struct.pack("<I", 0x12345678)), "78563412")
97n/a self.assertEqual(bin(s), "78563412")
98n/a self.assertEqual(s.value, 0x12345678)
99n/a
100n/a def test_endian_longlong(self):
101n/a if sys.byteorder == "little":
102n/a self.assertIs(c_longlong.__ctype_le__, c_longlong)
103n/a self.assertIs(c_longlong.__ctype_be__.__ctype_le__, c_longlong)
104n/a else:
105n/a self.assertIs(c_longlong.__ctype_be__, c_longlong)
106n/a self.assertIs(c_longlong.__ctype_le__.__ctype_be__, c_longlong)
107n/a
108n/a s = c_longlong.__ctype_be__(0x1234567890ABCDEF)
109n/a self.assertEqual(bin(struct.pack(">q", 0x1234567890ABCDEF)), "1234567890ABCDEF")
110n/a self.assertEqual(bin(s), "1234567890ABCDEF")
111n/a self.assertEqual(s.value, 0x1234567890ABCDEF)
112n/a
113n/a s = c_longlong.__ctype_le__(0x1234567890ABCDEF)
114n/a self.assertEqual(bin(struct.pack("<q", 0x1234567890ABCDEF)), "EFCDAB9078563412")
115n/a self.assertEqual(bin(s), "EFCDAB9078563412")
116n/a self.assertEqual(s.value, 0x1234567890ABCDEF)
117n/a
118n/a s = c_ulonglong.__ctype_be__(0x1234567890ABCDEF)
119n/a self.assertEqual(bin(struct.pack(">Q", 0x1234567890ABCDEF)), "1234567890ABCDEF")
120n/a self.assertEqual(bin(s), "1234567890ABCDEF")
121n/a self.assertEqual(s.value, 0x1234567890ABCDEF)
122n/a
123n/a s = c_ulonglong.__ctype_le__(0x1234567890ABCDEF)
124n/a self.assertEqual(bin(struct.pack("<Q", 0x1234567890ABCDEF)), "EFCDAB9078563412")
125n/a self.assertEqual(bin(s), "EFCDAB9078563412")
126n/a self.assertEqual(s.value, 0x1234567890ABCDEF)
127n/a
128n/a def test_endian_float(self):
129n/a if sys.byteorder == "little":
130n/a self.assertIs(c_float.__ctype_le__, c_float)
131n/a self.assertIs(c_float.__ctype_be__.__ctype_le__, c_float)
132n/a else:
133n/a self.assertIs(c_float.__ctype_be__, c_float)
134n/a self.assertIs(c_float.__ctype_le__.__ctype_be__, c_float)
135n/a s = c_float(math.pi)
136n/a self.assertEqual(bin(struct.pack("f", math.pi)), bin(s))
137n/a # Hm, what's the precision of a float compared to a double?
138n/a self.assertAlmostEqual(s.value, math.pi, places=6)
139n/a s = c_float.__ctype_le__(math.pi)
140n/a self.assertAlmostEqual(s.value, math.pi, places=6)
141n/a self.assertEqual(bin(struct.pack("<f", math.pi)), bin(s))
142n/a s = c_float.__ctype_be__(math.pi)
143n/a self.assertAlmostEqual(s.value, math.pi, places=6)
144n/a self.assertEqual(bin(struct.pack(">f", math.pi)), bin(s))
145n/a
146n/a def test_endian_double(self):
147n/a if sys.byteorder == "little":
148n/a self.assertIs(c_double.__ctype_le__, c_double)
149n/a self.assertIs(c_double.__ctype_be__.__ctype_le__, c_double)
150n/a else:
151n/a self.assertIs(c_double.__ctype_be__, c_double)
152n/a self.assertIs(c_double.__ctype_le__.__ctype_be__, c_double)
153n/a s = c_double(math.pi)
154n/a self.assertEqual(s.value, math.pi)
155n/a self.assertEqual(bin(struct.pack("d", math.pi)), bin(s))
156n/a s = c_double.__ctype_le__(math.pi)
157n/a self.assertEqual(s.value, math.pi)
158n/a self.assertEqual(bin(struct.pack("<d", math.pi)), bin(s))
159n/a s = c_double.__ctype_be__(math.pi)
160n/a self.assertEqual(s.value, math.pi)
161n/a self.assertEqual(bin(struct.pack(">d", math.pi)), bin(s))
162n/a
163n/a def test_endian_other(self):
164n/a self.assertIs(c_byte.__ctype_le__, c_byte)
165n/a self.assertIs(c_byte.__ctype_be__, c_byte)
166n/a
167n/a self.assertIs(c_ubyte.__ctype_le__, c_ubyte)
168n/a self.assertIs(c_ubyte.__ctype_be__, c_ubyte)
169n/a
170n/a self.assertIs(c_char.__ctype_le__, c_char)
171n/a self.assertIs(c_char.__ctype_be__, c_char)
172n/a
173n/a def test_struct_fields_1(self):
174n/a if sys.byteorder == "little":
175n/a base = BigEndianStructure
176n/a else:
177n/a base = LittleEndianStructure
178n/a
179n/a class T(base):
180n/a pass
181n/a _fields_ = [("a", c_ubyte),
182n/a ("b", c_byte),
183n/a ("c", c_short),
184n/a ("d", c_ushort),
185n/a ("e", c_int),
186n/a ("f", c_uint),
187n/a ("g", c_long),
188n/a ("h", c_ulong),
189n/a ("i", c_longlong),
190n/a ("k", c_ulonglong),
191n/a ("l", c_float),
192n/a ("m", c_double),
193n/a ("n", c_char),
194n/a
195n/a ("b1", c_byte, 3),
196n/a ("b2", c_byte, 3),
197n/a ("b3", c_byte, 2),
198n/a ("a", c_int * 3 * 3 * 3)]
199n/a T._fields_ = _fields_
200n/a
201n/a # these fields do not support different byte order:
202n/a for typ in c_wchar, c_void_p, POINTER(c_int):
203n/a _fields_.append(("x", typ))
204n/a class T(base):
205n/a pass
206n/a self.assertRaises(TypeError, setattr, T, "_fields_", [("x", typ)])
207n/a
208n/a def test_struct_struct(self):
209n/a # nested structures with different byteorders
210n/a
211n/a # create nested structures with given byteorders and set memory to data
212n/a
213n/a for nested, data in (
214n/a (BigEndianStructure, b'\0\0\0\1\0\0\0\2'),
215n/a (LittleEndianStructure, b'\1\0\0\0\2\0\0\0'),
216n/a ):
217n/a for parent in (
218n/a BigEndianStructure,
219n/a LittleEndianStructure,
220n/a Structure,
221n/a ):
222n/a class NestedStructure(nested):
223n/a _fields_ = [("x", c_uint32),
224n/a ("y", c_uint32)]
225n/a
226n/a class TestStructure(parent):
227n/a _fields_ = [("point", NestedStructure)]
228n/a
229n/a self.assertEqual(len(data), sizeof(TestStructure))
230n/a ptr = POINTER(TestStructure)
231n/a s = cast(data, ptr)[0]
232n/a del ctypes._pointer_type_cache[TestStructure]
233n/a self.assertEqual(s.point.x, 1)
234n/a self.assertEqual(s.point.y, 2)
235n/a
236n/a def test_struct_fields_2(self):
237n/a # standard packing in struct uses no alignment.
238n/a # So, we have to align using pad bytes.
239n/a #
240n/a # Unaligned accesses will crash Python (on those platforms that
241n/a # don't allow it, like sparc solaris).
242n/a if sys.byteorder == "little":
243n/a base = BigEndianStructure
244n/a fmt = ">bxhid"
245n/a else:
246n/a base = LittleEndianStructure
247n/a fmt = "<bxhid"
248n/a
249n/a class S(base):
250n/a _fields_ = [("b", c_byte),
251n/a ("h", c_short),
252n/a ("i", c_int),
253n/a ("d", c_double)]
254n/a
255n/a s1 = S(0x12, 0x1234, 0x12345678, 3.14)
256n/a s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
257n/a self.assertEqual(bin(s1), bin(s2))
258n/a
259n/a def test_unaligned_nonnative_struct_fields(self):
260n/a if sys.byteorder == "little":
261n/a base = BigEndianStructure
262n/a fmt = ">b h xi xd"
263n/a else:
264n/a base = LittleEndianStructure
265n/a fmt = "<b h xi xd"
266n/a
267n/a class S(base):
268n/a _pack_ = 1
269n/a _fields_ = [("b", c_byte),
270n/a
271n/a ("h", c_short),
272n/a
273n/a ("_1", c_byte),
274n/a ("i", c_int),
275n/a
276n/a ("_2", c_byte),
277n/a ("d", c_double)]
278n/a
279n/a s1 = S()
280n/a s1.b = 0x12
281n/a s1.h = 0x1234
282n/a s1.i = 0x12345678
283n/a s1.d = 3.14
284n/a s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
285n/a self.assertEqual(bin(s1), bin(s2))
286n/a
287n/a def test_unaligned_native_struct_fields(self):
288n/a if sys.byteorder == "little":
289n/a fmt = "<b h xi xd"
290n/a else:
291n/a base = LittleEndianStructure
292n/a fmt = ">b h xi xd"
293n/a
294n/a class S(Structure):
295n/a _pack_ = 1
296n/a _fields_ = [("b", c_byte),
297n/a
298n/a ("h", c_short),
299n/a
300n/a ("_1", c_byte),
301n/a ("i", c_int),
302n/a
303n/a ("_2", c_byte),
304n/a ("d", c_double)]
305n/a
306n/a s1 = S()
307n/a s1.b = 0x12
308n/a s1.h = 0x1234
309n/a s1.i = 0x12345678
310n/a s1.d = 3.14
311n/a s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
312n/a self.assertEqual(bin(s1), bin(s2))
313n/a
314n/aif __name__ == "__main__":
315n/a unittest.main()