@@ -82,58 +82,52 @@ def test_new_features(self):
8282 # Test some of the new features in detail
8383 # (format, argument, big-endian result, little-endian result, asymmetric)
8484 tests = [
85- ('c' , 'a' , 'a' , 'a' , 0 ),
86- ('xc' , 'a' , '\0 a' , '\0 a' , 0 ),
87- ('cx' , 'a' , 'a\0 ' , 'a\0 ' , 0 ),
88- ('s' , 'a' , 'a' , 'a' , 0 ),
89- ('0s' , 'helloworld' , '' , '' , 1 ),
90- ('1s' , 'helloworld' , 'h' , 'h' , 1 ),
91- ('9s' , 'helloworld' , 'helloworl' , 'helloworl' , 1 ),
92- ('10s' , 'helloworld' , 'helloworld' , 'helloworld' , 0 ),
93- ('11s' , 'helloworld' , 'helloworld\0 ' , 'helloworld\0 ' , 1 ),
94- ('20s' , 'helloworld' , 'helloworld' + 10 * '\0 ' , 'helloworld' + 10 * '\0 ' , 1 ),
95- ('b' , 7 , '\7 ' , '\7 ' , 0 ),
96- ('b' , - 7 , '\371 ' , '\371 ' , 0 ),
97- ('B' , 7 , '\7 ' , '\7 ' , 0 ),
98- ('B' , 249 , '\371 ' , '\371 ' , 0 ),
99- ('h' , 700 , '\002 \274 ' , '\274 \002 ' , 0 ),
100- ('h' , - 700 , '\375 D' , 'D\375 ' , 0 ),
101- ('H' , 700 , '\002 \274 ' , '\274 \002 ' , 0 ),
102- ('H' , 0x10000 - 700 , '\375 D' , 'D\375 ' , 0 ),
103- ('i' , 70000000 , '\004 ,\035 \200 ' , '\200 \035 ,\004 ' , 0 ),
104- ('i' , - 70000000 , '\373 \323 \342 \200 ' , '\200 \342 \323 \373 ' , 0 ),
105- ('I' , 70000000 , '\004 ,\035 \200 ' , '\200 \035 ,\004 ' , 0 ),
106- ('I' , 0x100000000 - 70000000 , '\373 \323 \342 \200 ' , '\200 \342 \323 \373 ' , 0 ),
107- ('l' , 70000000 , '\004 ,\035 \200 ' , '\200 \035 ,\004 ' , 0 ),
108- ('l' , - 70000000 , '\373 \323 \342 \200 ' , '\200 \342 \323 \373 ' , 0 ),
109- ('L' , 70000000 , '\004 ,\035 \200 ' , '\200 \035 ,\004 ' , 0 ),
110- ('L' , 0x100000000 - 70000000 , '\373 \323 \342 \200 ' , '\200 \342 \323 \373 ' , 0 ),
111- ('f' , 2.0 , '@\000 \000 \000 ' , '\000 \000 \000 @' , 0 ),
112- ('d' , 2.0 , '@\000 \000 \000 \000 \000 \000 \000 ' ,
113- '\000 \000 \000 \000 \000 \000 \000 @' , 0 ),
114- ('f' , - 2.0 , '\300 \000 \000 \000 ' , '\000 \000 \000 \300 ' , 0 ),
115- ('d' , - 2.0 , '\300 \000 \000 \000 \000 \000 \000 \000 ' ,
116- '\000 \000 \000 \000 \000 \000 \000 \300 ' , 0 ),
117- ('?' , 0 , '\0 ' , '\0 ' , 0 ),
118- ('?' , 3 , '\1 ' , '\1 ' , 1 ),
119- ('?' , True , '\1 ' , '\1 ' , 0 ),
120- ('?' , [], '\0 ' , '\0 ' , 1 ),
121- ('?' , (1 ,), '\1 ' , '\1 ' , 1 ),
85+ ('c' , b 'a' , b 'a' , b 'a' , 0 ),
86+ ('xc' , b 'a' , b '\0 a' , b '\0 a' , 0 ),
87+ ('cx' , b 'a' , b 'a\0 ' , b 'a\0 ' , 0 ),
88+ ('s' , b 'a' , b 'a' , b 'a' , 0 ),
89+ ('0s' , b 'helloworld' , b '' , b '' , 1 ),
90+ ('1s' , b 'helloworld' , b 'h' , b 'h' , 1 ),
91+ ('9s' , b 'helloworld' , b 'helloworl' , b 'helloworl' , 1 ),
92+ ('10s' , b 'helloworld' , b 'helloworld' , b 'helloworld' , 0 ),
93+ ('11s' , b 'helloworld' , b 'helloworld\0 ' , b 'helloworld\0 ' , 1 ),
94+ ('20s' , b 'helloworld' , b 'helloworld'+ 10 * b '\0 ' , b 'helloworld'+ 10 * b '\0 ' , 1 ),
95+ ('b' , 7 , b '\7 ' , b '\7 ' , 0 ),
96+ ('b' , - 7 , b '\371 ' , b '\371 ' , 0 ),
97+ ('B' , 7 , b '\7 ' , b '\7 ' , 0 ),
98+ ('B' , 249 , b '\371 ' , b '\371 ' , 0 ),
99+ ('h' , 700 , b '\002 \274 ' , b '\274 \002 ' , 0 ),
100+ ('h' , - 700 , b '\375 D' , b 'D\375 ' , 0 ),
101+ ('H' , 700 , b '\002 \274 ' , b '\274 \002 ' , 0 ),
102+ ('H' , 0x10000 - 700 , b '\375 D' , b 'D\375 ' , 0 ),
103+ ('i' , 70000000 , b '\004 ,\035 \200 ' , b '\200 \035 ,\004 ' , 0 ),
104+ ('i' , - 70000000 , b '\373 \323 \342 \200 ' , b '\200 \342 \323 \373 ' , 0 ),
105+ ('I' , 70000000 , b '\004 ,\035 \200 ' , b '\200 \035 ,\004 ' , 0 ),
106+ ('I' , 0x100000000 - 70000000 , b '\373 \323 \342 \200 ' , b '\200 \342 \323 \373 ' , 0 ),
107+ ('l' , 70000000 , b '\004 ,\035 \200 ' , b '\200 \035 ,\004 ' , 0 ),
108+ ('l' , - 70000000 , b '\373 \323 \342 \200 ' , b '\200 \342 \323 \373 ' , 0 ),
109+ ('L' , 70000000 , b '\004 ,\035 \200 ' , b '\200 \035 ,\004 ' , 0 ),
110+ ('L' , 0x100000000 - 70000000 , b '\373 \323 \342 \200 ' , b '\200 \342 \323 \373 ' , 0 ),
111+ ('f' , 2.0 , b '@\000 \000 \000 ' , b '\000 \000 \000 @' , 0 ),
112+ ('d' , 2.0 , b '@\000 \000 \000 \000 \000 \000 \000 ' ,
113+ b '\000 \000 \000 \000 \000 \000 \000 @' , 0 ),
114+ ('f' , - 2.0 , b '\300 \000 \000 \000 ' , b '\000 \000 \000 \300 ' , 0 ),
115+ ('d' , - 2.0 , b '\300 \000 \000 \000 \000 \000 \000 \000 ' ,
116+ b '\000 \000 \000 \000 \000 \000 \000 \300 ' , 0 ),
117+ ('?' , 0 , b '\0 ' , b '\0 ' , 0 ),
118+ ('?' , 3 , b '\1 ' , b '\1 ' , 1 ),
119+ ('?' , True , b '\1 ' , b '\1 ' , 0 ),
120+ ('?' , [], b '\0 ' , b '\0 ' , 1 ),
121+ ('?' , (1 ,), b '\1 ' , b '\1 ' , 1 ),
122122 ]
123123
124124 for fmt , arg , big , lil , asy in tests :
125- big = bytes (big , "latin-1" )
126- lil = bytes (lil , "latin-1" )
127125 for (xfmt , exp ) in [('>' + fmt , big ), ('!' + fmt , big ), ('<' + fmt , lil ),
128126 ('=' + fmt , ISBIGENDIAN and big or lil )]:
129127 res = struct .pack (xfmt , arg )
130128 self .assertEqual (res , exp )
131129 self .assertEqual (struct .calcsize (xfmt ), len (res ))
132130 rev = struct .unpack (xfmt , res )[0 ]
133- if isinstance (arg , str ):
134- # Strings are returned as bytes since you can't know the
135- # encoding of the string when packed.
136- arg = bytes (arg , 'latin1' )
137131 if rev != arg :
138132 self .assertTrue (asy )
139133
@@ -334,15 +328,14 @@ def __int__(self):
334328 def test_p_code (self ):
335329 # Test p ("Pascal string") code.
336330 for code , input , expected , expectedback in [
337- ('p' ,'abc' , '\x00 ' , b'' ),
338- ('1p' , 'abc' , '\x00 ' , b'' ),
339- ('2p' , 'abc' , '\x01 a' , b'a' ),
340- ('3p' , 'abc' , '\x02 ab' , b'ab' ),
341- ('4p' , 'abc' , '\x03 abc' , b'abc' ),
342- ('5p' , 'abc' , '\x03 abc\x00 ' , b'abc' ),
343- ('6p' , 'abc' , '\x03 abc\x00 \x00 ' , b'abc' ),
344- ('1000p' , 'x' * 1000 , '\xff ' + 'x' * 999 , b'x' * 255 )]:
345- expected = bytes (expected , "latin-1" )
331+ ('p' , b'abc' , b'\x00 ' , b'' ),
332+ ('1p' , b'abc' , b'\x00 ' , b'' ),
333+ ('2p' , b'abc' , b'\x01 a' , b'a' ),
334+ ('3p' , b'abc' , b'\x02 ab' , b'ab' ),
335+ ('4p' , b'abc' , b'\x03 abc' , b'abc' ),
336+ ('5p' , b'abc' , b'\x03 abc\x00 ' , b'abc' ),
337+ ('6p' , b'abc' , b'\x03 abc\x00 \x00 ' , b'abc' ),
338+ ('1000p' , b'x' * 1000 , b'\xff ' + b'x' * 999 , b'x' * 255 )]:
346339 got = struct .pack (code , input )
347340 self .assertEqual (got , expected )
348341 (got ,) = struct .unpack (code , got )
@@ -401,15 +394,11 @@ def test_unpack_from(self):
401394 s = struct .Struct (fmt )
402395 for cls in (bytes , bytearray ):
403396 data = cls (test_string )
404- if not isinstance (data , (bytes , bytearray )):
405- bytes_data = bytes (data , 'latin1' )
406- else :
407- bytes_data = data
408397 self .assertEqual (s .unpack_from (data ), (b'abcd' ,))
409398 self .assertEqual (s .unpack_from (data , 2 ), (b'cd01' ,))
410399 self .assertEqual (s .unpack_from (data , 4 ), (b'0123' ,))
411400 for i in range (6 ):
412- self .assertEqual (s .unpack_from (data , i ), (bytes_data [i :i + 4 ],))
401+ self .assertEqual (s .unpack_from (data , i ), (data [i :i + 4 ],))
413402 for i in range (6 , len (test_string ) + 1 ):
414403 self .assertRaises (struct .error , s .unpack_from , data , i )
415404 for cls in (bytes , bytearray ):
0 commit comments