Skip to content

Commit ebcf358

Browse files
Fix test_Struct_reinitialization on big-endian and add tests for invalid format.
1 parent 3de045b commit ebcf358

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

Lib/test/test_struct.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,31 @@ def test_Struct_reinitialization(self):
584584
# Issue 9422: there was a memory leak when reinitializing a
585585
# Struct instance. This test can be used to detect the leak
586586
# when running with regrtest -L.
587-
s = struct.Struct('i')
587+
s = struct.Struct('>h')
588588
with self.assertWarns(DeprecationWarning):
589-
s.__init__('ii')
590-
self.assertEqual(s.format, 'ii')
591-
packed = b'\x01\x00\x00\x00\x02\x00\x00\x00'
589+
s.__init__('>hh')
590+
self.assertEqual(s.format, '>hh')
591+
packed = b'\x00\x01\x00\x02'
592+
self.assertEqual(s.pack(1, 2), packed)
593+
self.assertEqual(s.unpack(packed), (1, 2))
594+
595+
with self.assertWarns(DeprecationWarning):
596+
s.__init__('>hh')
597+
self.assertEqual(s.format, '>hh')
598+
self.assertEqual(s.pack(1, 2), packed)
599+
self.assertEqual(s.unpack(packed), (1, 2))
600+
601+
with self.assertWarns(DeprecationWarning):
602+
with self.assertRaises(UnicodeEncodeError):
603+
s.__init__('\u20ac')
604+
self.assertEqual(s.format, '>hh')
605+
self.assertEqual(s.pack(1, 2), packed)
606+
self.assertEqual(s.unpack(packed), (1, 2))
607+
608+
with self.assertWarns(DeprecationWarning):
609+
with self.assertRaises(struct.error):
610+
s.__init__('$')
611+
self.assertEqual(s.format, '$')
592612
self.assertEqual(s.pack(1, 2), packed)
593613
self.assertEqual(s.unpack(packed), (1, 2))
594614

@@ -895,18 +915,18 @@ def __init__(self, newargs, initargs):
895915
MyStruct((), ('>h',))
896916
with self.assertRaises(TypeError):
897917
MyStruct((42,), ('>h',))
898-
with self.assertRaises(TypeError):
899-
with self.assertWarns(FutureWarning):
918+
with self.assertWarns(FutureWarning):
919+
with self.assertRaises(TypeError):
900920
MyStruct(('>h',), (42,))
901921
with self.assertRaises(struct.error):
902922
MyStruct(('$',), ('>h',))
903-
with self.assertRaises(struct.error):
904-
with self.assertWarns(FutureWarning):
923+
with self.assertWarns(FutureWarning):
924+
with self.assertRaises(struct.error):
905925
MyStruct(('>h',), ('$',))
906926
with self.assertRaises(UnicodeEncodeError):
907927
MyStruct(('\u20ac',), ('>h',))
908-
with self.assertRaises(UnicodeEncodeError):
909-
with self.assertWarns(FutureWarning):
928+
with self.assertWarns(FutureWarning):
929+
with self.assertRaises(UnicodeEncodeError):
910930
MyStruct(('>h',), ('\u20ac',))
911931
with self.assertWarns(FutureWarning):
912932
my_struct = MyStruct(('>h',), ('<h',))

0 commit comments

Comments
 (0)