@@ -428,56 +428,63 @@ def test_unpack_from(self):
428428 self .assertEqual (s .unpack_from (buffer = test_string , offset = 2 ),
429429 (b'cd01' ,))
430430
431- def test_pack_into (self ):
431+ def _test_pack_into (self , pack_into ):
432432 test_string = b'Reykjavik rocks, eow!'
433- writable_buf = array .array ('b' , b' ' * 100 )
434- fmt = '21s'
435- s = struct .Struct (fmt )
433+ writable_buf = memoryview (array .array ('b' , b' ' * 100 ))
436434
437435 # Test without offset
438- s . pack_into (writable_buf , 0 , test_string )
436+ pack_into (writable_buf , 0 , test_string )
439437 from_buf = writable_buf .tobytes ()[:len (test_string )]
440438 self .assertEqual (from_buf , test_string )
441439
442440 # Test with offset.
443- s . pack_into (writable_buf , 10 , test_string )
441+ pack_into (writable_buf , 10 , test_string )
444442 from_buf = writable_buf .tobytes ()[:len (test_string )+ 10 ]
445443 self .assertEqual (from_buf , test_string [:10 ] + test_string )
446444
445+ # Test with negative offset.
446+ pack_into (writable_buf , - 30 , test_string )
447+ from_buf = writable_buf .tobytes ()[- 30 :- 30 + len (test_string )]
448+ self .assertEqual (from_buf , test_string )
449+
447450 # Go beyond boundaries.
448451 small_buf = array .array ('b' , b' ' * 10 )
449- self .assertRaises ((ValueError , struct .error ), s .pack_into , small_buf , 0 ,
450- test_string )
451- self .assertRaises ((ValueError , struct .error ), s .pack_into , small_buf , 2 ,
452- test_string )
452+ with self .assertRaises ((ValueError , struct .error )):
453+ pack_into (small_buf , 0 , test_string )
454+ with self .assertRaises ((ValueError , struct .error )):
455+ pack_into (writable_buf , 90 , test_string )
456+ with self .assertRaises ((ValueError , struct .error )):
457+ pack_into (writable_buf , - 10 , test_string )
458+ with self .assertRaises ((ValueError , struct .error )):
459+ pack_into (writable_buf , 150 , test_string )
460+ with self .assertRaises ((ValueError , struct .error )):
461+ pack_into (writable_buf , - 150 , test_string )
462+
463+ # Test invalid buffer.
464+ self .assertRaises (TypeError , pack_into , b' ' * 100 , 0 , test_string )
465+ self .assertRaises (TypeError , pack_into , ' ' * 100 , 0 , test_string )
466+ self .assertRaises (TypeError , pack_into , [0 ]* 100 , 0 , test_string )
467+ self .assertRaises (TypeError , pack_into , None , 0 , test_string )
468+ self .assertRaises (TypeError , pack_into , writable_buf [::2 ], 0 , test_string )
469+ self .assertRaises (TypeError , pack_into , writable_buf [::- 1 ], 0 , test_string )
470+
471+ # Test bogus offset (issue bpo-3694)
472+ with self .assertRaises (TypeError ):
473+ pack_into (writable_buf , None , test_string )
474+ with self .assertRaises (TypeError ):
475+ pack_into (writable_buf , 0.0 , test_string )
476+ with self .assertRaises ((IndexError , OverflowError )):
477+ pack_into (writable_buf , 2 ** 1000 , test_string )
478+ with self .assertRaises ((IndexError , OverflowError )):
479+ pack_into (writable_buf , - 2 ** 1000 , test_string )
453480
454- # Test bogus offset (issue 3694)
455- sb = small_buf
456- self .assertRaises ((TypeError , struct .error ), struct .pack_into , b'' , sb ,
457- None )
481+ def test_pack_into (self ):
482+ s = struct .Struct ('21s' )
483+ self ._test_pack_into (s .pack_into )
458484
459485 def test_pack_into_fn (self ):
460- test_string = b'Reykjavik rocks, eow!'
461- writable_buf = array .array ('b' , b' ' * 100 )
462- fmt = '21s'
463- pack_into = lambda * args : struct .pack_into (fmt , * args )
464-
465- # Test without offset.
466- pack_into (writable_buf , 0 , test_string )
467- from_buf = writable_buf .tobytes ()[:len (test_string )]
468- self .assertEqual (from_buf , test_string )
469-
470- # Test with offset.
471- pack_into (writable_buf , 10 , test_string )
472- from_buf = writable_buf .tobytes ()[:len (test_string )+ 10 ]
473- self .assertEqual (from_buf , test_string [:10 ] + test_string )
474-
475- # Go beyond boundaries.
476- small_buf = array .array ('b' , b' ' * 10 )
477- self .assertRaises ((ValueError , struct .error ), pack_into , small_buf , 0 ,
478- test_string )
479- self .assertRaises ((ValueError , struct .error ), pack_into , small_buf , 2 ,
480- test_string )
486+ pack_into = lambda * args : struct .pack_into ('21s' , * args )
487+ self ._test_pack_into (pack_into )
481488
482489 def test_unpack_with_buffer (self ):
483490 # SF bug 1563759: struct.unpack doesn't support buffer protocol objects
0 commit comments