Index: Doc/library/base64.rst =================================================================== --- Doc/library/base64.rst (revision 71822) +++ Doc/library/base64.rst (working copy) @@ -128,10 +128,10 @@ ``input.read()`` returns an empty string. -.. function:: decodestring(s) +.. function:: decodebytes(s) - Decode the string *s*, which must contain one or more lines of base64 encoded - data, and return a string containing the resulting binary data. + Decode the bytes *s*, which must contain one or more lines of base64 encoded + data, and return a bytes containing the resulting binary data. .. function:: encode(input, output) @@ -143,11 +143,11 @@ data plus a trailing newline character (``'\n'``). -.. function:: encodestring(s) +.. function:: encodebytes(s) - Encode the string *s*, which can contain arbitrary binary data, and return a - string containing one or more lines of base64-encoded data. - :func:`encodestring` returns a string containing one or more lines of + Encode the bytes *s*, which can contain arbitrary binary data, and return a + bytes containing one or more lines of base64-encoded data. + :func:`encodestring` returns a bytes containing one or more lines of base64-encoded data always including an extra trailing newline (``'\n'``). An example usage of the module: Index: Lib/base64.py =================================================================== --- Lib/base64.py (revision 71822) +++ Lib/base64.py (working copy) @@ -329,8 +329,8 @@ 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. """ @@ -342,16 +342,21 @@ pieces.append(binascii.b2a_base64(chunk)) return b"".join(pieces) +# For historical reasons +encodestring = encodebytes -def decodestring(s): - """Decode a string. +def decodebytes(s): + """Decode bytes of base-64 encoded data. + Argument and return value are bytes. """ if not isinstance(s, bytes_types): raise TypeError("expected bytes, not %s" % s.__class__.__name__) return binascii.a2b_base64(s) +# For historical reasons +decodestring = decodebytes # Usable as a script... Index: Lib/test/test_base64.py =================================================================== --- Lib/test/test_base64.py (revision 71822) +++ Lib/test/test_base64.py (working copy) @@ -6,35 +6,37 @@ 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"), b"d3d3LnB5dGhvbi5vcmc=\n") + eq(base64.encodebytes(b"a"), b"YQ==\n") + eq(base64.encodebytes(b"ab"), b"YWI=\n") + eq(base64.encodebytes(b"abc"), b"YWJj\n") + eq(base64.encodebytes(b""), 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, "") + self.assertRaises(TypeError, base64.encodebytes, "") + eq(base64.encodestring(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") - def test_decodestring(self): + def test_decodebytes(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" + eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") + eq(base64.decodebytes(b"YQ==\n"), b"a") + eq(base64.decodebytes(b"YWI=\n"), b"ab") + eq(base64.decodebytes(b"YWJj\n"), b"abc") + eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), b"abcdefghijklmnopqrstuvwxyz" b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"0123456789!@#0^&*();:<>,. []{}") - eq(base64.decodestring(b''), b'') - self.assertRaises(TypeError, base64.decodestring, "") + eq(base64.decodebytes(b''), b'') + self.assertRaises(TypeError, base64.decodebytes, "") + eq(base64.decodestring(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") def test_encode(self): eq = self.assertEqual