From: Guido van Rossum Date: Mon, 27 Aug 2007 17:23:59 +0000 (+0000) Subject: Changes in anticipation of stricter str vs. bytes enforcement. X-Git-Tag: v3.0a1~207 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=39478e852827a4ca6955151bf004c7a15099a4b1;p=thirdparty%2FPython%2Fcpython.git Changes in anticipation of stricter str vs. bytes enforcement. --- diff --git a/Lib/ctypes/test/test_internals.py b/Lib/ctypes/test/test_internals.py index 520ff87f86dc..c6a51a76de95 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 7bf3c03ea05c..1911689ea7f1 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 45d9d32f6af2..0553989cb451 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 6901a6444db9..9127f142a905 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):