99import weakref
1010
1111from test import support
12- from test .support import import_helper
12+ from test .support import import_helper , suppress_immortalization
1313from test .support .script_helper import assert_python_ok
1414
1515ISBIGENDIAN = sys .byteorder == "big"
@@ -96,6 +96,13 @@ def test_new_features(self):
9696 ('10s' , b'helloworld' , b'helloworld' , b'helloworld' , 0 ),
9797 ('11s' , b'helloworld' , b'helloworld\0 ' , b'helloworld\0 ' , 1 ),
9898 ('20s' , b'helloworld' , b'helloworld' + 10 * b'\0 ' , b'helloworld' + 10 * b'\0 ' , 1 ),
99+ ('0p' , b'helloworld' , b'' , b'' , 1 ),
100+ ('1p' , b'helloworld' , b'\x00 ' , b'\x00 ' , 1 ),
101+ ('2p' , b'helloworld' , b'\x01 h' , b'\x01 h' , 1 ),
102+ ('10p' , b'helloworld' , b'\x09 helloworl' , b'\x09 helloworl' , 1 ),
103+ ('11p' , b'helloworld' , b'\x0A helloworld' , b'\x0A helloworld' , 0 ),
104+ ('12p' , b'helloworld' , b'\x0A helloworld\0 ' , b'\x0A helloworld\0 ' , 1 ),
105+ ('20p' , b'helloworld' , b'\x0A helloworld' + 9 * b'\0 ' , b'\x0A helloworld' + 9 * b'\0 ' , 1 ),
99106 ('b' , 7 , b'\7 ' , b'\7 ' , 0 ),
100107 ('b' , - 7 , b'\371 ' , b'\371 ' , 0 ),
101108 ('B' , 7 , b'\7 ' , b'\7 ' , 0 ),
@@ -339,6 +346,7 @@ def assertStructError(func, *args, **kwargs):
339346 def test_p_code (self ):
340347 # Test p ("Pascal string") code.
341348 for code , input , expected , expectedback in [
349+ ('0p' , b'abc' , b'' , b'' ),
342350 ('p' , b'abc' , b'\x00 ' , b'' ),
343351 ('1p' , b'abc' , b'\x00 ' , b'' ),
344352 ('2p' , b'abc' , b'\x01 a' , b'a' ),
@@ -523,6 +531,9 @@ def __bool__(self):
523531
524532 for c in [b'\x01 ' , b'\x7f ' , b'\xff ' , b'\x0f ' , b'\xf0 ' ]:
525533 self .assertTrue (struct .unpack ('>?' , c )[0 ])
534+ self .assertTrue (struct .unpack ('<?' , c )[0 ])
535+ self .assertTrue (struct .unpack ('=?' , c )[0 ])
536+ self .assertTrue (struct .unpack ('@?' , c )[0 ])
526537
527538 def test_count_overflow (self ):
528539 hugecount = '{}b' .format (sys .maxsize + 1 )
@@ -582,6 +593,7 @@ def test__sizeof__(self):
582593 self .check_sizeof ('187s' , 1 )
583594 self .check_sizeof ('20p' , 1 )
584595 self .check_sizeof ('0s' , 1 )
596+ self .check_sizeof ('0p' , 1 )
585597 self .check_sizeof ('0c' , 0 )
586598
587599 def test_boundary_error_message (self ):
@@ -678,6 +690,7 @@ def __del__(self):
678690 self .assertIn (b"Exception ignored in:" , stderr )
679691 self .assertIn (b"C.__del__" , stderr )
680692
693+ @suppress_immortalization ()
681694 def test__struct_reference_cycle_cleaned_up (self ):
682695 # Regression test for python/cpython#94207.
683696
@@ -714,6 +727,74 @@ def test_issue35714(self):
714727 'embedded null character' ):
715728 struct .calcsize (s )
716729
730+ @support .cpython_only
731+ def test_issue98248 (self ):
732+ def test_error_msg (prefix , int_type , is_unsigned ):
733+ fmt_str = prefix + int_type
734+ size = struct .calcsize (fmt_str )
735+ if is_unsigned :
736+ max_ = 2 ** (size * 8 ) - 1
737+ min_ = 0
738+ else :
739+ max_ = 2 ** (size * 8 - 1 ) - 1
740+ min_ = - 2 ** (size * 8 - 1 )
741+ error_msg = f"'{ int_type } ' format requires { min_ } <= number <= { max_ } "
742+ for number in [int (- 1e50 ), min_ - 1 , max_ + 1 , int (1e50 )]:
743+ with self .subTest (format_str = fmt_str , number = number ):
744+ with self .assertRaisesRegex (struct .error , error_msg ):
745+ struct .pack (fmt_str , number )
746+ error_msg = "required argument is not an integer"
747+ not_number = ""
748+ with self .subTest (format_str = fmt_str , number = not_number ):
749+ with self .assertRaisesRegex (struct .error , error_msg ):
750+ struct .pack (fmt_str , not_number )
751+
752+ for prefix in '@=<>' :
753+ for int_type in 'BHILQ' :
754+ test_error_msg (prefix , int_type , True )
755+ for int_type in 'bhilq' :
756+ test_error_msg (prefix , int_type , False )
757+
758+ int_type = 'N'
759+ test_error_msg ('@' , int_type , True )
760+
761+ int_type = 'n'
762+ test_error_msg ('@' , int_type , False )
763+
764+ @support .cpython_only
765+ def test_issue98248_error_propagation (self ):
766+ class Div0 :
767+ def __index__ (self ):
768+ 1 / 0
769+
770+ def test_error_propagation (fmt_str ):
771+ with self .subTest (format_str = fmt_str , exception = "ZeroDivisionError" ):
772+ with self .assertRaises (ZeroDivisionError ):
773+ struct .pack (fmt_str , Div0 ())
774+
775+ for prefix in '@=<>' :
776+ for int_type in 'BHILQbhilq' :
777+ test_error_propagation (prefix + int_type )
778+
779+ test_error_propagation ('N' )
780+ test_error_propagation ('n' )
781+
782+ # TODO: RUSTPYTHON
783+ @unittest .expectedFailure
784+ def test_struct_subclass_instantiation (self ):
785+ # Regression test for https://github.com/python/cpython/issues/112358
786+ class MyStruct (struct .Struct ):
787+ def __init__ (self ):
788+ super ().__init__ ('>h' )
789+
790+ my_struct = MyStruct ()
791+ self .assertEqual (my_struct .pack (12345 ), b'\x30 \x39 ' )
792+
793+ # TODO: RUSTPYTHON
794+ @unittest .expectedFailure
795+ def test_repr (self ):
796+ s = struct .Struct ('=i2H' )
797+ self .assertEqual (repr (s ), f'Struct({ s .format !r} )' )
717798
718799class UnpackIteratorTest (unittest .TestCase ):
719800 """
@@ -895,4 +976,4 @@ def test_half_float(self):
895976
896977
897978if __name__ == '__main__' :
898- unittest .main ()
979+ unittest .main ()
0 commit comments