@@ -101,7 +101,7 @@ def deprecated_err(func, *args):
101
101
simple_err (struct .unpack , 'iii' , s )
102
102
simple_err (struct .unpack , 'i' , s )
103
103
104
- c = str8 ('a' )
104
+ c = str8 (b 'a' )
105
105
b = 1
106
106
h = 255
107
107
i = 65535
@@ -186,7 +186,7 @@ def deprecated_err(func, *args):
186
186
if isinstance (arg , str ):
187
187
# Strings are returned as str8 since you can't know the encoding of
188
188
# the string when packed.
189
- arg = str8 (arg )
189
+ arg = str8 (arg , 'latin1' )
190
190
if rev != arg and not asy :
191
191
raise TestFailed ("unpack(%r, %r) -> (%r,) # expected (%r,)" % (
192
192
fmt , res , rev , arg ))
@@ -428,14 +428,14 @@ def run(self):
428
428
429
429
def test_p_code ():
430
430
for code , input , expected , expectedback in [
431
- ('p' ,'abc' , '\x00 ' , str8 ('' )),
432
- ('1p' , 'abc' , '\x00 ' , str8 ('' )),
433
- ('2p' , 'abc' , '\x01 a' , str8 ('a' )),
434
- ('3p' , 'abc' , '\x02 ab' , str8 ('ab' )),
435
- ('4p' , 'abc' , '\x03 abc' , str8 ('abc' )),
436
- ('5p' , 'abc' , '\x03 abc\x00 ' , str8 ('abc' )),
437
- ('6p' , 'abc' , '\x03 abc\x00 \x00 ' , str8 ('abc' )),
438
- ('1000p' , 'x' * 1000 , '\xff ' + 'x' * 999 , str8 ('x' * 255 ))]:
431
+ ('p' ,'abc' , '\x00 ' , str8 ()),
432
+ ('1p' , 'abc' , '\x00 ' , str8 ()),
433
+ ('2p' , 'abc' , '\x01 a' , str8 (b 'a' )),
434
+ ('3p' , 'abc' , '\x02 ab' , str8 (b 'ab' )),
435
+ ('4p' , 'abc' , '\x03 abc' , str8 (b 'abc' )),
436
+ ('5p' , 'abc' , '\x03 abc\x00 ' , str8 (b 'abc' )),
437
+ ('6p' , 'abc' , '\x03 abc\x00 \x00 ' , str8 (b 'abc' )),
438
+ ('1000p' , 'x' * 1000 , '\xff ' + 'x' * 999 , str8 (b 'x'* 255 ))]:
439
439
expected = bytes (expected , "latin-1" )
440
440
got = struct .pack (code , input )
441
441
if got != expected :
@@ -564,20 +564,24 @@ def test_unpack_from():
564
564
if verbose :
565
565
print ("test_unpack_from using" , cls .__name__ )
566
566
data = cls (test_string )
567
- vereq (s .unpack_from (data ), (str8 ('abcd' ),))
568
- vereq (s .unpack_from (data , 2 ), (str8 ('cd01' ),))
569
- vereq (s .unpack_from (data , 4 ), (str8 ('0123' ),))
567
+ if not isinstance (data , (str8 , bytes )):
568
+ bytes_data = str8 (data , 'latin1' )
569
+ else :
570
+ bytes_data = data
571
+ vereq (s .unpack_from (data ), (str8 (b'abcd' ),))
572
+ vereq (s .unpack_from (data , 2 ), (str8 (b'cd01' ),))
573
+ vereq (s .unpack_from (data , 4 ), (str8 (b'0123' ),))
570
574
for i in range (6 ):
571
- vereq (s .unpack_from (data , i ), (str8 ( data [i :i + 4 ]) ,))
575
+ vereq (s .unpack_from (data , i ), (bytes_data [i :i + 4 ],))
572
576
for i in range (6 , len (test_string ) + 1 ):
573
577
simple_err (s .unpack_from , data , i )
574
578
for cls in (str , str8 , bytes ): # XXX + memoryview
575
579
data = cls (test_string )
576
- vereq (struct .unpack_from (fmt , data ), (str8 ('abcd' ),))
577
- vereq (struct .unpack_from (fmt , data , 2 ), (str8 ('cd01' ),))
578
- vereq (struct .unpack_from (fmt , data , 4 ), (str8 ('0123' ),))
580
+ vereq (struct .unpack_from (fmt , data ), (str8 (b 'abcd' ),))
581
+ vereq (struct .unpack_from (fmt , data , 2 ), (str8 (b 'cd01' ),))
582
+ vereq (struct .unpack_from (fmt , data , 4 ), (str8 (b '0123' ),))
579
583
for i in range (6 ):
580
- vereq (struct .unpack_from (fmt , data , i ), (str8 ( data [i :i + 4 ]) ,))
584
+ vereq (struct .unpack_from (fmt , data , i ), (bytes_data [i :i + 4 ],))
581
585
for i in range (6 , len (test_string ) + 1 ):
582
586
simple_err (struct .unpack_from , fmt , data , i )
583
587
0 commit comments