@@ -433,56 +433,63 @@ def test_unpack_from(self):
433433 self .assertEqual (s .unpack_from (buffer = test_string , offset = 2 ),
434434 (b'cd01' ,))
435435
436- def test_pack_into (self ):
436+ def _test_pack_into (self , pack_into ):
437437 test_string = b'Reykjavik rocks, eow!'
438- writable_buf = array .array ('b' , b' ' * 100 )
439- fmt = '21s'
440- s = struct .Struct (fmt )
438+ writable_buf = memoryview (array .array ('b' , b' ' * 100 ))
441439
442440 # Test without offset
443- s . pack_into (writable_buf , 0 , test_string )
441+ pack_into (writable_buf , 0 , test_string )
444442 from_buf = writable_buf .tobytes ()[:len (test_string )]
445443 self .assertEqual (from_buf , test_string )
446444
447445 # Test with offset.
448- s . pack_into (writable_buf , 10 , test_string )
446+ pack_into (writable_buf , 10 , test_string )
449447 from_buf = writable_buf .tobytes ()[:len (test_string )+ 10 ]
450448 self .assertEqual (from_buf , test_string [:10 ] + test_string )
451449
450+ # Test with negative offset.
451+ pack_into (writable_buf , - 30 , test_string )
452+ from_buf = writable_buf .tobytes ()[- 30 :- 30 + len (test_string )]
453+ self .assertEqual (from_buf , test_string )
454+
452455 # Go beyond boundaries.
453456 small_buf = array .array ('b' , b' ' * 10 )
454- self .assertRaises ((ValueError , struct .error ), s .pack_into , small_buf , 0 ,
455- test_string )
456- self .assertRaises ((ValueError , struct .error ), s .pack_into , small_buf , 2 ,
457- test_string )
457+ with self .assertRaises ((ValueError , struct .error )):
458+ pack_into (small_buf , 0 , test_string )
459+ with self .assertRaises ((ValueError , struct .error )):
460+ pack_into (writable_buf , 90 , test_string )
461+ with self .assertRaises ((ValueError , struct .error )):
462+ pack_into (writable_buf , - 10 , test_string )
463+ with self .assertRaises ((ValueError , struct .error )):
464+ pack_into (writable_buf , 150 , test_string )
465+ with self .assertRaises ((ValueError , struct .error )):
466+ pack_into (writable_buf , - 150 , test_string )
467+
468+ # Test invalid buffer.
469+ self .assertRaises (TypeError , pack_into , b' ' * 100 , 0 , test_string )
470+ self .assertRaises (TypeError , pack_into , ' ' * 100 , 0 , test_string )
471+ self .assertRaises (TypeError , pack_into , [0 ]* 100 , 0 , test_string )
472+ self .assertRaises (TypeError , pack_into , None , 0 , test_string )
473+ self .assertRaises (TypeError , pack_into , writable_buf [::2 ], 0 , test_string )
474+ self .assertRaises (TypeError , pack_into , writable_buf [::- 1 ], 0 , test_string )
475+
476+ # Test bogus offset (issue bpo-3694)
477+ with self .assertRaises (TypeError ):
478+ pack_into (writable_buf , None , test_string )
479+ with self .assertRaises (TypeError ):
480+ pack_into (writable_buf , 0.0 , test_string )
481+ with self .assertRaises ((IndexError , OverflowError )):
482+ pack_into (writable_buf , 2 ** 1000 , test_string )
483+ with self .assertRaises ((IndexError , OverflowError )):
484+ pack_into (writable_buf , - 2 ** 1000 , test_string )
458485
459- # Test bogus offset (issue 3694)
460- sb = small_buf
461- self .assertRaises ((TypeError , struct .error ), struct .pack_into , b'' , sb ,
462- None )
486+ def test_pack_into (self ):
487+ s = struct .Struct ('21s' )
488+ self ._test_pack_into (s .pack_into )
463489
464490 def test_pack_into_fn (self ):
465- test_string = b'Reykjavik rocks, eow!'
466- writable_buf = array .array ('b' , b' ' * 100 )
467- fmt = '21s'
468- pack_into = lambda * args : struct .pack_into (fmt , * args )
469-
470- # Test without offset.
471- pack_into (writable_buf , 0 , test_string )
472- from_buf = writable_buf .tobytes ()[:len (test_string )]
473- self .assertEqual (from_buf , test_string )
474-
475- # Test with offset.
476- pack_into (writable_buf , 10 , test_string )
477- from_buf = writable_buf .tobytes ()[:len (test_string )+ 10 ]
478- self .assertEqual (from_buf , test_string [:10 ] + test_string )
479-
480- # Go beyond boundaries.
481- small_buf = array .array ('b' , b' ' * 10 )
482- self .assertRaises ((ValueError , struct .error ), pack_into , small_buf , 0 ,
483- test_string )
484- self .assertRaises ((ValueError , struct .error ), pack_into , small_buf , 2 ,
485- test_string )
491+ pack_into = lambda * args : struct .pack_into ('21s' , * args )
492+ self ._test_pack_into (pack_into )
486493
487494 def test_unpack_with_buffer (self ):
488495 # SF bug 1563759: struct.unpack doesn't support buffer protocol objects
0 commit comments