Kaydet (Commit) 09549f44 authored tarafından Guido van Rossum's avatar Guido van Rossum

Changes in anticipation of stricter str vs. bytes enforcement.

üst 739e2ad6
...@@ -54,7 +54,7 @@ def b64encode(s, altchars=None): ...@@ -54,7 +54,7 @@ def b64encode(s, altchars=None):
encoded = binascii.b2a_base64(s)[:-1] encoded = binascii.b2a_base64(s)[:-1]
if altchars is not None: if altchars is not None:
if not isinstance(altchars, bytes): if not isinstance(altchars, bytes):
altchars = bytes(altchars) altchars = bytes(altchars, "ascii")
assert len(altchars) == 2, repr(altchars) assert len(altchars) == 2, repr(altchars)
return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]}) return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]})
return encoded return encoded
...@@ -75,7 +75,7 @@ def b64decode(s, altchars=None): ...@@ -75,7 +75,7 @@ def b64decode(s, altchars=None):
s = bytes(s) s = bytes(s)
if altchars is not None: if altchars is not None:
if not isinstance(altchars, bytes): if not isinstance(altchars, bytes):
altchars = bytes(altchars) altchars = bytes(altchars, "ascii")
assert len(altchars) == 2, repr(altchars) assert len(altchars) == 2, repr(altchars)
s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'}) s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'})
return binascii.a2b_base64(s) return binascii.a2b_base64(s)
...@@ -239,7 +239,7 @@ def b32decode(s, casefold=False, map01=None): ...@@ -239,7 +239,7 @@ def b32decode(s, casefold=False, map01=None):
acc = 0 acc = 0
shift = 35 shift = 35
# Process the last, partial quanta # Process the last, partial quanta
last = binascii.unhexlify(bytes('%010x' % acc)) last = binascii.unhexlify(bytes('%010x' % acc, "ascii"))
if padchars == 0: if padchars == 0:
last = b'' # No characters last = b'' # No characters
elif padchars == 1: elif padchars == 1:
...@@ -323,8 +323,7 @@ def decode(input, output): ...@@ -323,8 +323,7 @@ def decode(input, output):
def encodestring(s): def encodestring(s):
"""Encode a string into multiple lines of base-64 data.""" """Encode a string into multiple lines of base-64 data."""
if not isinstance(s, bytes): assert isinstance(s, bytes), repr(s)
s = bytes(s)
pieces = [] pieces = []
for i in range(0, len(s), MAXBINSIZE): for i in range(0, len(s), MAXBINSIZE):
chunk = s[i : i + MAXBINSIZE] chunk = s[i : i + MAXBINSIZE]
...@@ -334,8 +333,7 @@ def encodestring(s): ...@@ -334,8 +333,7 @@ def encodestring(s):
def decodestring(s): def decodestring(s):
"""Decode a string.""" """Decode a string."""
if not isinstance(s, bytes): assert isinstance(s, bytes), repr(s)
s = bytes(s)
return binascii.a2b_base64(s) return binascii.a2b_base64(s)
......
...@@ -191,8 +191,8 @@ class BinHex: ...@@ -191,8 +191,8 @@ class BinHex:
nl = len(name) nl = len(name)
if nl > 63: if nl > 63:
raise Error, 'Filename too long' raise Error, 'Filename too long'
d = bytes([nl]) + bytes(name) + b'\0' d = bytes([nl]) + name.encode("latin-1") + b'\0'
d2 = bytes(finfo.Type) + bytes(finfo.Creator) d2 = bytes(finfo.Type, "ascii") + bytes(finfo.Creator, "ascii")
# Force all structs to be packed with big-endian # Force all structs to be packed with big-endian
d3 = struct.pack('>h', finfo.Flags) d3 = struct.pack('>h', finfo.Flags)
......
...@@ -45,40 +45,44 @@ class BaseTest(unittest.TestCase): ...@@ -45,40 +45,44 @@ class BaseTest(unittest.TestCase):
else: else:
return obj return obj
# check that object.method(*args) returns result # check that obj.method(*args) returns result
def checkequal(self, result, object, methodname, *args): def checkequal(self, result, obj, methodname, *args):
result = self.fixtype(result) result = self.fixtype(result)
object = self.fixtype(object) obj = self.fixtype(obj)
args = self.fixtype(args) args = self.fixtype(args)
realresult = getattr(object, methodname)(*args) realresult = getattr(obj, methodname)(*args)
self.assertEqual( self.assertEqual(
result, result,
realresult realresult
) )
# if the original is returned make sure that # if the original is returned make sure that
# this doesn't happen with subclasses # this doesn't happen with subclasses
if object == realresult: if obj is realresult:
class subtype(self.__class__.type2test): try:
pass class subtype(self.__class__.type2test):
object = subtype(object) pass
realresult = getattr(object, methodname)(*args) except TypeError:
self.assert_(object is not realresult) pass # Skip this if we can't subclass
else:
# check that object.method(*args) raises exc obj = subtype(obj)
def checkraises(self, exc, object, methodname, *args): realresult = getattr(obj, methodname)(*args)
object = self.fixtype(object) self.assert_(obj is not realresult)
# check that obj.method(*args) raises exc
def checkraises(self, exc, obj, methodname, *args):
obj = self.fixtype(obj)
args = self.fixtype(args) args = self.fixtype(args)
self.assertRaises( self.assertRaises(
exc, exc,
getattr(object, methodname), getattr(obj, methodname),
*args *args
) )
# call object.method(*args) without any checks # call obj.method(*args) without any checks
def checkcall(self, object, methodname, *args): def checkcall(self, obj, methodname, *args):
object = self.fixtype(object) obj = self.fixtype(obj)
args = self.fixtype(args) args = self.fixtype(args)
getattr(object, methodname)(*args) getattr(obj, methodname)(*args)
def test_count(self): def test_count(self):
self.checkequal(3, 'aaa', 'count', 'a') self.checkequal(3, 'aaa', 'count', 'a')
...@@ -118,14 +122,14 @@ class BaseTest(unittest.TestCase): ...@@ -118,14 +122,14 @@ class BaseTest(unittest.TestCase):
i, m = divmod(i, base) i, m = divmod(i, base)
entry.append(charset[m]) entry.append(charset[m])
teststrings.add(''.join(entry)) teststrings.add(''.join(entry))
teststrings = list(teststrings) teststrings = [self.fixtype(ts) for ts in teststrings]
for i in teststrings: for i in teststrings:
i = self.fixtype(i)
n = len(i) n = len(i)
for j in teststrings: for j in teststrings:
r1 = i.count(j) r1 = i.count(j)
if j: if j:
r2, rem = divmod(n - len(i.replace(j, '')), len(j)) r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
len(j))
else: else:
r2, rem = len(i)+1, 0 r2, rem = len(i)+1, 0
if rem or r1 != r2: if rem or r1 != r2:
...@@ -157,9 +161,8 @@ class BaseTest(unittest.TestCase): ...@@ -157,9 +161,8 @@ class BaseTest(unittest.TestCase):
i, m = divmod(i, base) i, m = divmod(i, base)
entry.append(charset[m]) entry.append(charset[m])
teststrings.add(''.join(entry)) teststrings.add(''.join(entry))
teststrings = list(teststrings) teststrings = [self.fixtype(ts) for ts in teststrings]
for i in teststrings: for i in teststrings:
i = self.fixtype(i)
for j in teststrings: for j in teststrings:
loc = i.find(j) loc = i.find(j)
r1 = (loc != -1) r1 = (loc != -1)
......
...@@ -528,9 +528,9 @@ class BytesTest(unittest.TestCase): ...@@ -528,9 +528,9 @@ class BytesTest(unittest.TestCase):
def test_count(self): def test_count(self):
b = b'mississippi' b = b'mississippi'
self.assertEqual(b.count('i'), 4) self.assertEqual(b.count(b'i'), 4)
self.assertEqual(b.count('ss'), 2) self.assertEqual(b.count(b'ss'), 2)
self.assertEqual(b.count('w'), 0) self.assertEqual(b.count(b'w'), 0)
def test_append(self): def test_append(self):
b = b'hell' b = b'hell'
...@@ -551,58 +551,58 @@ class BytesTest(unittest.TestCase): ...@@ -551,58 +551,58 @@ class BytesTest(unittest.TestCase):
def test_startswith(self): def test_startswith(self):
b = b'hello' b = b'hello'
self.assertFalse(bytes().startswith("anything")) self.assertFalse(bytes().startswith(b"anything"))
self.assertTrue(b.startswith("hello")) self.assertTrue(b.startswith(b"hello"))
self.assertTrue(b.startswith("hel")) self.assertTrue(b.startswith(b"hel"))
self.assertTrue(b.startswith("h")) self.assertTrue(b.startswith(b"h"))
self.assertFalse(b.startswith("hellow")) self.assertFalse(b.startswith(b"hellow"))
self.assertFalse(b.startswith("ha")) self.assertFalse(b.startswith(b"ha"))
def test_endswith(self): def test_endswith(self):
b = b'hello' b = b'hello'
self.assertFalse(bytes().endswith("anything")) self.assertFalse(bytes().endswith(b"anything"))
self.assertTrue(b.endswith("hello")) self.assertTrue(b.endswith(b"hello"))
self.assertTrue(b.endswith("llo")) self.assertTrue(b.endswith(b"llo"))
self.assertTrue(b.endswith("o")) self.assertTrue(b.endswith(b"o"))
self.assertFalse(b.endswith("whello")) self.assertFalse(b.endswith(b"whello"))
self.assertFalse(b.endswith("no")) self.assertFalse(b.endswith(b"no"))
def test_find(self): def test_find(self):
b = b'mississippi' b = b'mississippi'
self.assertEqual(b.find('ss'), 2) self.assertEqual(b.find(b'ss'), 2)
self.assertEqual(b.find('ss', 3), 5) self.assertEqual(b.find(b'ss', 3), 5)
self.assertEqual(b.find('ss', 1, 7), 2) self.assertEqual(b.find(b'ss', 1, 7), 2)
self.assertEqual(b.find('ss', 1, 3), -1) self.assertEqual(b.find(b'ss', 1, 3), -1)
self.assertEqual(b.find('w'), -1) self.assertEqual(b.find(b'w'), -1)
self.assertEqual(b.find('mississippian'), -1) self.assertEqual(b.find(b'mississippian'), -1)
def test_rfind(self): def test_rfind(self):
b = b'mississippi' b = b'mississippi'
self.assertEqual(b.rfind('ss'), 5) self.assertEqual(b.rfind(b'ss'), 5)
self.assertEqual(b.rfind('ss', 3), 5) self.assertEqual(b.rfind(b'ss', 3), 5)
self.assertEqual(b.rfind('ss', 0, 6), 2) self.assertEqual(b.rfind(b'ss', 0, 6), 2)
self.assertEqual(b.rfind('w'), -1) self.assertEqual(b.rfind(b'w'), -1)
self.assertEqual(b.rfind('mississippian'), -1) self.assertEqual(b.rfind(b'mississippian'), -1)
def test_index(self): def test_index(self):
b = b'world' b = b'world'
self.assertEqual(b.index('w'), 0) self.assertEqual(b.index(b'w'), 0)
self.assertEqual(b.index('orl'), 1) self.assertEqual(b.index(b'orl'), 1)
self.assertRaises(ValueError, lambda: b.index('worm')) self.assertRaises(ValueError, b.index, b'worm')
self.assertRaises(ValueError, lambda: b.index('ldo')) self.assertRaises(ValueError, b.index, b'ldo')
def test_rindex(self): def test_rindex(self):
# XXX could be more rigorous # XXX could be more rigorous
b = b'world' b = b'world'
self.assertEqual(b.rindex('w'), 0) self.assertEqual(b.rindex(b'w'), 0)
self.assertEqual(b.rindex('orl'), 1) self.assertEqual(b.rindex(b'orl'), 1)
self.assertRaises(ValueError, lambda: b.rindex('worm')) self.assertRaises(ValueError, b.rindex, b'worm')
self.assertRaises(ValueError, lambda: b.rindex('ldo')) self.assertRaises(ValueError, b.rindex, b'ldo')
def test_replace(self): def test_replace(self):
b = b'mississippi' b = b'mississippi'
self.assertEqual(b.replace('i', 'a'), b'massassappa') self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
self.assertEqual(b.replace('ss', 'x'), b'mixixippi') self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
def test_translate(self): def test_translate(self):
b = b'hello' b = b'hello'
...@@ -614,19 +614,19 @@ class BytesTest(unittest.TestCase): ...@@ -614,19 +614,19 @@ class BytesTest(unittest.TestCase):
def test_split(self): def test_split(self):
b = b'mississippi' b = b'mississippi'
self.assertEqual(b.split('i'), [b'm', b'ss', b'ss', b'pp', b'']) self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
self.assertEqual(b.split('ss'), [b'mi', b'i', b'ippi']) self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
self.assertEqual(b.split('w'), [b]) self.assertEqual(b.split(b'w'), [b])
# require an arg (no magic whitespace split) # require an arg (no magic whitespace split)
self.assertRaises(TypeError, lambda: b.split()) self.assertRaises(TypeError, b.split)
def test_rsplit(self): def test_rsplit(self):
b = b'mississippi' b = b'mississippi'
self.assertEqual(b.rsplit('i'), [b'm', b'ss', b'ss', b'pp', b'']) self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
self.assertEqual(b.rsplit('ss'), [b'mi', b'i', b'ippi']) self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
self.assertEqual(b.rsplit('w'), [b]) self.assertEqual(b.rsplit(b'w'), [b])
# require an arg (no magic whitespace split) # require an arg (no magic whitespace split)
self.assertRaises(TypeError, lambda: b.rsplit()) self.assertRaises(TypeError, b.rsplit)
def test_partition(self): def test_partition(self):
b = b'mississippi' b = b'mississippi'
...@@ -695,30 +695,11 @@ class BytesAsStringTest(test.string_tests.BaseTest): ...@@ -695,30 +695,11 @@ class BytesAsStringTest(test.string_tests.BaseTest):
return obj.encode("utf-8") return obj.encode("utf-8")
return super().fixtype(obj) return super().fixtype(obj)
def checkequal(self, result, object, methodname, *args):
object = bytes(object, "utf-8")
realresult = getattr(bytes, methodname)(object, *args)
self.assertEqual(
self.fixtype(result),
realresult
)
def checkraises(self, exc, object, methodname, *args):
object = bytes(object, "utf-8")
self.assertRaises(
exc,
getattr(bytes, methodname),
object,
*args
)
# Currently the bytes containment testing uses a single integer # Currently the bytes containment testing uses a single integer
# value. This may not be the final design, but until then the # value. This may not be the final design, but until then the
# bytes section with in a bytes containment not valid # bytes section with in a bytes containment not valid
def test_contains(self): def test_contains(self):
pass pass
def test_find(self):
pass
def test_expandtabs(self): def test_expandtabs(self):
pass pass
def test_upper(self): def test_upper(self):
......
...@@ -232,7 +232,7 @@ class CgiTests(unittest.TestCase): ...@@ -232,7 +232,7 @@ class CgiTests(unittest.TestCase):
return a return a
f = TestReadlineFile(tempfile.TemporaryFile()) f = TestReadlineFile(tempfile.TemporaryFile())
f.write('x' * 256 * 1024) f.write(b'x' * 256 * 1024)
f.seek(0) f.seek(0)
env = {'REQUEST_METHOD':'PUT'} env = {'REQUEST_METHOD':'PUT'}
fs = cgi.FieldStorage(fp=f, environ=env) fs = cgi.FieldStorage(fp=f, environ=env)
......
...@@ -675,22 +675,22 @@ class CodecCallbackTest(unittest.TestCase): ...@@ -675,22 +675,22 @@ class CodecCallbackTest(unittest.TestCase):
# enhance coverage of: # enhance coverage of:
# Objects/unicodeobject.c::unicode_decode_call_errorhandler() # Objects/unicodeobject.c::unicode_decode_call_errorhandler()
# and callers # and callers
self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown") self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown")
def baddecodereturn1(exc): def baddecodereturn1(exc):
return 42 return 42
codecs.register_error("test.baddecodereturn1", baddecodereturn1) codecs.register_error("test.baddecodereturn1", baddecodereturn1)
self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1") self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1")
self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1") self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1")
self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1") self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1")
self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1") self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1") self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1") self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
def baddecodereturn2(exc): def baddecodereturn2(exc):
return ("?", None) return ("?", None)
codecs.register_error("test.baddecodereturn2", baddecodereturn2) codecs.register_error("test.baddecodereturn2", baddecodereturn2)
self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2") self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2")
handler = PosReturn() handler = PosReturn()
codecs.register_error("test.posreturn", handler.handle) codecs.register_error("test.posreturn", handler.handle)
......
...@@ -559,7 +559,7 @@ class ReadBufferTest(unittest.TestCase): ...@@ -559,7 +559,7 @@ class ReadBufferTest(unittest.TestCase):
def test_array(self): def test_array(self):
import array import array
self.assertEqual( self.assertEqual(
codecs.readbuffer_encode(array.array("b", bytes("spam"))), codecs.readbuffer_encode(array.array("b", b"spam")),
(b"spam", 4) (b"spam", 4)
) )
...@@ -573,10 +573,10 @@ class ReadBufferTest(unittest.TestCase): ...@@ -573,10 +573,10 @@ class ReadBufferTest(unittest.TestCase):
class CharBufferTest(unittest.TestCase): class CharBufferTest(unittest.TestCase):
def test_string(self): def test_string(self):
self.assertEqual(codecs.charbuffer_encode("spam"), (b"spam", 4)) self.assertEqual(codecs.charbuffer_encode(b"spam"), (b"spam", 4))
def test_empty(self): def test_empty(self):
self.assertEqual(codecs.charbuffer_encode(""), (b"", 0)) self.assertEqual(codecs.charbuffer_encode(b""), (b"", 0))
def test_bad_args(self): def test_bad_args(self):
self.assertRaises(TypeError, codecs.charbuffer_encode) self.assertRaises(TypeError, codecs.charbuffer_encode)
...@@ -999,19 +999,19 @@ class IDNACodecTest(unittest.TestCase): ...@@ -999,19 +999,19 @@ class IDNACodecTest(unittest.TestCase):
def test_incremental_decode(self): def test_incremental_decode(self):
self.assertEquals( self.assertEquals(
"".join(codecs.iterdecode((bytes(chr(c)) for c in b"python.org"), "idna")), "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")),
"python.org" "python.org"
) )
self.assertEquals( self.assertEquals(
"".join(codecs.iterdecode((bytes(chr(c)) for c in b"python.org."), "idna")), "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")),
"python.org." "python.org."
) )
self.assertEquals( self.assertEquals(
"".join(codecs.iterdecode((bytes(chr(c)) for c in b"xn--pythn-mua.org."), "idna")), "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")),
"pyth\xf6n.org." "pyth\xf6n.org."
) )
self.assertEquals( self.assertEquals(
"".join(codecs.iterdecode((bytes(chr(c)) for c in b"xn--pythn-mua.org."), "idna")), "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")),
"pyth\xf6n.org." "pyth\xf6n.org."
) )
......
...@@ -318,11 +318,11 @@ class ComplexTest(unittest.TestCase): ...@@ -318,11 +318,11 @@ class ComplexTest(unittest.TestCase):
fo = None fo = None
try: try:
fo = open(test_support.TESTFN, "wb") fo = open(test_support.TESTFN, "w")
print(a, b, file=fo) print(a, b, file=fo)
fo.close() fo.close()
fo = open(test_support.TESTFN, "rb") fo = open(test_support.TESTFN, "r")
self.assertEqual(fo.read(), ("%s %s\n" % (a, b)).encode("ascii")) self.assertEqual(fo.read(), ("%s %s\n" % (a, b)))
finally: finally:
if (fo is not None) and (not fo.closed): if (fo is not None) and (not fo.closed):
fo.close() fo.close()
......
...@@ -459,7 +459,7 @@ class Wave_write: ...@@ -459,7 +459,7 @@ class Wave_write:
self._write_header(datasize) self._write_header(datasize)
def _write_header(self, initlength): def _write_header(self, initlength):
self._file.write('RIFF') self._file.write(b'RIFF')
if not self._nframes: if not self._nframes:
self._nframes = initlength / (self._nchannels * self._sampwidth) self._nframes = initlength / (self._nchannels * self._sampwidth)
self._datalength = self._nframes * self._nchannels * self._sampwidth self._datalength = self._nframes * self._nchannels * self._sampwidth
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment