Debian Codegraph @

lib-python/3/ctypes/test/test_byteswap.py

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