Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09549f4

Browse files
committed
Changes in anticipation of stricter str vs. bytes enforcement.
1 parent 739e2ad commit 09549f4

9 files changed

Lines changed: 98 additions & 116 deletions

File tree

Lib/base64.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def b64encode(s, altchars=None):
5454
encoded = binascii.b2a_base64(s)[:-1]
5555
if altchars is not None:
5656
if not isinstance(altchars, bytes):
57-
altchars = bytes(altchars)
57+
altchars = bytes(altchars, "ascii")
5858
assert len(altchars) == 2, repr(altchars)
5959
return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]})
6060
return encoded
@@ -75,7 +75,7 @@ def b64decode(s, altchars=None):
7575
s = bytes(s)
7676
if altchars is not None:
7777
if not isinstance(altchars, bytes):
78-
altchars = bytes(altchars)
78+
altchars = bytes(altchars, "ascii")
7979
assert len(altchars) == 2, repr(altchars)
8080
s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'})
8181
return binascii.a2b_base64(s)
@@ -239,7 +239,7 @@ def b32decode(s, casefold=False, map01=None):
239239
acc = 0
240240
shift = 35
241241
# Process the last, partial quanta
242-
last = binascii.unhexlify(bytes('%010x' % acc))
242+
last = binascii.unhexlify(bytes('%010x' % acc, "ascii"))
243243
if padchars == 0:
244244
last = b'' # No characters
245245
elif padchars == 1:
@@ -323,8 +323,7 @@ def decode(input, output):
323323

324324
def encodestring(s):
325325
"""Encode a string into multiple lines of base-64 data."""
326-
if not isinstance(s, bytes):
327-
s = bytes(s)
326+
assert isinstance(s, bytes), repr(s)
328327
pieces = []
329328
for i in range(0, len(s), MAXBINSIZE):
330329
chunk = s[i : i + MAXBINSIZE]
@@ -334,8 +333,7 @@ def encodestring(s):
334333

335334
def decodestring(s):
336335
"""Decode a string."""
337-
if not isinstance(s, bytes):
338-
s = bytes(s)
336+
assert isinstance(s, bytes), repr(s)
339337
return binascii.a2b_base64(s)
340338

341339

Lib/binhex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def _writeinfo(self, name, finfo):
191191
nl = len(name)
192192
if nl > 63:
193193
raise Error, 'Filename too long'
194-
d = bytes([nl]) + bytes(name) + b'\0'
195-
d2 = bytes(finfo.Type) + bytes(finfo.Creator)
194+
d = bytes([nl]) + name.encode("latin-1") + b'\0'
195+
d2 = bytes(finfo.Type, "ascii") + bytes(finfo.Creator, "ascii")
196196

197197
# Force all structs to be packed with big-endian
198198
d3 = struct.pack('>h', finfo.Flags)

Lib/test/string_tests.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,44 @@ def fixtype(self, obj):
4545
else:
4646
return obj
4747

48-
# check that object.method(*args) returns result
49-
def checkequal(self, result, object, methodname, *args):
48+
# check that obj.method(*args) returns result
49+
def checkequal(self, result, obj, methodname, *args):
5050
result = self.fixtype(result)
51-
object = self.fixtype(object)
51+
obj = self.fixtype(obj)
5252
args = self.fixtype(args)
53-
realresult = getattr(object, methodname)(*args)
53+
realresult = getattr(obj, methodname)(*args)
5454
self.assertEqual(
5555
result,
5656
realresult
5757
)
5858
# if the original is returned make sure that
5959
# this doesn't happen with subclasses
60-
if object == realresult:
61-
class subtype(self.__class__.type2test):
62-
pass
63-
object = subtype(object)
64-
realresult = getattr(object, methodname)(*args)
65-
self.assert_(object is not realresult)
66-
67-
# check that object.method(*args) raises exc
68-
def checkraises(self, exc, object, methodname, *args):
69-
object = self.fixtype(object)
60+
if obj is realresult:
61+
try:
62+
class subtype(self.__class__.type2test):
63+
pass
64+
except TypeError:
65+
pass # Skip this if we can't subclass
66+
else:
67+
obj = subtype(obj)
68+
realresult = getattr(obj, methodname)(*args)
69+
self.assert_(obj is not realresult)
70+
71+
# check that obj.method(*args) raises exc
72+
def checkraises(self, exc, obj, methodname, *args):
73+
obj = self.fixtype(obj)
7074
args = self.fixtype(args)
7175
self.assertRaises(
7276
exc,
73-
getattr(object, methodname),
77+
getattr(obj, methodname),
7478
*args
7579
)
7680

77-
# call object.method(*args) without any checks
78-
def checkcall(self, object, methodname, *args):
79-
object = self.fixtype(object)
81+
# call obj.method(*args) without any checks
82+
def checkcall(self, obj, methodname, *args):
83+
obj = self.fixtype(obj)
8084
args = self.fixtype(args)
81-
getattr(object, methodname)(*args)
85+
getattr(obj, methodname)(*args)
8286

