From 39478e852827a4ca6955151bf004c7a15099a4b1 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 27 Aug 2007 17:23:59 +0000 Subject: Changes in anticipation of stricter str vs. bytes enforcement. --- Lib/ctypes/test/test_internals.py | 6 ++++-- Lib/hmac.py | 10 ++-------- Lib/mimetools.py | 2 +- Lib/pickle.py | 17 +++++++++-------- Lib/test/pickletester.py | 29 +++++++++++++++-------------- Lib/test/string_tests.py | 10 ++++++---- Lib/test/test_complex.py | 2 +- Lib/test/test_datetime.py | 2 +- Lib/test/test_gettext.py | 6 +++--- Lib/test/test_hmac.py | 36 ++++++++++++++++++------------------ Lib/test/test_httplib.py | 2 +- Lib/test/test_io.py | 4 +++- Lib/test/test_tempfile.py | 18 +++++++++--------- Lib/test/test_unicode.py | 4 ++-- Lib/uu.py | 6 +++--- 15 files changed, 78 insertions(+), 76 deletions(-) diff --git a/Lib/ctypes/test/test_internals.py b/Lib/ctypes/test/test_internals.py index 520ff87..c6a51a7 100644 --- a/Lib/ctypes/test/test_internals.py +++ b/Lib/ctypes/test/test_internals.py @@ -76,11 +76,13 @@ class ObjectsTestCase(unittest.TestCase): x = X() x.a = s1 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.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 ## del y ## print x._b_base_._objects diff --git a/Lib/hmac.py b/Lib/hmac.py index 7bf3c03..1911689 100644 --- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -37,10 +37,7 @@ class HMAC: if key is _secret_backdoor_key: # cheap return - if not isinstance(key, bytes): - if hasattr(key, "__index__"): - raise TypeError("key can't be a number") - key = bytes(key) + assert isinstance(key, bytes), repr(key) if digestmod is None: import hashlib @@ -71,10 +68,7 @@ class HMAC: def update(self, msg): """Update this hashing object with the string msg. """ - if not isinstance(msg, bytes): - if hasattr(msg, "__index__"): - raise TypeError("msg can't be a number") - msg = bytes(msg) + assert isinstance(msg, bytes), repr(msg) self.inner.update(msg) def copy(self): diff --git a/Lib/mimetools.py b/Lib/mimetools.py index 45d9d32..0553989 100644 --- a/Lib/mimetools.py +++ b/Lib/mimetools.py @@ -158,7 +158,7 @@ def decode(input, output, encoding): import uu return uu.decode(input, output) if encoding in ('7bit', '8bit'): - return output.write(input.read().decode("Latin-1")) + return output.write(input.read()) if encoding in decodetab: pipethrough(input, decodetab[encoding], output) else: diff --git a/Lib/pickle.py b/Lib/pickle.py index 6901a64..9127f14 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -248,7 +248,7 @@ class Pickler: else: return LONG_BINPUT + pack("d', obj)) else: - self.write(FLOAT + bytes(repr(obj)) + b'\n') + self.write(FLOAT + repr(obj).encode("ascii") + b'\n') dispatch[float] = save_float def save_string(self, obj, pack=struct.pack): @@ -500,7 +500,7 @@ class Pickler: self.write(BINSTRING + pack(" 0: - out_file.write(str(binascii.b2a_uu(data), "ascii")) + out_file.write(binascii.b2a_uu(data)) 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): -- cgit v0.12