Lines Matching refs:struct
6 import struct
35 self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN)
38 self.assertRaises(struct.error, struct.calcsize, 'Z')
40 sz = struct.calcsize('i')
41 self.assertEqual(sz * 3, struct.calcsize('iii'))
45 sz = struct.calcsize(fmt)
46 sz3 = struct.calcsize(fmt3)
49 self.assertRaises(struct.error, struct.pack, 'iii', 3)
50 self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3)
51 self.assertRaises((TypeError, struct.error), struct.pack, 'i', 'foo')
52 self.assertRaises((TypeError, struct.error), struct.pack, 'P', 'foo')
53 self.assertRaises(struct.error, struct.unpack, 'd', b'flap')
54 s = struct.pack('ii', 1, 2)
55 self.assertRaises(struct.error, struct.unpack, 'iii', s)
56 self.assertRaises(struct.error, struct.unpack, 'i', s)
71 s = struct.pack(format, c, b, h, i, l, f, d, t)
72 cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s)
128 res = struct.pack(xfmt, arg)
130 self.assertEqual(struct.calcsize(xfmt), len(res))
131 rev = struct.unpack(xfmt, res)[0]
147 size = struct.calcsize(format)
154 signed_size = struct.calcsize(byteorder + format_pair[0])
155 unsigned_size = struct.calcsize(byteorder + format_pair[1])
159 self.assertEqual(struct.calcsize('b'), 1)
160 self.assertLessEqual(2, struct.calcsize('h'))
161 self.assertLessEqual(4, struct.calcsize('l'))
162 self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
163 self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
164 self.assertLessEqual(8, struct.calcsize('q'))
165 self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
166 self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('i'))
167 self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('P'))
182 self.bytesize = struct.calcsize(format)
196 def test_one(self, x, pack=struct.pack,
197 unpack=struct.unpack,
227 self.assertRaises((struct.error, TypeError), unpack, format,
231 self.assertRaises((OverflowError, ValueError, struct.error),
286 self.assertRaises((TypeError, struct.error),
287 struct.pack, self.format,
289 self.assertRaises((TypeError, struct.error),
290 struct.pack, self.format,
292 self.assertRaises((TypeError, struct.error),
293 struct.pack, self.format,
295 self.assertRaises((TypeError, struct.error),
296 struct.pack, self.format,
298 self.assertRaises((TypeError, struct.error),
299 struct.pack, self.format,
306 struct.pack(format, obj)
314 self.assertRaises((TypeError, struct.error),
315 struct.pack, self.format,
326 with self.assertRaises(struct.error) as cm:
332 assertStructError(struct.calcsize, format)
333 assertStructError(struct.pack, format, 0)
334 assertStructError(struct.unpack, format, b"")
347 got = struct.pack(code, input)
349 (got,) = struct.unpack(code, got)
363 packed = struct.pack("<f", smaller)
364 unpacked = struct.unpack("<f", packed)[0]
368 bigpacked = struct.pack(">f", smaller)
370 unpacked = struct.unpack(">f", bigpacked)[0]
376 packed = struct.pack(">f", big)
377 unpacked = struct.unpack(">f", packed)[0]
383 self.assertRaises(OverflowError, struct.pack, ">f", big)
388 self.assertRaises(struct.error, struct.pack, format, 1.0)
389 self.assertRaises(struct.error, struct.pack, format, 1.5)
390 self.assertRaises(struct.error, struct.pack, 'P', 1.0)
391 self.assertRaises(struct.error, struct.pack, 'P', 1.5)
396 s = struct.Struct(fmt)
405 self.assertRaises(struct.error, s.unpack_from, data, i)
408 self.assertEqual(struct.unpack_from(fmt, data), (b'abcd',))
409 self.assertEqual(struct.unpack_from(fmt, data, 2), (b'cd01',))
410 self.assertEqual(struct.unpack_from(fmt, data, 4), (b'0123',))
412 self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
414 self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)
424 s = struct.Struct(fmt)
438 self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
440 self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
445 self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
452 pack_into = lambda *args: struct.pack_into(fmt, *args)
466 self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
468 self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
476 value, = struct.unpack('>I', data)
488 packedFalse = struct.pack(falseFormat, *false)
489 unpackedFalse = struct.unpack(falseFormat, packedFalse)
492 packedTrue = struct.pack(trueFormat, *true)
493 unpackedTrue = struct.unpack(trueFormat, packedTrue)
503 packed = struct.pack(prefix+'?', 1)
505 self.assertEqual(len(packed), struct.calcsize(prefix+'?'))
512 struct.pack(prefix + '?', ExplodingBool())
520 self.assertTrue(struct.unpack('>?', c)[0])
524 self.assertRaises(struct.error, struct.calcsize, hugecount)
527 self.assertRaises(struct.error, struct.calcsize, hugecount2)
533 self.assertRaises(struct.error, struct.pack, '12345')
534 self.assertRaises(struct.error, struct.unpack, '12345', b'')
535 self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
536 self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)
539 self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
540 self.assertRaises(struct.error, struct.unpack, 'c12345', b'x')
541 self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
543 self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
547 self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
548 self.assertRaises(struct.error, struct.unpack, '14s42',
550 self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
552 self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
558 s = struct.Struct('i')
565 totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
566 support.check_sizeof(self, struct.Struct(format_str), totalsize)
588 with self.assertRaisesRegex(struct.error, regex1):
589 struct.pack_into('b', bytearray(1), 5, 1)
596 with self.assertRaisesRegex(struct.error, regex2):
597 struct.unpack_from('b', bytearray(1), 5)
602 struct.error,
604 struct.pack_into('<I', byte_list, -2, 123)
607 struct.error,
609 struct.pack_into('<B', byte_list, -11, 123)
612 struct.error,
614 struct.unpack_from('<I', byte_list, -2)
617 struct.error,
619 struct.unpack_from('<B', byte_list, -11)
628 with self.assertRaisesRegex(struct.error, regex1):
629 struct.pack_into('<I', bytearray(10), sys.maxsize, 1)
636 with self.assertRaisesRegex(struct.error, regex2):
637 struct.unpack_from('<I', bytearray(10), sys.maxsize)
644 struct.unpack('b', 0)
646 self.assertEqual(struct.unpack('b', b'a'), (b'a'[0],))
649 s = struct.Struct('=i2H')
653 s2 = struct.Struct(s.format.encode())
677 with self.assertRaisesRegex(struct.error,
679 struct.calcsize(s)
691 s = struct.Struct('>ibcp')
697 with self.assertRaises(struct.error):
699 with self.assertRaises(struct.error):
702 s = struct.Struct('>')
703 with self.assertRaises(struct.error):
705 with self.assertRaises(struct.error):
709 iter_unpack_type = type(struct.Struct(">ibcp").iter_unpack(b""))
713 s = struct.Struct('>IB')
723 s = struct.Struct('>IB')
733 s = struct.Struct('>IB')
748 it = struct.iter_unpack('>IB', bytes(range(1, 11)))
772 self.assertEqual(f, struct.unpack('<e', le_bits)[0])
773 self.assertEqual(le_bits, struct.pack('<e', f))
774 self.assertEqual(f, struct.unpack('>e', be_bits)[0])
775 self.assertEqual(be_bits, struct.pack('>e', f))
777 self.assertEqual(f, struct.unpack('e', le_bits)[0])
778 self.assertEqual(le_bits, struct.pack('e', f))
780 self.assertEqual(f, struct.unpack('e', be_bits)[0])
781 self.assertEqual(be_bits, struct.pack('e', f))
794 self.assertTrue(math.isnan(struct.unpack('<e', bits)[0]))
795 self.assertTrue(math.isnan(struct.unpack('>e', bits[::-1])[0]))
799 packed = struct.pack('<e', math.nan)
801 packed = struct.pack('<e', -math.nan)
828 self.assertEqual(bits, struct.pack(formatcode, f))
848 self.assertRaises(OverflowError, struct.pack, formatcode, f)
856 self.assertEqual(bits, struct.pack(formatcode, f))