Skip to content

Commit aa12acc

Browse files
authored
Update test_struct.py from 3.14.4 (RustPython#7614)
1 parent fd21173 commit aa12acc

1 file changed

Lines changed: 63 additions & 2 deletions

File tree

Lib/test/test_struct.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ def test_705836(self):
398398
big = (1 << 25) - 1
399399
big = math.ldexp(big, 127 - 24)
400400
self.assertRaises(OverflowError, struct.pack, ">f", big)
401+
self.assertRaises(OverflowError, struct.pack, "<f", big)
402+
# same for native format, see gh-145633
403+
self.assertRaises(OverflowError, struct.pack, "f", big)
404+
405+
# And for half-floats
406+
big = (1 << 11) - 1
407+
big = math.ldexp(big, 15 - 10)
408+
packed = struct.pack(">e", big)
409+
unpacked = struct.unpack(">e", packed)[0]
410+
self.assertEqual(big, unpacked)
411+
big = (1 << 12) - 1
412+
big = math.ldexp(big, 15 - 11)
413+
self.assertRaises(OverflowError, struct.pack, ">e", big)
414+
self.assertRaises(OverflowError, struct.pack, "<e", big)
415+
self.assertRaises(OverflowError, struct.pack, "e", big)
401416

402417
def test_1530559(self):
403418
for code, byteorder in iter_integer_formats():
@@ -555,6 +570,15 @@ def test_count_overflow(self):
555570
hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
556571
self.assertRaises(struct.error, struct.calcsize, hugecount2)
557572

573+
hugecount3 = '{}i{}q'.format(sys.maxsize // 4, sys.maxsize // 8)
574+
self.assertRaises(struct.error, struct.calcsize, hugecount3)
575+
576+
hugecount4 = '{}?s'.format(sys.maxsize)
577+
self.assertRaises(struct.error, struct.calcsize, hugecount4)
578+
579+
hugecount5 = '{}?p'.format(sys.maxsize)
580+
self.assertRaises(struct.error, struct.calcsize, hugecount5)
581+
558582
def test_trailing_counter(self):
559583
store = array.array('b', b' '*100)
560584

@@ -580,12 +604,29 @@ def test_trailing_counter(self):
580604
'spam and eggs')
581605
self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
582606

607+
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: '>h' != '>hh'
583608
def test_Struct_reinitialization(self):
584609
# Issue 9422: there was a memory leak when reinitializing a
585610
# Struct instance. This test can be used to detect the leak
586611
# when running with regrtest -L.
587-
s = struct.Struct('i')
588-
s.__init__('ii')
612+
s = struct.Struct('>h')
613+
s.__init__('>hh')
614+
self.assertEqual(s.format, '>hh')
615+
packed = b'\x00\x01\x00\x02'
616+
self.assertEqual(s.pack(1, 2), packed)
617+
self.assertEqual(s.unpack(packed), (1, 2))
618+
619+
with self.assertRaises(UnicodeEncodeError):
620+
s.__init__('\udc00')
621+
self.assertEqual(s.format, '>hh')
622+
self.assertEqual(s.pack(1, 2), packed)
623+
self.assertEqual(s.unpack(packed), (1, 2))
624+
625+
with self.assertRaises(struct.error):
626+
s.__init__('$')
627+
self.assertEqual(s.format, '>hh')
628+
self.assertEqual(s.pack(1, 2), packed)
629+
self.assertEqual(s.unpack(packed), (1, 2))
589630

590631
def check_sizeof(self, format_str, number_of_codes):
591632
# The size of 'PyStructObject'
@@ -840,8 +881,28 @@ def test_operations_on_half_initialized_Struct(self):
840881
self.assertRaises(RuntimeError, S.unpack, spam)
841882
self.assertRaises(RuntimeError, S.unpack_from, spam)
842883
self.assertRaises(RuntimeError, getattr, S, 'format')
884+
self.assertRaises(RuntimeError, S.__sizeof__)
885+
self.assertRaises(RuntimeError, repr, S)
843886
self.assertEqual(S.size, -1)
844887

888+
def test_float_round_trip(self):
889+
for format in (
890+
"f", "<f", ">f",
891+
"d", "<d", ">d",
892+
"e", "<e", ">e",
893+
):
894+
with self.subTest(format=format):
895+
f = struct.unpack(format, struct.pack(format, 1.5))[0]
896+
self.assertEqual(f, 1.5)
897+
f = struct.unpack(format, struct.pack(format, NAN))[0]
898+
self.assertTrue(math.isnan(f), f)
899+
f = struct.unpack(format, struct.pack(format, INF))[0]
900+
self.assertTrue(math.isinf(f), f)
901+
self.assertEqual(math.copysign(1.0, f), 1.0)
902+
f = struct.unpack(format, struct.pack(format, -INF))[0]
903+
self.assertTrue(math.isinf(f), f)
904+
self.assertEqual(math.copysign(1.0, f), -1.0)
905+
845906

846907
class UnpackIteratorTest(unittest.TestCase):
847908
"""

0 commit comments

Comments
 (0)