Index: Lib/base64.py =================================================================== --- Lib/base64.py (revision 71822) +++ Lib/base64.py (working copy) @@ -329,28 +329,31 @@ output.write(s) -def encodestring(s): - """Encode a string into multiple lines of base-64 data. +def encodebytes(s): + """Encode bytes into multiple lines of base-64 data. - Argument and return value are bytes. + Argument type is bytes, return type is str. """ if not isinstance(s, bytes_types): raise TypeError("expected bytes, not %s" % s.__class__.__name__) pieces = [] for i in range(0, len(s), MAXBINSIZE): chunk = s[i : i + MAXBINSIZE] - pieces.append(binascii.b2a_base64(chunk)) - return b"".join(pieces) + pieces.append(binascii.b2a_base64(chunk).decode("ascii")) + return "".join(pieces) +# For historical reasons +encodestring = encodebytes + def decodestring(s): """Decode a string. - Argument and return value are bytes. + Argument type is str, return type is bytes. """ - if not isinstance(s, bytes_types): - raise TypeError("expected bytes, not %s" % s.__class__.__name__) - return binascii.a2b_base64(s) + if not isinstance(s, str): + raise TypeError("expected str, not %s" % s.__class__.__name__) + return binascii.a2b_base64(s.encode("ascii")) Index: Lib/test/test_base64.py =================================================================== --- Lib/test/test_base64.py (revision 71822) +++ Lib/test/test_base64.py (working copy) @@ -6,35 +6,36 @@ class LegacyBase64TestCase(unittest.TestCase): - def test_encodestring(self): + def test_encodebytes(self): eq = self.assertEqual - eq(base64.encodestring(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") - eq(base64.encodestring(b"a"), b"YQ==\n") - eq(base64.encodestring(b"ab"), b"YWI=\n") - eq(base64.encodestring(b"abc"), b"YWJj\n") - eq(base64.encodestring(b""), b"") - eq(base64.encodestring(b"abcdefghijklmnopqrstuvwxyz" + eq(base64.encodebytes(b"www.python.org"), "d3d3LnB5dGhvbi5vcmc=\n") + eq(base64.encodebytes(b"a"), "YQ==\n") + eq(base64.encodebytes(b"ab"), "YWI=\n") + eq(base64.encodebytes(b"abc"), "YWJj\n") + eq(base64.encodebytes(b""), "") + eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"0123456789!@#0^&*();:<>,. []{}"), - b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") - self.assertRaises(TypeError, base64.encodestring, "") + "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" + "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" + "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") + self.assertRaises(TypeError, base64.encodebytes, "") + eq(base64.encodestring(b"www.python.org"), "d3d3LnB5dGhvbi5vcmc=\n") def test_decodestring(self): eq = self.assertEqual - eq(base64.decodestring(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") - eq(base64.decodestring(b"YQ==\n"), b"a") - eq(base64.decodestring(b"YWI=\n"), b"ab") - eq(base64.decodestring(b"YWJj\n"), b"abc") - eq(base64.decodestring(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" - b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" - b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), + eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") + eq(base64.decodestring("YQ==\n"), b"a") + eq(base64.decodestring("YWI=\n"), b"ab") + eq(base64.decodestring("YWJj\n"), b"abc") + eq(base64.decodestring("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" + "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" + "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), b"abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"0123456789!@#0^&*();:<>,. []{}") - eq(base64.decodestring(b''), b'') - self.assertRaises(TypeError, base64.decodestring, "") + eq(base64.decodestring(''), b'') + self.assertRaises(TypeError, base64.decodestring, b"") def test_encode(self): eq = self.assertEqual