Debian Codegraph @

Lib/test/test_struct.py

raw
1 from collections import abc
2 from itertools import combinations
3 import array
4 import gc
5 import math
6 import operator
7 import unittest
8 import struct
9 import sys
10 import weakref
11
12 from test import support
13 from test.support import import_helper
14 from test.support.script_helper import assert_python_ok
15 from test.support.testcase import ComplexesAreIdenticalMixin
16
17 ISBIGENDIAN = sys.byteorder == "big"
18
19 integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'n', 'N'
20 byteorders = '', '@', '=', '<', '>', '!'
21
22 INF = float('inf')
23 NAN = float('nan')
24
25 def iter_integer_formats(byteorders=byteorders):
26 for code in integer_codes:
27 for byteorder in byteorders:
28 if (byteorder not in ('', '@') and code in ('n', 'N')):
29 continue
30 yield code, byteorder
31
32 def string_reverse(s):
33 return s[::-1]
34
35 def bigendian_to_native(value):
36 if ISBIGENDIAN:
37 return value
38 else:
39 return string_reverse(value)
40
41 class StructTest(ComplexesAreIdenticalMixin, unittest.TestCase):
42 def test_isbigendian(self):
43 self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN)
44
45 def test_consistence(self):
46 self.assertRaises(struct.error, struct.calcsize, 'Z')
47
48 sz = struct.calcsize('i')
49 self.assertEqual(sz * 3, struct.calcsize('iii'))
50
51 fmt = 'cbxxxxxxhhhhiillffd?'
52 fmt3 = '3c3b18x12h6i6l6f3d3?'
53 sz = struct.calcsize(fmt)
54 sz3 = struct.calcsize(fmt3)
55 self.assertEqual(sz * 3, sz3)
56
57 self.assertRaises(struct.error, struct.pack, 'iii', 3)
58 self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3)
59 self.assertRaises((TypeError, struct.error), struct.pack, 'i', 'foo')
60 self.assertRaises((TypeError, struct.error), struct.pack, 'P', 'foo')
61 self.assertRaises(struct.error, struct.unpack, 'd', b'flap')
62 s = struct.pack('ii', 1, 2)
63 self.assertRaises(struct.error, struct.unpack, 'iii', s)
64 self.assertRaises(struct.error, struct.unpack, 'i', s)
65
66 def test_transitiveness(self):
67 c = b'a'
68 b = 1
69 h = 255
70 i = 65535
71 l = 65536
72 f = 3.1415
73 d = 3.1415
74 t = True
75
76 for prefix in ('', '@', '<', '>', '=', '!'):
77 for format in ('xcbhilfd?', 'xcBHILfd?'):
78 format = prefix + format
79 s = struct.pack(format, c, b, h, i, l, f, d, t)
80 cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s)
81 self.assertEqual(cp, c)
82 self.assertEqual(bp, b)
83 self.assertEqual(hp, h)
84 self.assertEqual(ip, i)
85 self.assertEqual(lp, l)
86 self.assertEqual(int(100 * fp), int(100 * f))
87 self.assertEqual(int(100 * dp), int(100 * d))
88 self.assertEqual(tp, t)
89
90 def test_new_features(self):
91 # Test some of the new features in detail
92 # (format, argument, big-endian result, little-endian result, asymmetric)
93 tests = [
94 ('c', b'a', b'a', b'a', 0),
95 ('xc', b'a', b'\0a', b'\0a', 0),
96 ('cx', b'a', b'a\0', b'a\0', 0),
97 ('s', b'a', b'a', b'a', 0),
98 ('0s', b'helloworld', b'', b'', 1),
99 ('1s', b'helloworld', b'h', b'h', 1),
100 ('9s', b'helloworld', b'helloworl', b'helloworl', 1),
101 ('10s', b'helloworld', b'helloworld', b'helloworld', 0),
102 ('11s', b'helloworld', b'helloworld\0', b'helloworld\0', 1),
103 ('20s', b'helloworld', b'helloworld'+10*b'\0', b'helloworld'+10*b'\0', 1),
104 ('0p', b'helloworld', b'', b'', 1),
105 ('1p', b'helloworld', b'\x00', b'\x00', 1),
106 ('2p', b'helloworld', b'\x01h', b'\x01h', 1),
107 ('10p', b'helloworld', b'\x09helloworl', b'\x09helloworl', 1),
108 ('11p', b'helloworld', b'\x0Ahelloworld', b'\x0Ahelloworld', 0),
109 ('12p', b'helloworld', b'\x0Ahelloworld\0', b'\x0Ahelloworld\0', 1),
110 ('20p', b'helloworld', b'\x0Ahelloworld'+9*b'\0', b'\x0Ahelloworld'+9*b'\0', 1),
111 ('b', 7, b'\7', b'\7', 0),
112 ('b', -7, b'\371', b'\371', 0),
113 ('B', 7, b'\7', b'\7', 0),
114 ('B', 249, b'\371', b'\371', 0),
115 ('h', 700, b'\002\274', b'\274\002', 0),
116 ('h', -700, b'\375D', b'D\375', 0),
117 ('H', 700, b'\002\274', b'\274\002', 0),
118 ('H', 0x10000-700, b'\375D', b'D\375', 0),
119 ('i', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
120 ('i', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
121 ('I', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
122 ('I', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
123 ('l', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
124 ('l', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
125 ('L', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
126 ('L', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
127 ('f', 2.0, b'@\000\000\000', b'\000\000\000@', 0),
128 ('d', 2.0, b'@\000\000\000\000\000\000\000',
129 b'\000\000\000\000\000\000\000@', 0),
130 ('f', -2.0, b'\300\000\000\000', b'\000\000\000\300', 0),
131 ('d', -2.0, b'\300\000\000\000\000\000\000\000',
132 b'\000\000\000\000\000\000\000\300', 0),
133 ('?', 0, b'\0', b'\0', 0),
134 ('?', 3, b'\1', b'\1', 1),
135 ('?', True, b'\1', b'\1', 0),
136 ('?', [], b'\0', b'\0', 1),
137 ('?', (1,), b'\1', b'\1', 1),
138 ]
139
140 for fmt, arg, big, lil, asy in tests:
141 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
142 ('='+fmt, ISBIGENDIAN and big or lil)]:
143 res = struct.pack(xfmt, arg)
144 self.assertEqual(res, exp)
145 self.assertEqual(struct.calcsize(xfmt), len(res))
146 rev = struct.unpack(xfmt, res)[0]
147 if rev != arg:
148 self.assertTrue(asy)
149
150 def test_calcsize(self):
151 expected_size = {
152 'b': 1, 'B': 1,
153 'h': 2, 'H': 2,
154 'i': 4, 'I': 4,
155 'l': 4, 'L': 4,
156 'q': 8, 'Q': 8,
157 }
158
159 # standard integer sizes
160 for code, byteorder in iter_integer_formats(('=', '<', '>', '!')):
161 format = byteorder+code
162 size = struct.calcsize(format)
163 self.assertEqual(size, expected_size[code])
164
165 # native integer sizes
166 native_pairs = 'bB', 'hH', 'iI', 'lL', 'nN', 'qQ'
167 for format_pair in native_pairs:
168 for byteorder in '', '@':
169 signed_size = struct.calcsize(byteorder + format_pair[0])
170 unsigned_size = struct.calcsize(byteorder + format_pair[1])
171 self.assertEqual(signed_size, unsigned_size)
172
173 # bounds for native integer sizes
174 self.assertEqual(struct.calcsize('b'), 1)
175 self.assertLessEqual(2, struct.calcsize('h'))
176 self.assertLessEqual(4, struct.calcsize('l'))
177 self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
178 self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
179 self.assertLessEqual(8, struct.calcsize('q'))
180 self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
181 self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('i'))
182 self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('P'))
183
184 def test_integers(self):
185 # Integer tests (bBhHiIlLqQnN).
186 import binascii
187
188 class IntTester(unittest.TestCase):
189 def __init__(self, format):
190 super(IntTester, self).__init__(methodName='test_one')
191 self.format = format
192 self.code = format[-1]
193 self.byteorder = format[:-1]
194 if not self.byteorder in byteorders:
195 raise ValueError("unrecognized packing byteorder: %s" %
196 self.byteorder)
197 self.bytesize = struct.calcsize(format)
198 self.bitsize = self.bytesize * 8
199 if self.code in tuple('bhilqn'):
200 self.signed = True
201 self.min_value = -(2**(self.bitsize-1))
202 self.max_value = 2**(self.bitsize-1) - 1
203 elif self.code in tuple('BHILQN'):
204 self.signed = False
205 self.min_value = 0
206 self.max_value = 2**self.bitsize - 1
207 else:
208 raise ValueError("unrecognized format code: %s" %
209 self.code)
210
211 def test_one(self, x, pack=struct.pack,
212 unpack=struct.unpack,
213 unhexlify=binascii.unhexlify):
214
215 format = self.format
216 if self.min_value <= x <= self.max_value:
217 expected = x
218 if self.signed and x < 0:
219 expected += 1 << self.bitsize
220 self.assertGreaterEqual(expected, 0)
221 expected = '%x' % expected
222 if len(expected) & 1:
223 expected = "0" + expected
224 expected = expected.encode('ascii')
225 expected = unhexlify(expected)
226 expected = (b"\x00" * (self.bytesize - len(expected)) +
227 expected)
228 if (self.byteorder == '<' or
229 self.byteorder in ('', '@', '=') and not ISBIGENDIAN):
230 expected = string_reverse(expected)
231 self.assertEqual(len(expected), self.bytesize)
232
233 # Pack work?
234 got = pack(format, x)
235 self.assertEqual(got, expected)
236
237 # Unpack work?
238 retrieved = unpack(format, got)[0]
239 self.assertEqual(x, retrieved)
240
241 # Adding any byte should cause a "too big" error.
242 self.assertRaises((struct.error, TypeError), unpack, format,
243 b'\x01' + got)
244 else:
245 # x is out of range -- verify pack realizes that.
246 self.assertRaises((OverflowError, ValueError, struct.error),
247 pack, format, x)
248
249 def run(self):
250 from random import randrange
251
252 # Create all interesting powers of 2.
253 values = []
254 for exp in range(self.bitsize + 3):
255 values.append(1 << exp)
256
257 # Add some random values.
258 for i in range(self.bitsize):
259 val = 0
260 for j in range(self.bytesize):
261 val = (val << 8) | randrange(256)
262 values.append(val)
263
264 # Values absorbed from other tests
265 values.extend([300, 700000, sys.maxsize*4])
266
267 # Try all those, and their negations, and +-1 from
268 # them. Note that this tests all power-of-2
269 # boundaries in range, and a few out of range, plus
270 # +-(2**n +- 1).
271 for base in values:
272 for val in -base, base:
273 for incr in -1, 0, 1:
274 x = val + incr
275 self.test_one(x)
276
277 # Some error cases.
278 class NotAnInt:
279 def __int__(self):
280 return 42
281
282 # Objects with an '__index__' method should be allowed
283 # to pack as integers. That is assuming the implemented
284 # '__index__' method returns an 'int'.
285 class Indexable(object):
286 def __init__(self, value):
287 self._value = value
288
289 def __index__(self):
290 return self._value
291
292 # If the '__index__' method raises a type error, then
293 # '__int__' should be used with a deprecation warning.
294 class BadIndex(object):
295 def __index__(self):
296 raise TypeError
297
298 def __int__(self):
299 return 42
300
301 self.assertRaises((TypeError, struct.error),
302 struct.pack, self.format,
303 "a string")
304 self.assertRaises((TypeError, struct.error),
305 struct.pack, self.format,
306 randrange)
307 self.assertRaises((TypeError, struct.error),
308 struct.pack, self.format,
309 3+42j)
310 self.assertRaises((TypeError, struct.error),
311 struct.pack, self.format,
312 NotAnInt())
313 self.assertRaises((TypeError, struct.error),
314 struct.pack, self.format,
315 BadIndex())
316
317 # Check for legitimate values from '__index__'.
318 for obj in (Indexable(0), Indexable(10), Indexable(17),
319 Indexable(42), Indexable(100), Indexable(127)):
320 try:
321 struct.pack(format, obj)
322 except:
323 self.fail("integer code pack failed on object "
324 "with '__index__' method")
325
326 # Check for bogus values from '__index__'.
327 for obj in (Indexable(b'a'), Indexable('b'), Indexable(None),
328 Indexable({'a': 1}), Indexable([1, 2, 3])):
329 self.assertRaises((TypeError, struct.error),
330 struct.pack, self.format,
331 obj)
332
333 for code, byteorder in iter_integer_formats():
334 format = byteorder+code
335 t = IntTester(format)
336 t.run()
337
338 def test_nN_code(self):
339 # n and N don't exist in standard sizes
340 def assertStructError(func, *args, **kwargs):
341 with self.assertRaises(struct.error) as cm:
342 func(*args, **kwargs)
343 self.assertIn("bad char in struct format", str(cm.exception))
344 for code in 'nN':
345 for byteorder in ('=', '<', '>', '!'):
346 format = byteorder+code
347 assertStructError(struct.calcsize, format)
348 assertStructError(struct.pack, format, 0)
349 assertStructError(struct.unpack, format, b"")
350
351 def test_p_code(self):
352 # Test p ("Pascal string") code.
353 for code, input, expected, expectedback in [
354 ('0p', b'abc', b'', b''),
355 ('p', b'abc', b'\x00', b''),
356 ('1p', b'abc', b'\x00', b''),
357 ('2p', b'abc', b'\x01a', b'a'),
358 ('3p', b'abc', b'\x02ab', b'ab'),
359 ('4p', b'abc', b'\x03abc', b'abc'),
360 ('5p', b'abc', b'\x03abc\x00', b'abc'),
361 ('6p', b'abc', b'\x03abc\x00\x00', b'abc'),
362 ('1000p', b'x'*1000, b'\xff' + b'x'*999, b'x'*255)]:
363 got = struct.pack(code, input)
364 self.assertEqual(got, expected)
365 (got,) = struct.unpack(code, got)
366 self.assertEqual(got, expectedback)
367
368 def test_705836(self):
369 # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry
370 # from the low-order discarded bits could propagate into the exponent
371 # field, causing the result to be wrong by a factor of 2.
372 for base in range(1, 33):
373 # smaller <- largest representable float less than base.
374 delta = 0.5
375 while base - delta / 2.0 != base:
376 delta /= 2.0
377 smaller = base - delta
378 # Packing this rounds away a solid string of trailing 1 bits.
379 packed = struct.pack("<f", smaller)
380 unpacked = struct.unpack("<f", packed)[0]
381 # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and
382 # 16, respectively.
383 self.assertEqual(base, unpacked)
384 bigpacked = struct.pack(">f", smaller)
385 self.assertEqual(bigpacked, string_reverse(packed))
386 unpacked = struct.unpack(">f", bigpacked)[0]
387 self.assertEqual(base, unpacked)
388
389 # Largest finite IEEE single.
390 big = (1 << 24) - 1
391 big = math.ldexp(big, 127 - 23)
392 packed = struct.pack(">f", big)
393 unpacked = struct.unpack(">f", packed)[0]
394 self.assertEqual(big, unpacked)
395
396 # The same, but tack on a 1 bit so it rounds up to infinity.
397 big = (1 << 25) - 1
398 big = math.ldexp(big, 127 - 24)
399 self.assertRaises(OverflowError, struct.pack, ">f", big)
400 self.assertRaises(OverflowError, struct.pack, "<f", big)
401 # same for native format, see gh-145633
402 self.assertRaises(OverflowError, struct.pack, "f", big)
403
404 # And for half-floats
405 big = (1 << 11) - 1
406 big = math.ldexp(big, 15 - 10)
407 packed = struct.pack(">e", big)
408 unpacked = struct.unpack(">e", packed)[0]
409 self.assertEqual(big, unpacked)
410 big = (1 << 12) - 1
411 big = math.ldexp(big, 15 - 11)
412 self.assertRaises(OverflowError, struct.pack, ">e", big)
413 self.assertRaises(OverflowError, struct.pack, "<e", big)
414 self.assertRaises(OverflowError, struct.pack, "e", big)
415
416 def test_1530559(self):
417 for code, byteorder in iter_integer_formats():
418 format = byteorder + code
419 self.assertRaises(struct.error, struct.pack, format, 1.0)
420 self.assertRaises(struct.error, struct.pack, format, 1.5)
421 self.assertRaises(struct.error, struct.pack, 'P', 1.0)
422 self.assertRaises(struct.error, struct.pack, 'P', 1.5)
423
424 def test_unpack_from(self):
425 test_string = b'abcd01234'
426 fmt = '4s'
427 s = struct.Struct(fmt)
428 for cls in (bytes, bytearray):
429 data = cls(test_string)
430 self.assertEqual(s.unpack_from(data), (b'abcd',))
431 self.assertEqual(s.unpack_from(data, 2), (b'cd01',))
432 self.assertEqual(s.unpack_from(data, 4), (b'0123',))
433 for i in range(6):
434 self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
435 for i in range(6, len(test_string) + 1):
436 self.assertRaises(struct.error, s.unpack_from, data, i)
437 for cls in (bytes, bytearray):
438 data = cls(test_string)
439 self.assertEqual(struct.unpack_from(fmt, data), (b'abcd',))
440 self.assertEqual(struct.unpack_from(fmt, data, 2), (b'cd01',))
441 self.assertEqual(struct.unpack_from(fmt, data, 4), (b'0123',))
442 for i in range(6):
443 self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
444 for i in range(6, len(test_string) + 1):
445 self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)
446
447 # keyword arguments
448 self.assertEqual(s.unpack_from(buffer=test_string, offset=2),
449 (b'cd01',))
450
451 def _test_pack_into(self, pack_into):
452 test_string = b'Reykjavik rocks, eow!'
453 writable_buf = memoryview(array.array('b', b' '*100))
454
455 # Test without offset
456 pack_into(writable_buf, 0, test_string)
457 from_buf = writable_buf.tobytes()[:len(test_string)]
458 self.assertEqual(from_buf, test_string)
459
460 # Test with offset.
461 pack_into(writable_buf, 10, test_string)
462 from_buf = writable_buf.tobytes()[:len(test_string)+10]
463 self.assertEqual(from_buf, test_string[:10] + test_string)
464
465 # Test with negative offset.
466 pack_into(writable_buf, -30, test_string)
467 from_buf = writable_buf.tobytes()[-30:-30+len(test_string)]
468 self.assertEqual(from_buf, test_string)
469
470 # Go beyond boundaries.
471 small_buf = array.array('b', b' '*10)
472 with self.assertRaises((ValueError, struct.error)):
473 pack_into(small_buf, 0, test_string)
474 with self.assertRaises((ValueError, struct.error)):
475 pack_into(writable_buf, 90, test_string)
476 with self.assertRaises((ValueError, struct.error)):
477 pack_into(writable_buf, -10, test_string)
478 with self.assertRaises((ValueError, struct.error)):
479 pack_into(writable_buf, 150, test_string)
480 with self.assertRaises((ValueError, struct.error)):
481 pack_into(writable_buf, -150, test_string)
482
483 # Test invalid buffer.
484 self.assertRaises(TypeError, pack_into, b' '*100, 0, test_string)
485 self.assertRaises(TypeError, pack_into, ' '*100, 0, test_string)
486 self.assertRaises(TypeError, pack_into, [0]*100, 0, test_string)
487 self.assertRaises(TypeError, pack_into, None, 0, test_string)
488 self.assertRaises(TypeError, pack_into, writable_buf[::2], 0, test_string)
489 self.assertRaises(TypeError, pack_into, writable_buf[::-1], 0, test_string)
490
491 # Test bogus offset (issue bpo-3694)
492 with self.assertRaises(TypeError):
493 pack_into(writable_buf, None, test_string)
494 with self.assertRaises(TypeError):
495 pack_into(writable_buf, 0.0, test_string)
496 with self.assertRaises((IndexError, OverflowError)):
497 pack_into(writable_buf, 2**1000, test_string)
498 with self.assertRaises((IndexError, OverflowError)):
499 pack_into(writable_buf, -2**1000, test_string)
500
501 def test_pack_into(self):
502 s = struct.Struct('21s')
503 self._test_pack_into(s.pack_into)
504
505 def test_pack_into_fn(self):
506 pack_into = lambda *args: struct.pack_into('21s', *args)
507 self._test_pack_into(pack_into)
508
509 def test_unpack_with_buffer(self):
510 # SF bug 1563759: struct.unpack doesn't support buffer protocol objects
511 data1 = array.array('B', b'\x12\x34\x56\x78')
512 data2 = memoryview(b'\x12\x34\x56\x78') # XXX b'......XXXX......', 6, 4
513 for data in [data1, data2]:
514 value, = struct.unpack('>I', data)
515 self.assertEqual(value, 0x12345678)
516
517 def test_bool(self):
518 class ExplodingBool(object):
519 def __bool__(self):
520 raise OSError
521 for prefix in tuple("<>!=")+('',):
522 false = (), [], [], '', 0
523 true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2
524
525 falseFormat = prefix + '?' * len(false)
526 packedFalse = struct.pack(falseFormat, *false)
527 unpackedFalse = struct.unpack(falseFormat, packedFalse)
528
529 trueFormat = prefix + '?' * len(true)
530 packedTrue = struct.pack(trueFormat, *true)
531 unpackedTrue = struct.unpack(trueFormat, packedTrue)
532
533 self.assertEqual(len(true), len(unpackedTrue))
534 self.assertEqual(len(false), len(unpackedFalse))
535
536 for t in unpackedFalse:
537 self.assertFalse(t)
538 for t in unpackedTrue:
539 self.assertTrue(t)
540
541 packed = struct.pack(prefix+'?', 1)
542
543 self.assertEqual(len(packed), struct.calcsize(prefix+'?'))
544
545 if len(packed) != 1:
546 self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
547 %packed)
548
549 try:
550 struct.pack(prefix + '?', ExplodingBool())
551 except OSError:
552 pass
553 else:
554 self.fail("Expected OSError: struct.pack(%r, "
555 "ExplodingBool())" % (prefix + '?'))
556
557 for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
558 self.assertTrue(struct.unpack('>?', c)[0])
559 self.assertTrue(struct.unpack('<?', c)[0])
560 self.assertTrue(struct.unpack('=?', c)[0])
561 self.assertTrue(struct.unpack('@?', c)[0])
562
563 def test_count_overflow(self):
564 hugecount = '{}b'.format(sys.maxsize+1)
565 self.assertRaises(struct.error, struct.calcsize, hugecount)
566
567 hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
568 self.assertRaises(struct.error, struct.calcsize, hugecount2)
569
570 hugecount3 = '{}i{}q'.format(sys.maxsize // 4, sys.maxsize // 8)
571 self.assertRaises(struct.error, struct.calcsize, hugecount3)
572
573 hugecount4 = '{}?s'.format(sys.maxsize)
574 self.assertRaises(struct.error, struct.calcsize, hugecount4)
575
576 hugecount5 = '{}?p'.format(sys.maxsize)
577 self.assertRaises(struct.error, struct.calcsize, hugecount5)
578
579 def test_trailing_counter(self):
580 store = array.array('b', b' '*100)
581
582 # format lists containing only count spec should result in an error
583 self.assertRaises(struct.error, struct.pack, '12345')
584 self.assertRaises(struct.error, struct.unpack, '12345', b'')
585 self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
586 self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)
587
588 # Format lists with trailing count spec should result in an error
589 self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
590 self.assertRaises(struct.error, struct.unpack, 'c12345', b'x')
591 self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
592 'x')
593 self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
594 0)
595
596 # Mixed format tests
597 self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
598 self.assertRaises(struct.error, struct.unpack, '14s42',
599 b'spam and eggs')
600 self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
601 'spam and eggs')
602 self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
603
604 def test_Struct_reinitialization(self):
605 # Issue 9422: there was a memory leak when reinitializing a
606 # Struct instance. This test can be used to detect the leak
607 # when running with regrtest -L.
608 s = struct.Struct('>h')
609 s.__init__('>hh')
610 self.assertEqual(s.format, '>hh')
611 packed = b'\x00\x01\x00\x02'
612 self.assertEqual(s.pack(1, 2), packed)
613 self.assertEqual(s.unpack(packed), (1, 2))
614
615 with self.assertRaises(UnicodeEncodeError):
616 s.__init__('\udc00')
617 self.assertEqual(s.format, '>hh')
618 self.assertEqual(s.pack(1, 2), packed)
619 self.assertEqual(s.unpack(packed), (1, 2))
620
621 with self.assertRaises(struct.error):
622 s.__init__('$')
623 self.assertEqual(s.format, '>hh')
624 self.assertEqual(s.pack(1, 2), packed)
625 self.assertEqual(s.unpack(packed), (1, 2))
626
627 def check_sizeof(self, format_str, number_of_codes):
628 # The size of 'PyStructObject'
629 totalsize = support.calcobjsize('2n3P')
630 # The size taken up by the 'formatcode' dynamic array
631 totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
632 support.check_sizeof(self, struct.Struct(format_str), totalsize)
633
634 @support.cpython_only
635 def test__sizeof__(self):
636 for code in integer_codes:
637 self.check_sizeof(code, 1)
638 self.check_sizeof('BHILfdspP', 9)
639 self.check_sizeof('B' * 1234, 1234)
640 self.check_sizeof('fd', 2)
641 self.check_sizeof('xxxxxxxxxxxxxx', 0)
642 self.check_sizeof('100H', 1)
643 self.check_sizeof('187s', 1)
644 self.check_sizeof('20p', 1)
645 self.check_sizeof('0s', 1)
646 self.check_sizeof('0p', 1)
647 self.check_sizeof('0c', 0)
648
649 def test_boundary_error_message(self):
650 regex1 = (
651 r'pack_into requires a buffer of at least 6 '
652 r'bytes for packing 1 bytes at offset 5 '
653 r'\(actual buffer size is 1\)'
654 )
655 with self.assertRaisesRegex(struct.error, regex1):
656 struct.pack_into('b', bytearray(1), 5, 1)
657
658 regex2 = (
659 r'unpack_from requires a buffer of at least 6 '
660 r'bytes for unpacking 1 bytes at offset 5 '
661 r'\(actual buffer size is 1\)'
662 )
663 with self.assertRaisesRegex(struct.error, regex2):
664 struct.unpack_from('b', bytearray(1), 5)
665
666 def test_boundary_error_message_with_negative_offset(self):
667 byte_list = bytearray(10)
668 with self.assertRaisesRegex(
669 struct.error,
670 r'no space to pack 4 bytes at offset -2'):
671 struct.pack_into('<I', byte_list, -2, 123)
672
673 with self.assertRaisesRegex(
674 struct.error,
675 'offset -11 out of range for 10-byte buffer'):
676 struct.pack_into('<B', byte_list, -11, 123)
677
678 with self.assertRaisesRegex(
679 struct.error,
680 r'not enough data to unpack 4 bytes at offset -2'):
681 struct.unpack_from('<I', byte_list, -2)
682
683 with self.assertRaisesRegex(
684 struct.error,
685 "offset -11 out of range for 10-byte buffer"):
686 struct.unpack_from('<B', byte_list, -11)
687
688 def test_boundary_error_message_with_large_offset(self):
689 # Test overflows cause by large offset and value size (issue 30245)
690 regex1 = (
691 r'pack_into requires a buffer of at least ' + str(sys.maxsize + 4) +
692 r' bytes for packing 4 bytes at offset ' + str(sys.maxsize) +
693 r' \(actual buffer size is 10\)'
694 )
695 with self.assertRaisesRegex(struct.error, regex1):
696 struct.pack_into('<I', bytearray(10), sys.maxsize, 1)
697
698 regex2 = (
699 r'unpack_from requires a buffer of at least ' + str(sys.maxsize + 4) +
700 r' bytes for unpacking 4 bytes at offset ' + str(sys.maxsize) +
701 r' \(actual buffer size is 10\)'
702 )
703 with self.assertRaisesRegex(struct.error, regex2):
704 struct.unpack_from('<I', bytearray(10), sys.maxsize)
705
706 def test_issue29802(self):
707 # When the second argument of struct.unpack() was of wrong type
708 # the Struct object was decrefed twice and the reference to
709 # deallocated object was left in a cache.
710 with self.assertRaises(TypeError):
711 struct.unpack('b', 0)
712 # Shouldn't crash.
713 self.assertEqual(struct.unpack('b', b'a'), (b'a'[0],))
714
715 def test_format_attr(self):
716 s = struct.Struct('=i2H')
717 self.assertEqual(s.format, '=i2H')
718
719 # use a bytes string
720 s2 = struct.Struct(s.format.encode())
721 self.assertEqual(s2.format, s.format)
722
723 def test_struct_cleans_up_at_runtime_shutdown(self):
724 code = """if 1:
725 import struct
726
727 class C:
728 def __init__(self):
729 self.pack = struct.pack
730 def __del__(self):
731 self.pack('I', -42)
732
733 struct.x = C()
734 """
735 rc, stdout, stderr = assert_python_ok("-c", code)
736 self.assertEqual(rc, 0)
737 self.assertEqual(stdout.rstrip(), b"")
738 self.assertIn(b"Exception ignored while calling deallocator", stderr)
739 self.assertIn(b"C.__del__", stderr)
740
741 def test__struct_reference_cycle_cleaned_up(self):
742 # Regression test for python/cpython#94207.
743
744 # When we create a new struct module, trigger use of its cache,
745 # and then delete it ...
746 _struct_module = import_helper.import_fresh_module("_struct")
747 module_ref = weakref.ref(_struct_module)
748 _struct_module.calcsize("b")
749 del _struct_module
750
751 # Then the module should have been garbage collected.
752 gc.collect()
753 self.assertIsNone(
754 module_ref(), "_struct module was not garbage collected")
755
756 @support.cpython_only
757 def test__struct_types_immutable(self):
758 # See https://github.com/python/cpython/issues/94254
759
760 Struct = struct.Struct
761 unpack_iterator = type(struct.iter_unpack("b", b'x'))
762 for cls in (Struct, unpack_iterator):
763 with self.subTest(cls=cls):
764 with self.assertRaises(TypeError):
765 cls.x = 1
766
767
768 def test_issue35714(self):
769 # Embedded null characters should not be allowed in format strings.
770 for s in '\0', '2\0i', b'\0':
771 with self.assertRaisesRegex(struct.error,
772 'embedded null character'):
773 struct.calcsize(s)
774
775 @support.cpython_only
776 def test_issue98248(self):
777 def test_error_msg(prefix, int_type, is_unsigned):
778 fmt_str = prefix + int_type
779 size = struct.calcsize(fmt_str)
780 if is_unsigned:
781 max_ = 2 ** (size * 8) - 1
782 min_ = 0
783 else:
784 max_ = 2 ** (size * 8 - 1) - 1
785 min_ = -2 ** (size * 8 - 1)
786 error_msg = f"'{int_type}' format requires {min_} <= number <= {max_}"
787 for number in [int(-1e50), min_ - 1, max_ + 1, int(1e50)]:
788 with self.subTest(format_str=fmt_str, number=number):
789 with self.assertRaisesRegex(struct.error, error_msg):
790 struct.pack(fmt_str, number)
791 error_msg = "required argument is not an integer"
792 not_number = ""
793 with self.subTest(format_str=fmt_str, number=not_number):
794 with self.assertRaisesRegex(struct.error, error_msg):
795 struct.pack(fmt_str, not_number)
796
797 for prefix in '@=<>':
798 for int_type in 'BHILQ':
799 test_error_msg(prefix, int_type, True)
800 for int_type in 'bhilq':
801 test_error_msg(prefix, int_type, False)
802
803 int_type = 'N'
804 test_error_msg('@', int_type, True)
805
806 int_type = 'n'
807 test_error_msg('@', int_type, False)
808
809 @support.cpython_only
810 def test_issue98248_error_propagation(self):
811 class Div0:
812 def __index__(self):
813 1 / 0
814
815 def test_error_propagation(fmt_str):
816 with self.subTest(format_str=fmt_str, exception="ZeroDivisionError"):
817 with self.assertRaises(ZeroDivisionError):
818 struct.pack(fmt_str, Div0())
819
820 for prefix in '@=<>':
821 for int_type in 'BHILQbhilq':
822 test_error_propagation(prefix + int_type)
823
824 test_error_propagation('N')
825 test_error_propagation('n')
826
827 def test_struct_subclass_instantiation(self):
828 # Regression test for https://github.com/python/cpython/issues/112358
829 class MyStruct(struct.Struct):
830 def __init__(self):
831 super().__init__('>h')
832
833 my_struct = MyStruct()
834 self.assertEqual(my_struct.pack(12345), b'\x30\x39')
835
836 def test_repr(self):
837 s = struct.Struct('=i2H')
838 self.assertEqual(repr(s), f'Struct({s.format!r})')
839
840 def test_c_complex_round_trip(self):
841 values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2,
842 -3, INF, -INF, NAN], 2)]
843 for z in values:
844 for f in ['F', 'D', '>F', '>D', '<F', '<D']:
845 with self.subTest(z=z, format=f):
846 round_trip = struct.unpack(f, struct.pack(f, z))[0]
847 self.assertComplexesAreIdentical(z, round_trip)
848
849 @unittest.skipIf(
850 support.is_android or support.is_apple_mobile,
851 "Subinterpreters are not supported on Android and iOS"
852 )
853 def test_endian_table_init_subinterpreters(self):
854 # Verify that the _struct extension module can be initialized
855 # concurrently in subinterpreters (gh-140260).
856 try:
857 from concurrent.futures import InterpreterPoolExecutor
858 except ImportError:
859 raise unittest.SkipTest("InterpreterPoolExecutor not available")
860
861 code = "import struct"
862 with InterpreterPoolExecutor(max_workers=5) as executor:
863 results = executor.map(exec, [code] * 5)
864 self.assertListEqual(list(results), [None] * 5)
865
866 def test_operations_on_half_initialized_Struct(self):
867 S = struct.Struct.__new__(struct.Struct)
868
869 spam = array.array('b', b' ')
870 self.assertRaises(RuntimeError, S.iter_unpack, spam)
871 self.assertRaises(RuntimeError, S.pack, 1)
872 self.assertRaises(RuntimeError, S.pack_into, spam, 1)
873 self.assertRaises(RuntimeError, S.unpack, spam)
874 self.assertRaises(RuntimeError, S.unpack_from, spam)
875 self.assertRaises(RuntimeError, getattr, S, 'format')
876 self.assertRaises(RuntimeError, S.__sizeof__)
877 self.assertRaises(RuntimeError, repr, S)
878 self.assertEqual(S.size, -1)
879
880 def test_float_round_trip(self):
881 for format in (
882 "f", "<f", ">f",
883 "d", "<d", ">d",
884 "e", "<e", ">e",
885 ):
886 with self.subTest(format=format):
887 f = struct.unpack(format, struct.pack(format, 1.5))[0]
888 self.assertEqual(f, 1.5)
889 f = struct.unpack(format, struct.pack(format, NAN))[0]
890 self.assertTrue(math.isnan(f), f)
891 f = struct.unpack(format, struct.pack(format, INF))[0]
892 self.assertTrue(math.isinf(f), f)
893 self.assertEqual(math.copysign(1.0, f), 1.0)
894 f = struct.unpack(format, struct.pack(format, -INF))[0]
895 self.assertTrue(math.isinf(f), f)
896 self.assertEqual(math.copysign(1.0, f), -1.0)
897
898
899 class UnpackIteratorTest(unittest.TestCase):
900 """
901 Tests for iterative unpacking (struct.Struct.iter_unpack).
902 """
903
904 def test_construct(self):
905 def _check_iterator(it):
906 self.assertIsInstance(it, abc.Iterator)
907 self.assertIsInstance(it, abc.Iterable)
908 s = struct.Struct('>ibcp')
909 it = s.iter_unpack(b"")
910 _check_iterator(it)
911 it = s.iter_unpack(b"1234567")
912 _check_iterator(it)
913 # Wrong bytes length
914 with self.assertRaises(struct.error):
915 s.iter_unpack(b"123456")
916 with self.assertRaises(struct.error):
917 s.iter_unpack(b"12345678")
918 # Zero-length struct
919 s = struct.Struct('>')
920 with self.assertRaises(struct.error):
921 s.iter_unpack(b"")
922 with self.assertRaises(struct.error):
923 s.iter_unpack(b"12")
924
925 def test_uninstantiable(self):
926 iter_unpack_type = type(struct.Struct(">ibcp").iter_unpack(b""))
927 self.assertRaises(TypeError, iter_unpack_type)
928
929 def test_iterate(self):
930 s = struct.Struct('>IB')
931 b = bytes(range(1, 16))
932 it = s.iter_unpack(b)
933 self.assertEqual(next(it), (0x01020304, 5))
934 self.assertEqual(next(it), (0x06070809, 10))
935 self.assertEqual(next(it), (0x0b0c0d0e, 15))
936 self.assertRaises(StopIteration, next, it)
937 self.assertRaises(StopIteration, next, it)
938
939 def test_arbitrary_buffer(self):
940 s = struct.Struct('>IB')
941 b = bytes(range(1, 11))
942 it = s.iter_unpack(memoryview(b))
943 self.assertEqual(next(it), (0x01020304, 5))
944 self.assertEqual(next(it), (0x06070809, 10))
945 self.assertRaises(StopIteration, next, it)
946 self.assertRaises(StopIteration, next, it)
947
948 def test_length_hint(self):
949 lh = operator.length_hint
950 s = struct.Struct('>IB')
951 b = bytes(range(1, 16))
952 it = s.iter_unpack(b)
953 self.assertEqual(lh(it), 3)
954 next(it)
955 self.assertEqual(lh(it), 2)
956 next(it)
957 self.assertEqual(lh(it), 1)
958 next(it)
959 self.assertEqual(lh(it), 0)
960 self.assertRaises(StopIteration, next, it)
961 self.assertEqual(lh(it), 0)
962
963 def test_module_func(self):
964 # Sanity check for the global struct.iter_unpack()
965 it = struct.iter_unpack('>IB', bytes(range(1, 11)))
966 self.assertEqual(next(it), (0x01020304, 5))
967 self.assertEqual(next(it), (0x06070809, 10))
968 self.assertRaises(StopIteration, next, it)
969 self.assertRaises(StopIteration, next, it)
970
971 def test_half_float(self):
972 _testcapi = import_helper.import_module('_testcapi')
973 # Little-endian examples from:
974 # http://en.wikipedia.org/wiki/Half_precision_floating-point_format
975 format_bits_float__cleanRoundtrip_list = [
976 (b'\x00\x3c', 1.0),
977 (b'\x00\xc0', -2.0),
978 (b'\xff\x7b', 65504.0), # (max half precision)
979 (b'\x00\x04', 2**-14), # ~= 6.10352 * 10**-5 (min pos normal)
980 (b'\x01\x00', 2**-24), # ~= 5.96046 * 10**-8 (min pos subnormal)
981 (b'\x00\x00', 0.0),
982 (b'\x00\x80', -0.0),
983 (b'\x00\x7c', float('+inf')),
984 (b'\x00\xfc', float('-inf')),
985 (b'\x55\x35', 0.333251953125), # ~= 1/3
986 ]
987
988 for le_bits, f in format_bits_float__cleanRoundtrip_list:
989 be_bits = le_bits[::-1]
990 self.assertEqual(f, struct.unpack('<e', le_bits)[0])
991 self.assertEqual(le_bits, struct.pack('<e', f))
992 self.assertEqual(f, struct.unpack('>e', be_bits)[0])
993 self.assertEqual(be_bits, struct.pack('>e', f))
994 if sys.byteorder == 'little':
995 self.assertEqual(f, struct.unpack('e', le_bits)[0])
996 self.assertEqual(le_bits, struct.pack('e', f))
997 else:
998 self.assertEqual(f, struct.unpack('e', be_bits)[0])
999 self.assertEqual(be_bits, struct.pack('e', f))
1000
1001 # Check for NaN handling:
1002 format_bits__nan_list = [
1003 ('<e', b'\x01\xfc'),
1004 ('<e', b'\x00\xfe'),
1005 ('<e', b'\xff\xff'),
1006 ('<e', b'\x01\x7c'),
1007 ('<e', b'\x00\x7e'),
1008 ('<e', b'\xff\x7f'),
1009 ]
1010
1011 for formatcode, bits in format_bits__nan_list:
1012 self.assertTrue(math.isnan(struct.unpack('<e', bits)[0]))
1013 self.assertTrue(math.isnan(struct.unpack('>e', bits[::-1])[0]))
1014
1015 # Check that packing produces a bit pattern representing a quiet NaN:
1016 # all exponent bits and the msb of the fraction should all be 1.
1017 if _testcapi.nan_msb_is_signaling:
1018 # HP PA RISC and some MIPS CPUs use 0 for quiet, see:
1019 # https://en.wikipedia.org/wiki/NaN#Encoding
1020 expected = 0x7c
1021 else:
1022 expected = 0x7e
1023
1024 packed = struct.pack('<e', math.nan)
1025 self.assertEqual(packed[1] & 0x7e, expected)
1026 packed = struct.pack('<e', -math.nan)
1027 self.assertEqual(packed[1] & 0x7e, expected)
1028
1029 # Checks for round-to-even behavior
1030 format_bits_float__rounding_list = [
1031 ('>e', b'\x00\x01', 2.0**-25 + 2.0**-35), # Rounds to minimum subnormal
1032 ('>e', b'\x00\x00', 2.0**-25), # Underflows to zero (nearest even mode)
1033 ('>e', b'\x00\x00', 2.0**-26), # Underflows to zero
1034 ('>e', b'\x03\xff', 2.0**-14 - 2.0**-24), # Largest subnormal.
1035 ('>e', b'\x03\xff', 2.0**-14 - 2.0**-25 - 2.0**-65),
1036 ('>e', b'\x04\x00', 2.0**-14 - 2.0**-25),
1037 ('>e', b'\x04\x00', 2.0**-14), # Smallest normal.
1038 ('>e', b'\x3c\x01', 1.0+2.0**-11 + 2.0**-16), # rounds to 1.0+2**(-10)
1039 ('>e', b'\x3c\x00', 1.0+2.0**-11), # rounds to 1.0 (nearest even mode)
1040 ('>e', b'\x3c\x00', 1.0+2.0**-12), # rounds to 1.0
1041 ('>e', b'\x7b\xff', 65504), # largest normal
1042 ('>e', b'\x7b\xff', 65519), # rounds to 65504
1043 ('>e', b'\x80\x01', -2.0**-25 - 2.0**-35), # Rounds to minimum subnormal
1044 ('>e', b'\x80\x00', -2.0**-25), # Underflows to zero (nearest even mode)
1045 ('>e', b'\x80\x00', -2.0**-26), # Underflows to zero
1046 ('>e', b'\xbc\x01', -1.0-2.0**-11 - 2.0**-16), # rounds to 1.0+2**(-10)
1047 ('>e', b'\xbc\x00', -1.0-2.0**-11), # rounds to 1.0 (nearest even mode)
1048 ('>e', b'\xbc\x00', -1.0-2.0**-12), # rounds to 1.0
1049 ('>e', b'\xfb\xff', -65519), # rounds to 65504
1050 ]
1051
1052 for formatcode, bits, f in format_bits_float__rounding_list:
1053 self.assertEqual(bits, struct.pack(formatcode, f))
1054
1055 # This overflows, and so raises an error
1056 format_bits_float__roundingError_list = [
1057 # Values that round to infinity.
1058 ('>e', 65520.0),
1059 ('>e', 65536.0),
1060 ('>e', 1e300),
1061 ('>e', -65520.0),
1062 ('>e', -65536.0),
1063 ('>e', -1e300),
1064 ('<e', 65520.0),
1065 ('<e', 65536.0),
1066 ('<e', 1e300),
1067 ('<e', -65520.0),
1068 ('<e', -65536.0),
1069 ('<e', -1e300),
1070 ]
1071
1072 for formatcode, f in format_bits_float__roundingError_list:
1073 self.assertRaises(OverflowError, struct.pack, formatcode, f)
1074
1075 # Double rounding
1076 format_bits_float__doubleRoundingError_list = [
1077 ('>e', b'\x67\xff', 0x1ffdffffff * 2**-26), # should be 2047, if double-rounded 64>32>16, becomes 2048
1078 ]
1079
1080 for formatcode, bits, f in format_bits_float__doubleRoundingError_list:
1081 self.assertEqual(bits, struct.pack(formatcode, f))
1082
1083
1084 if __name__ == '__main__':
1085 unittest.main()