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