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