Debian Codegraph @

Lib/test/test_ctypes/test_byteswap.py

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