8387
def test_count(self):
8488
self.checkequal(3, 'aaa', 'count', 'a')
@@ -118,14 +122,14 @@ def test_count(self):
118122
i, m = divmod(i, base)
119123
entry.append(charset[m])
120124
teststrings.add(''.join(entry))
121-
teststrings = list(teststrings)
125+
teststrings = [self.fixtype(ts) for ts in teststrings]
122126
for i in teststrings:
123-
i = self.fixtype(i)
124127
n = len(i)
125128
for j in teststrings:
126129
r1 = i.count(j)
127130
if j:
128-
r2, rem = divmod(n - len(i.replace(j, '')), len(j))
131+
r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
132+
len(j))
129133
else:
130134
r2, rem = len(i)+1, 0
131135
if rem or r1 != r2:
@@ -157,9 +161,8 @@ def test_find(self):
157161
i, m = divmod(i, base)
158162
entry.append(charset[m])
159163
teststrings.add(''.join(entry))
160-
teststrings = list(teststrings)
164+
teststrings = [self.fixtype(ts) for ts in teststrings]
161165
for i in teststrings:
162-
i = self.fixtype(i)
163166
for j in teststrings:
164167
loc = i.find(j)
165168
r1 = (loc != -1)

Lib/test/test_bytes.py

Lines changed: 44 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,9 @@ def test_index(self):
528528

529529
def test_count(self):
530530
b = b'mississippi'
531-
self.assertEqual(b.count('i'), 4)
532-
self.assertEqual(b.count('ss'), 2)
533-
self.assertEqual(b.count('w'), 0)
531+
self.assertEqual(b.count(b'i'), 4)
532+
self.assertEqual(b.count(b'ss'), 2)
533+
self.assertEqual(b.count(b'w'), 0)
534534

535535
def test_append(self):
536536
b = b'hell'
@@ -551,58 +551,58 @@ def test_insert(self):
551551

552552
def test_startswith(self):
553553
b = b'hello'
554-
self.assertFalse(bytes().startswith("anything"))
555-
self.assertTrue(b.startswith("hello"))
556-
self.assertTrue(b.startswith("hel"))
557-
self.assertTrue(b.startswith("h"))
558-
self.assertFalse(b.startswith("hellow"))
559-
self.assertFalse(b.startswith("ha"))
554+
self.assertFalse(bytes().startswith(b"anything"))
555+
self.assertTrue(b.startswith(b"hello"))
556+
self.assertTrue(b.startswith(b"hel"))
557+
self.assertTrue(b.startswith(b"h"))
558+
self.assertFalse(b.startswith(b"hellow"))
559+
self.assertFalse(b.startswith(b"ha"))
560560

561561
def test_endswith(self):
562562
b = b'hello'
563-
self.assertFalse(bytes().endswith("anything"))
564-
self.assertTrue(b.endswith("hello"))
565-
self.assertTrue(b.endswith("llo"))
566-
self.assertTrue(b.endswith("o"))
567-
self.assertFalse(b.endswith("whello"))
568-
self.assertFalse(b.endswith("no"))
563+
self.assertFalse(bytes().endswith(b"anything"))
564+
self.assertTrue(b.endswith(b"hello"))
565+
self.assertTrue(b.endswith(b"llo"))
566+
self.assertTrue(b.endswith(b"o"))
567+
self.assertFalse(b.endswith(b"whello"))
568+
self.assertFalse(b.endswith(b"no"))
569569

570570
def test_find(self):
571571
b = b'mississippi'
572-
self.assertEqual(b.find('ss'), 2)
573-
self.assertEqual(b.find('ss', 3), 5)
574-
self.assertEqual(b.find('ss', 1, 7), 2)
575-
self.assertEqual(b.find('ss', 1, 3), -1)
576-
self.assertEqual(b.find('w'), -1)
577-
self.assertEqual(b.find('mississippian'), -1)
572+
self.assertEqual(b.find(b'ss'), 2)
573+
self.assertEqual(b.find(b'ss', 3), 5)
574+
self.assertEqual(b.find(b'ss', 1, 7), 2)
575+
self.assertEqual(b.find(b'ss', 1, 3), -1)
576+
self.assertEqual(b.find(b'w'), -1)
577+
self.assertEqual(b.find(b'mississippian'), -1)
578578

579579
def test_rfind(self):
580580
b = b'mississippi'
581-
self.assertEqual(b.rfind('ss'), 5)
582-
self.assertEqual(b.rfind('ss', 3), 5)
583-
self.assertEqual(b.rfind('ss', 0, 6), 2)
584-
self.assertEqual(b.rfind('w'), -1)
585-
self.assertEqual(b.rfind('mississippian'), -1)
581+
self.assertEqual(b.rfind(b'ss'), 5)
582+
self.assertEqual(b.rfind(b'ss', 3), 5)
583+
self.assertEqual(b.rfind(b'ss', 0, 6), 2)
584+
self.assertEqual(b.rfind(b'w'), -1)
585+
self.assertEqual(b.rfind(b'mississippian'), -1)
586586

