Commit 39478e85 authored by Guido van Rossum's avatar Guido van Rossum

Changes in anticipation of stricter str vs. bytes enforcement.

parent 85825dc1
...@@ -76,11 +76,13 @@ class ObjectsTestCase(unittest.TestCase): ...@@ -76,11 +76,13 @@ class ObjectsTestCase(unittest.TestCase):
x = X() x = X()
x.a = s1 x.a = s1
x.b = s2 x.b = s2
self.failUnlessEqual(x._objects, {"0": bytes(s1), "1": bytes(s2)}) self.failUnlessEqual(x._objects, {"0": bytes(s1, "ascii"),
"1": bytes(s2, "ascii")})
y = Y() y = Y()
y.x = x y.x = x
self.failUnlessEqual(y._objects, {"0": {"0": bytes(s1), "1": bytes(s2)}}) self.failUnlessEqual(y._objects, {"0": {"0": bytes(s1, "ascii"),
"1": bytes(s2, "ascii")}})
## x = y.x ## x = y.x
## del y ## del y
## print x._b_base_._objects ## print x._b_base_._objects
......
...@@ -37,10 +37,7 @@ class HMAC: ...@@ -37,10 +37,7 @@ class HMAC:
if key is _secret_backdoor_key: # cheap if key is _secret_backdoor_key: # cheap
return return
if not isinstance(key, bytes): assert isinstance(key, bytes), repr(key)
if hasattr(key, "__index__"):
raise TypeError("key can't be a number")
key = bytes(key)
if digestmod is None: if digestmod is None:
import hashlib import hashlib
...@@ -71,10 +68,7 @@ class HMAC: ...@@ -71,10 +68,7 @@ class HMAC:
def update(self, msg): def update(self, msg):
"""Update this hashing object with the string msg. """Update this hashing object with the string msg.
""" """
if not isinstance(msg, bytes): assert isinstance(msg, bytes), repr(msg)
if hasattr(msg, "__index__"):
raise TypeError("msg can't be a number")
msg = bytes(msg)
self.inner.update(msg) self.inner.update(msg)
def copy(self): def copy(self):
......
...@@ -158,7 +158,7 @@ def decode(input, output, encoding): ...@@ -158,7 +158,7 @@ def decode(input, output, encoding):
import uu import uu
return uu.decode(input, output) return uu.decode(input, output)
if encoding in ('7bit', '8bit'): if encoding in ('7bit', '8bit'):
return output.write(input.read().decode("Latin-1")) return output.write(input.read())
if encoding in decodetab: if encoding in decodetab:
pipethrough(input, decodetab[encoding], output) pipethrough(input, decodetab[encoding], output)
else: else:
......
...@@ -248,7 +248,7 @@ class Pickler: ...@@ -248,7 +248,7 @@ class Pickler:
else: else:
return LONG_BINPUT + pack("<i", i) return LONG_BINPUT + pack("<i", i)
return PUT + bytes(repr(i)) + b'\n' return PUT + repr(i).encode("ascii") + b'\n'
# Return a GET (BINGET, LONG_BINGET) opcode string, with argument i. # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i.
def get(self, i, pack=struct.pack): def get(self, i, pack=struct.pack):
...@@ -258,7 +258,7 @@ class Pickler: ...@@ -258,7 +258,7 @@ class Pickler:
else: else:
return LONG_BINGET + pack("<i", i) return LONG_BINGET + pack("<i", i)
return GET + bytes(repr(i)) + b'\n' return GET + repr(i).encode("ascii") + b'\n'
def save(self, obj): def save(self, obj):
# Check for persistent id (defined by a subclass) # Check for persistent id (defined by a subclass)
...@@ -334,7 +334,7 @@ class Pickler: ...@@ -334,7 +334,7 @@ class Pickler:
self.save(pid) self.save(pid)
self.write(BINPERSID) self.write(BINPERSID)
else: else:
self.write(PERSID + bytes(str(pid)) + b'\n') self.write(PERSID + str(pid).encode("ascii") + b'\n')
def save_reduce(self, func, args, state=None, def save_reduce(self, func, args, state=None,
listitems=None, dictitems=None, obj=None): listitems=None, dictitems=None, obj=None):
...@@ -449,7 +449,7 @@ class Pickler: ...@@ -449,7 +449,7 @@ class Pickler:
self.write(BININT + pack("<i", obj)) self.write(BININT + pack("<i", obj))
return return
# Text pickle, or int too big to fit in signed 4-byte format. # Text pickle, or int too big to fit in signed 4-byte format.
self.write(INT + bytes(repr(obj)) + b'\n') self.write(INT + repr(obj).encode("ascii") + b'\n')
# XXX save_int is merged into save_long # XXX save_int is merged into save_long
# dispatch[int] = save_int # dispatch[int] = save_int
...@@ -481,14 +481,14 @@ class Pickler: ...@@ -481,14 +481,14 @@ class Pickler:
else: else:
self.write(LONG4 + pack("<i", n) + encoded) self.write(LONG4 + pack("<i", n) + encoded)
return return
self.write(LONG + bytes(repr(obj)) + b'\n') self.write(LONG + repr(obj).encode("ascii") + b'\n')
dispatch[int] = save_long dispatch[int] = save_long
def save_float(self, obj, pack=struct.pack): def save_float(self, obj, pack=struct.pack):
if self.bin: if self.bin:
self.write(BINFLOAT + pack('>d', obj)) self.write(BINFLOAT + pack('>d', obj))
else: else:
self.write(FLOAT + bytes(repr(obj)) + b'\n') self.write(FLOAT + repr(obj).encode("ascii") + b'\n')
dispatch[float] = save_float dispatch[float] = save_float
def save_string(self, obj, pack=struct.pack): def save_string(self, obj, pack=struct.pack):
...@@ -500,7 +500,7 @@ class Pickler: ...@@ -500,7 +500,7 @@ class Pickler:
self.write(BINSTRING + pack("<i", n) + bytes(obj)) self.write(BINSTRING + pack("<i", n) + bytes(obj))
else: else:
# Strip leading 's' due to repr() of str8() returning s'...' # Strip leading 's' due to repr() of str8() returning s'...'
self.write(STRING + bytes(repr(obj).lstrip("s")) + b'\n') self.write(STRING + repr(obj).lstrip("s").encode("ascii") + b'\n')
self.memoize(obj) self.memoize(obj)
dispatch[str8] = save_string dispatch[str8] = save_string
...@@ -710,7 +710,8 @@ class Pickler: ...@@ -710,7 +710,8 @@ class Pickler:
write(EXT4 + pack("<i", code)) write(EXT4 + pack("<i", code))
return return
write(GLOBAL + bytes(module) + b'\n' + bytes(name) + b'\n') write(GLOBAL + bytes(module, "utf-8") + b'\n' +
bytes(name, "utf-8") + b'\n')
self.memoize(obj) self.memoize(obj)
dispatch[FunctionType] = save_global dispatch[FunctionType] = save_global
......
...@@ -461,18 +461,19 @@ class AbstractPickleTests(unittest.TestCase): ...@@ -461,18 +461,19 @@ class AbstractPickleTests(unittest.TestCase):
self.assertRaises(self.error, self.loads, b'garyp') self.assertRaises(self.error, self.loads, b'garyp')
def test_insecure_strings(self): def test_insecure_strings(self):
insecure = ["abc", "2 + 2", # not quoted # XXX Some of these tests are temporarily disabled
#"'abc' + 'def'", # not a single quoted string insecure = [b"abc", b"2 + 2", # not quoted
"'abc", # quote is not closed ## b"'abc' + 'def'", # not a single quoted string
"'abc\"", # open quote and close quote don't match b"'abc", # quote is not closed
"'abc' ?", # junk after close quote b"'abc\"", # open quote and close quote don't match
"'\\'", # trailing backslash b"'abc' ?", # junk after close quote
b"'\\'", # trailing backslash
# some tests of the quoting rules # some tests of the quoting rules
#"'abc\"\''", ## b"'abc\"\''",
#"'\\\\a\'\'\'\\\'\\\\\''", ## b"'\\\\a\'\'\'\\\'\\\\\''",
] ]
for s in insecure: for b in insecure:
buf = b"S" + bytes(s) + b"\012p0\012." buf = b"S" + b + b"\012p0\012."
self.assertRaises(ValueError, self.loads, buf) self.assertRaises(ValueError, self.loads, buf)
def test_unicode(self): def test_unicode(self):
...@@ -496,12 +497,12 @@ class AbstractPickleTests(unittest.TestCase): ...@@ -496,12 +497,12 @@ class AbstractPickleTests(unittest.TestCase):
def test_maxint64(self): def test_maxint64(self):
maxint64 = (1 << 63) - 1 maxint64 = (1 << 63) - 1
data = b'I' + bytes(str(maxint64)) + b'\n.' data = b'I' + str(maxint64).encode("ascii") + b'\n.'
got = self.loads(data) got = self.loads(data)
self.assertEqual(got, maxint64) self.assertEqual(got, maxint64)
# Try too with a bogus literal. # Try too with a bogus literal.
data = b'I' + bytes(str(maxint64)) + b'JUNK\n.' data = b'I' + str(maxint64).encode("ascii") + b'JUNK\n.'
self.assertRaises(ValueError, self.loads, data) self.assertRaises(ValueError, self.loads, data)
def test_long(self): def test_long(self):
...@@ -699,7 +700,7 @@ class AbstractPickleTests(unittest.TestCase): ...@@ -699,7 +700,7 @@ class AbstractPickleTests(unittest.TestCase):
# Dump using protocol 1 for comparison. # Dump using protocol 1 for comparison.
s1 = self.dumps(x, 1) s1 = self.dumps(x, 1)
self.assert_(bytes(__name__) in s1) self.assert_(__name__.encode("utf-8") in s1)
self.assert_(b"MyList" in s1) self.assert_(b"MyList" in s1)
self.assertEqual(opcode_in_pickle(opcode, s1), False) self.assertEqual(opcode_in_pickle(opcode, s1), False)
...@@ -709,7 +710,7 @@ class AbstractPickleTests(unittest.TestCase): ...@@ -709,7 +710,7 @@ class AbstractPickleTests(unittest.TestCase):
# Dump using protocol 2 for test. # Dump using protocol 2 for test.
s2 = self.dumps(x, 2) s2 = self.dumps(x, 2)
self.assert_(bytes(__name__) not in s2) self.assert_(__name__.encode("utf-8") not in s2)
self.assert_(b"MyList" not in s2) self.assert_(b"MyList" not in s2)
self.assertEqual(opcode_in_pickle(opcode, s2), True, repr(s2)) self.assertEqual(opcode_in_pickle(opcode, s2), True, repr(s2))
......
...@@ -498,10 +498,12 @@ class BaseTest(unittest.TestCase): ...@@ -498,10 +498,12 @@ class BaseTest(unittest.TestCase):
EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob") EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby") EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
ba = buffer('a') # XXX Commented out. Is there any reason to support buffer objects
bb = buffer('b') # as arguments for str.replace()? GvR
EQ("bbc", "abc", "replace", ba, bb) ## ba = buffer('a')
EQ("aac", "abc", "replace", bb, ba) ## bb = buffer('b')
## EQ("bbc", "abc", "replace", ba, bb)
## EQ("aac", "abc", "replace", bb, ba)
# #
self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1) self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
......
...@@ -322,7 +322,7 @@ class ComplexTest(unittest.TestCase): ...@@ -322,7 +322,7 @@ class ComplexTest(unittest.TestCase):
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, "rb")
self.assertEqual(fo.read(), bytes("%s %s\n" % (a, b))) self.assertEqual(fo.read(), ("%s %s\n" % (a, b)).encode("ascii"))
finally: finally:
if (fo is not None) and (not fo.closed): if (fo is not None) and (not fo.closed):
fo.close() fo.close()
......
...@@ -1085,7 +1085,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase): ...@@ -1085,7 +1085,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
# This shouldn't blow up because of the month byte alone. If # This shouldn't blow up because of the month byte alone. If
# the implementation changes to do more-careful checking, it may # the implementation changes to do more-careful checking, it may
# blow up because other fields are insane. # blow up because other fields are insane.
self.theclass(bytes(base[:2] + chr(ord_byte) + base[3:])) self.theclass(bytes(base[:2] + chr(ord_byte) + base[3:], "ascii"))
############################################################################# #############################################################################
# datetime tests # datetime tests
......
...@@ -13,7 +13,7 @@ from test import test_support ...@@ -13,7 +13,7 @@ from test import test_support
# has no sense, it would have if we were testing a parser (i.e. pygettext) # has no sense, it would have if we were testing a parser (i.e. pygettext)
# - Tests should have only one assert. # - Tests should have only one assert.
GNU_MO_DATA = '''\ GNU_MO_DATA = b'''\
3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj 3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
...@@ -33,7 +33,7 @@ IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1 ...@@ -33,7 +33,7 @@ IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA== ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
''' '''
UMO_DATA = '''\ UMO_DATA = b'''\
3hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm 3hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm
AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S
ZXZpc2lvbi1EYXRlOiAyMDAzLTA0LTExIDEyOjQyLTA0MDAKTGFzdC1UcmFuc2xhdG9yOiBCYXJy ZXZpc2lvbi1EYXRlOiAyMDAzLTA0LTExIDEyOjQyLTA0MDAKTGFzdC1UcmFuc2xhdG9yOiBCYXJy
...@@ -43,7 +43,7 @@ bjsgY2hhcnNldD11dGYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0CkdlbmVyYXRl ...@@ -43,7 +43,7 @@ bjsgY2hhcnNldD11dGYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0CkdlbmVyYXRl
ZC1CeTogbWFudWFsbHkKAMKkeXoA ZC1CeTogbWFudWFsbHkKAMKkeXoA
''' '''
MMO_DATA = '''\ MMO_DATA = b'''\
3hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA 3hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA
UHJvamVjdC1JZC1WZXJzaW9uOiBObyBQcm9qZWN0IDAuMApQT1QtQ3JlYXRpb24tRGF0ZTogV2Vk UHJvamVjdC1JZC1WZXJzaW9uOiBObyBQcm9qZWN0IDAuMApQT1QtQ3JlYXRpb24tRGF0ZTogV2Vk
IERlYyAxMSAwNzo0NDoxNSAyMDAyClBPLVJldmlzaW9uLURhdGU6IDIwMDItMDgtMTQgMDE6MTg6 IERlYyAxMSAwNzo0NDoxNSAyMDAyClBPLVJldmlzaW9uLURhdGU6IDIwMDItMDgtMTQgMDE6MTg6
......
...@@ -24,21 +24,21 @@ class TestVectorsTestCase(unittest.TestCase): ...@@ -24,21 +24,21 @@ class TestVectorsTestCase(unittest.TestCase):
b"\xdd" * 50, b"\xdd" * 50,
"56be34521d144c88dbb8c733f0e8b3f6") "56be34521d144c88dbb8c733f0e8b3f6")
md5test("".join([chr(i) for i in range(1, 26)]), md5test(bytes(range(1, 26)),
b"\xcd" * 50, b"\xcd" * 50,
"697eaf0aca3a3aea3a75164746ffaa79") "697eaf0aca3a3aea3a75164746ffaa79")
md5test(chr(0x0C) * 16, md5test(b"\x0C" * 16,
"Test With Truncation", b"Test With Truncation",
"56461ef2342edc00f9bab995690efd4c") "56461ef2342edc00f9bab995690efd4c")
md5test(b"\xaa" * 80, md5test(b"\xaa" * 80,
"Test Using Larger Than Block-Size Key - Hash Key First", b"Test Using Larger Than Block-Size Key - Hash Key First",
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
md5test(b"\xaa" * 80, md5test(b"\xaa" * 80,
("Test Using Larger Than Block-Size Key " (b"Test Using Larger Than Block-Size Key "
"and Larger Than One Block-Size Data"), b"and Larger Than One Block-Size Data"),
"6f630fad67cda0ee1fb1f562db3aa53e") "6f630fad67cda0ee1fb1f562db3aa53e")
def test_sha_vectors(self): def test_sha_vectors(self):
...@@ -62,8 +62,8 @@ class TestVectorsTestCase(unittest.TestCase): ...@@ -62,8 +62,8 @@ class TestVectorsTestCase(unittest.TestCase):
b"\xCD" * 50, b"\xCD" * 50,
"4c9007f4026250c6bc8414f9bf50c86c2d7235da") "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
shatest(chr(0x0C) * 20, shatest(b"\x0C" * 20,
"Test With Truncation", b"Test With Truncation",
"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
shatest(b"\xAA" * 80, shatest(b"\xAA" * 80,
...@@ -82,14 +82,14 @@ class ConstructorTestCase(unittest.TestCase): ...@@ -82,14 +82,14 @@ class ConstructorTestCase(unittest.TestCase):
# Standard constructor call. # Standard constructor call.
failed = 0 failed = 0
try: try:
h = hmac.HMAC("key") h = hmac.HMAC(b"key")
except: except:
self.fail("Standard constructor call raised exception.") self.fail("Standard constructor call raised exception.")
def test_withtext(self): def test_withtext(self):
# Constructor call with text. # Constructor call with text.
try: try:
h = hmac.HMAC("key", "hash this!") h = hmac.HMAC(b"key", b"hash this!")
except: except:
self.fail("Constructor call with text argument raised exception.") self.fail("Constructor call with text argument raised exception.")
...@@ -97,7 +97,7 @@ class ConstructorTestCase(unittest.TestCase): ...@@ -97,7 +97,7 @@ class ConstructorTestCase(unittest.TestCase):
# Constructor call with text and digest module. # Constructor call with text and digest module.
from hashlib import sha1 from hashlib import sha1
try: try:
h = hmac.HMAC("key", "", sha1) h = hmac.HMAC(b"key", b"", sha1)
except: except:
self.fail("Constructor call with hashlib.sha1 raised exception.") self.fail("Constructor call with hashlib.sha1 raised exception.")
...@@ -107,15 +107,15 @@ class SanityTestCase(unittest.TestCase): ...@@ -107,15 +107,15 @@ class SanityTestCase(unittest.TestCase):
# Testing if HMAC defaults to MD5 algorithm. # Testing if HMAC defaults to MD5 algorithm.
# NOTE: this whitebox test depends on the hmac class internals # NOTE: this whitebox test depends on the hmac class internals
import hashlib import hashlib
h = hmac.HMAC("key") h = hmac.HMAC(b"key")
self.assertEqual(h.digest_cons, hashlib.md5) self.assertEqual(h.digest_cons, hashlib.md5)
def test_exercise_all_methods(self): def test_exercise_all_methods(self):
# Exercising all methods once. # Exercising all methods once.
# This must not raise any exceptions # This must not raise any exceptions
try: try:
h = hmac.HMAC("my secret key") h = hmac.HMAC(b"my secret key")
h.update("compute the hash of this text!") h.update(b"compute the hash of this text!")
dig = h.digest() dig = h.digest()
dig = h.hexdigest() dig = h.hexdigest()
h2 = h.copy() h2 = h.copy()
...@@ -126,7 +126,7 @@ class CopyTestCase(unittest.TestCase): ...@@ -126,7 +126,7 @@ class CopyTestCase(unittest.TestCase):
def test_attributes(self): def test_attributes(self):
# Testing if attributes are of same type. # Testing if attributes are of same type.
h1 = hmac.HMAC("key") h1 = hmac.HMAC(b"key")
h2 = h1.copy() h2 = h1.copy()
self.failUnless(h1.digest_cons == h2.digest_cons, self.failUnless(h1.digest_cons == h2.digest_cons,
"digest constructors don't match.") "digest constructors don't match.")
...@@ -137,7 +137,7 @@ class CopyTestCase(unittest.TestCase): ...@@ -137,7 +137,7 @@ class CopyTestCase(unittest.TestCase):
def test_realcopy(self): def test_realcopy(self):
# Testing if the copy method created a real copy. # Testing if the copy method created a real copy.
h1 = hmac.HMAC("key") h1 = hmac.HMAC(b"key")
h2 = h1.copy() h2 = h1.copy()
# Using id() in case somebody has overridden __cmp__. # Using id() in case somebody has overridden __cmp__.
self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.") self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
...@@ -148,8 +148,8 @@ class CopyTestCase(unittest.TestCase): ...@@ -148,8 +148,8 @@ class CopyTestCase(unittest.TestCase):
def test_equality(self): def test_equality(self):
# Testing if the copy has the same digests. # Testing if the copy has the same digests.
h1 = hmac.HMAC("key") h1 = hmac.HMAC(b"key")
h1.update("some random text") h1.update(b"some random text")
h2 = h1.copy() h2 = h1.copy()
self.assertEqual(h1.digest(), h2.digest(), self.assertEqual(h1.digest(), h2.digest(),
"Digest of copy doesn't match original digest.") "Digest of copy doesn't match original digest.")
......
...@@ -10,7 +10,7 @@ from test import test_support ...@@ -10,7 +10,7 @@ from test import test_support
class FakeSocket: class FakeSocket:
def __init__(self, text, fileclass=io.BytesIO): def __init__(self, text, fileclass=io.BytesIO):
if isinstance(text, str): if isinstance(text, str):
text = bytes(text) text = text.encode("ascii")
self.text = text self.text = text
self.fileclass = fileclass self.fileclass = fileclass
self.data = b'' self.data = b''
......
...@@ -305,7 +305,9 @@ class MemorySeekTestMixin: ...@@ -305,7 +305,9 @@ class MemorySeekTestMixin:
class BytesIOTest(MemorySeekTestMixin, unittest.TestCase): class BytesIOTest(MemorySeekTestMixin, unittest.TestCase):
buftype = bytes @staticmethod
def buftype(s):
return s.encode("utf-8")
ioclass = io.BytesIO ioclass = io.BytesIO
EOF = b"" EOF = b""
......
...@@ -660,7 +660,7 @@ class test_SpooledTemporaryFile(TC): ...@@ -660,7 +660,7 @@ class test_SpooledTemporaryFile(TC):
try: try:
f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir) f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
self.failIf(f._rolled) self.failIf(f._rolled)
f.write('blat ' * 5) f.write(b'blat ' * 5)
self.failUnless(f._rolled) self.failUnless(f._rolled)
filename = f.name filename = f.name
f.close() f.close()
...@@ -675,7 +675,7 @@ class test_SpooledTemporaryFile(TC): ...@@ -675,7 +675,7 @@ class test_SpooledTemporaryFile(TC):
self.failIf(f._rolled) self.failIf(f._rolled)
for i in range(5): for i in range(5):
f.seek(0, 0) f.seek(0, 0)
f.write('x' * 20) f.write(b'x' * 20)
self.failIf(f._rolled) self.failIf(f._rolled)
def test_write_sequential(self): def test_write_sequential(self):
...@@ -683,11 +683,11 @@ class test_SpooledTemporaryFile(TC): ...@@ -683,11 +683,11 @@ class test_SpooledTemporaryFile(TC):
# over afterward # over afterward
f = self.do_create(max_size=30) f = self.do_create(max_size=30)
self.failIf(f._rolled) self.failIf(f._rolled)
f.write('x' * 20) f.write(b'x' * 20)
self.failIf(f._rolled) self.failIf(f._rolled)
f.write('x' * 10) f.write(b'x' * 10)
self.failIf(f._rolled) self.failIf(f._rolled)
f.write('x') f.write(b'x')
self.failUnless(f._rolled) self.failUnless(f._rolled)
def test_sparse(self): def test_sparse(self):
...@@ -697,7 +697,7 @@ class test_SpooledTemporaryFile(TC): ...@@ -697,7 +697,7 @@ class test_SpooledTemporaryFile(TC):
self.failIf(f._rolled) self.failIf(f._rolled)
f.seek(100, 0) f.seek(100, 0)
self.failIf(f._rolled) self.failIf(f._rolled)
f.write('x') f.write(b'x')
self.failUnless(f._rolled) self.failUnless(f._rolled)
def test_fileno(self): def test_fileno(self):
...@@ -710,7 +710,7 @@ class test_SpooledTemporaryFile(TC): ...@@ -710,7 +710,7 @@ class test_SpooledTemporaryFile(TC):
def test_multiple_close(self): def test_multiple_close(self):
# A SpooledTemporaryFile can be closed many times without error # A SpooledTemporaryFile can be closed many times without error
f = tempfile.SpooledTemporaryFile() f = tempfile.SpooledTemporaryFile()
f.write('abc\n') f.write(b'abc\n')
f.close() f.close()
try: try:
f.close() f.close()
...@@ -727,8 +727,8 @@ class test_SpooledTemporaryFile(TC): ...@@ -727,8 +727,8 @@ class test_SpooledTemporaryFile(TC):
write = f.write write = f.write
seek = f.seek seek = f.seek
write("a" * 35) write(b"a" * 35)
write("b" * 35) write(b"b" * 35)
seek(0, 0) seek(0, 0)
self.assertEqual(read(70), b'a'*35 + b'b'*35) self.assertEqual(read(70), b'a'*35 + b'b'*35)
......
...@@ -701,7 +701,7 @@ class UnicodeTest( ...@@ -701,7 +701,7 @@ class UnicodeTest(
if not sys.platform.startswith('java'): if not sys.platform.startswith('java'):
self.assertEqual( self.assertEqual(
str( str(
buffer('character buffers are decoded to unicode'), buffer(b'character buffers are decoded to unicode'),
'utf-8', 'utf-8',
'strict' 'strict'
), ),
...@@ -791,7 +791,7 @@ class UnicodeTest( ...@@ -791,7 +791,7 @@ class UnicodeTest(
self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x') self.assertEqual(str(b'Andr\202 x', 'ascii', 'replace'), 'Andr\uFFFD x')
# Error handling (unknown character names) # Error handling (unknown character names)
self.assertEqual("\\N{foo}xx".decode("unicode-escape", "ignore"), "xx") self.assertEqual(b"\\N{foo}xx".decode("unicode-escape", "ignore"), "xx")
# Error handling (truncated escape sequence) # Error handling (truncated escape sequence)
self.assertRaises(UnicodeError, "\\".decode, "unicode-escape") self.assertRaises(UnicodeError, "\\".decode, "unicode-escape")
......
...@@ -72,12 +72,12 @@ def encode(in_file, out_file, name=None, mode=None): ...@@ -72,12 +72,12 @@ def encode(in_file, out_file, name=None, mode=None):
# #
# Write the data # Write the data
# #
out_file.write('begin %o %s\n' % ((mode & 0o777),name)) out_file.write(('begin %o %s\n' % ((mode & 0o777), name)).encode("ascii"))
data = in_file.read(45) data = in_file.read(45)
while len(data) > 0: while len(data) > 0:
out_file.write(str(binascii.b2a_uu(data), "ascii")) out_file.write(binascii.b2a_uu(data))
data = in_file.read(45) data = in_file.read(45)
out_file.write(' \nend\n') out_file.write(b' \nend\n')
def decode(in_file, out_file=None, mode=None, quiet=0): def decode(in_file, out_file=None, mode=None, quiet=0):
......
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