587587
def test_index(self):
588588
b = b'world'
589-
self.assertEqual(b.index('w'), 0)
590-
self.assertEqual(b.index('orl'), 1)
591-
self.assertRaises(ValueError, lambda: b.index('worm'))
592-
self.assertRaises(ValueError, lambda: b.index('ldo'))
589+
self.assertEqual(b.index(b'w'), 0)
590+
self.assertEqual(b.index(b'orl'), 1)
591+
self.assertRaises(ValueError, b.index, b'worm')
592+
self.assertRaises(ValueError, b.index, b'ldo')
593593

594594
def test_rindex(self):
595595
# XXX could be more rigorous
596596
b = b'world'
597-
self.assertEqual(b.rindex('w'), 0)
598-
self.assertEqual(b.rindex('orl'), 1)
599-
self.assertRaises(ValueError, lambda: b.rindex('worm'))
600-
self.assertRaises(ValueError, lambda: b.rindex('ldo'))
597+
self.assertEqual(b.rindex(b'w'), 0)
598+
self.assertEqual(b.rindex(b'orl'), 1)
599+
self.assertRaises(ValueError, b.rindex, b'worm')
600+
self.assertRaises(ValueError, b.rindex, b'ldo')
601601

602602
def test_replace(self):
603603
b = b'mississippi'
604-
self.assertEqual(b.replace('i', 'a'), b'massassappa')
605-
self.assertEqual(b.replace('ss', 'x'), b'mixixippi')
604+
self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
605+
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
606606

607607
def test_translate(self):
608608
b = b'hello'
@@ -614,19 +614,19 @@ def test_translate(self):
614614

615615
def test_split(self):
616616
b = b'mississippi'
617-
self.assertEqual(b.split('i'), [b'm', b'ss', b'ss', b'pp', b''])
618-
self.assertEqual(b.split('ss'), [b'mi', b'i', b'ippi'])
619-
self.assertEqual(b.split('w'), [b])
617+
self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
618+
self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
619+
self.assertEqual(b.split(b'w'), [b])
620620
# require an arg (no magic whitespace split)
621-
self.assertRaises(TypeError, lambda: b.split())
621+
self.assertRaises(TypeError, b.split)
622622

623623
def test_rsplit(self):
624624
b = b'mississippi'
625-
self.assertEqual(b.rsplit('i'), [b'm', b'ss', b'ss', b'pp', b''])
626-
self.assertEqual(b.rsplit('ss'), [b'mi', b'i', b'ippi'])
627-
self.assertEqual(b.rsplit('w'), [b])
625+
self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
626+
self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
627+
self.assertEqual(b.rsplit(b'w'), [b])
628628
# require an arg (no magic whitespace split)
629-
self.assertRaises(TypeError, lambda: b.rsplit())
629+
self.assertRaises(TypeError, b.rsplit)
630630

631631
def test_partition(self):
632632
b = b'mississippi'
@@ -695,30 +695,11 @@ def fixtype(self, obj):
695695
return obj.encode("utf-8")
696696
return super().fixtype(obj)
697697

698-
def checkequal(self, result, object, methodname, *args):
699-
object = bytes(object, "utf-8")
700-
realresult = getattr(bytes, methodname)(object, *args)
701-
self.assertEqual(
702-
self.fixtype(result),
703-
realresult
704-
)
705-
706-
def checkraises(self, exc, object, methodname, *args):
707-
object = bytes(object, "utf-8")
708-
self.assertRaises(
709-
exc,
710-
getattr(bytes, methodname),
711-
object,
712-
*args
713-
)
714-
715698
# Currently the bytes containment testing uses a single integer
716699
# value. This may not be the final design, but until then the
717700
# bytes section with in a bytes containment not valid
718701
def test_contains(self):
719702
pass
720-
def test_find(self):
721-
pass
722703
def test_expandtabs(self):
723704
pass
724705
def test_upper(self):

Lib/test/test_cgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def __getattr__(self, name):
232232
return a
233233

234234
f = TestReadlineFile(tempfile.TemporaryFile())
235-
f.write('x' * 256 * 1024)
235+
f.write(b'x' * 256 * 1024)
236236
f.seek(0)
237237
env = {'REQUEST_METHOD':'PUT'}
238238
fs = cgi.FieldStorage(fp=f, environ=env)

Lib/test/test_codeccallbacks.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -675,22 +675,22 @@ def test_decodehelper(self):
675675
# enhance coverage of:
676676
# Objects/unicodeobject.c::unicode_decode_call_errorhandler()
677677
# and callers
678-
self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown")
678+
self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown")
679679

680680
def baddecodereturn1(exc):
681681
return 42
682682
codecs.register_error("test.baddecodereturn1", baddecodereturn1)
683-
self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1")
684-
self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1")
685-
self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1")
686-
self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
687-
self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
688-
self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
683+
self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1")
684+
self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1")
685+
self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1")
686+
self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
687+
self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
688+
self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
689689

690690
def baddecodereturn2(exc):
691691
return ("?", None)
692692
codecs.register_error("test.baddecodereturn2", baddecodereturn2)
693-
self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2")
693+
self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2")
694694

695695
handler = PosReturn()
696696
codecs.register_error("test.posreturn", handler.handle)

0 commit comments

Comments
 (0)