Skip to content

Commit b54d801

Browse files
committed
#3613: add base64.encodebytes and decodebytes as the new spelling of encodestring and decodestring; deprecate the latter.
1 parent cef803f commit b54d801

File tree

6 files changed

+66
-48
lines changed

6 files changed

+66
-48
lines changed

Doc/library/base64.rst

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,38 +116,44 @@ The modern interface provides:
116116
incorrectly padded or if there are non-alphabet characters present in the
117117
string.
118118

119-
The legacy interface:
120119

120+
The legacy interface:
121121

122122
.. function:: decode(input, output)
123123

124-
Decode the contents of the *input* file and write the resulting binary data to
125-
the *output* file. *input* and *output* must either be file objects or objects
126-
that mimic the file object interface. *input* will be read until
127-
``input.read()`` returns an empty string.
124+
Decode the contents of the binary *input* file and write the resulting binary
125+
data to the *output* file. *input* and *output* must either be file objects
126+
or objects that mimic the file object interface working with bytes
127+
objects. *input* will be read until ``input.read()`` returns an empty string.
128128

129129

130-
.. function:: decodestring(s)
130+
.. function:: decodebytes(s)
131+
decodestring(s)
131132

132-
Decode the string *s*, which must contain one or more lines of base64 encoded
133-
data, and return a string containing the resulting binary data.
133+
Decode the bytestring *s*, which must contain one or more lines of base64
134+
encoded data, and return a bytestring containing the resulting binary data.
135+
``decodestring`` is a deprecated alias.
134136

135137

136138
.. function:: encode(input, output)
137139

138-
Encode the contents of the *input* file and write the resulting base64 encoded
139-
data to the *output* file. *input* and *output* must either be file objects or
140-
objects that mimic the file object interface. *input* will be read until
141-
``input.read()`` returns an empty string. :func:`encode` returns the encoded
142-
data plus a trailing newline character (``'\n'``).
140+
Encode the contents of the binary *input* file and write the resulting base64
141+
encoded data to the *output* file. *input* and *output* must either be file
142+
objects or objects that mimic the file object interface working with bytes
143+
objects. *input* will be read until ``input.read()`` returns an empty string.
144+
:func:`encode` returns the encoded data plus a trailing newline character
145+
(``b'\n'``).
146+
143147

148+
.. function:: encodebytes(s)
149+
encodestring(s)
144150

145-
.. function:: encodestring(s)
151+
Encode the bytestring *s*, which can contain arbitrary binary data, and
152+
return a bytestring containing one or more lines of base64-encoded data.
153+
:func:`encodebytes` returns a string containing one or more lines of
154+
base64-encoded data always including an extra trailing newline (``b'\n'``).
155+
``encodestring`` is a deprecated alias.
146156

147-
Encode the string *s*, which can contain arbitrary binary data, and return a
148-
string containing one or more lines of base64-encoded data.
149-
:func:`encodestring` returns a string containing one or more lines of
150-
base64-encoded data always including an extra trailing newline (``'\n'``).
151157

152158
An example usage of the module:
153159

Lib/base64.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
__all__ = [
1515
# Legacy interface exports traditional RFC 1521 Base64 encodings
16-
'encode', 'decode', 'encodestring', 'decodestring',
16+
'encode', 'decode', 'encodebytes', 'decodebytes',
1717
# Generalized interface for other encodings
1818
'b64encode', 'b64decode', 'b32encode', 'b32decode',
1919
'b16encode', 'b16decode',
@@ -329,11 +329,9 @@ def decode(input, output):
329329
output.write(s)
330330

331331

332-
def encodestring(s):
333-
"""Encode a string into multiple lines of base-64 data.
334-
335-
Argument and return value are bytes.
336-
"""
332+
def encodebytes(s):
333+
"""Encode a bytestring into a bytestring containing multiple lines
334+
of base-64 data."""
337335
if not isinstance(s, bytes_types):
338336
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
339337
pieces = []
@@ -342,16 +340,26 @@ def encodestring(s):
342340
pieces.append(binascii.b2a_base64(chunk))
343341
return b"".join(pieces)
344342

343+
def encodestring(s):
344+
"""Legacy alias of encodebytes()."""
345+
import warnings
346+
warnings.warn("encodestring() is a deprecated alias, use encodebytes()",
347+
DeprecationWarning, 2)
348+
return encodebytes(s)
345349

346-
def decodestring(s):
347-
"""Decode a string.
348350

349-
Argument and return value are bytes.
350-
"""
351+
def decodebytes(s):
352+
"""Decode a bytestring of base-64 data into a bytestring."""
351353
if not isinstance(s, bytes_types):
352354
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
353355
return binascii.a2b_base64(s)
354356

357+
def decodestring(s):
358+
"""Legacy alias of decodebytes()."""
359+
import warnings
360+
warnings.warn("decodestring() is a deprecated alias, use decodebytes()",
361+
DeprecationWarning, 2)
362+
return decodebytes(s)
355363

356364

357365
# Usable as a script...

Lib/test/test_base64.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@
66

77

88
class LegacyBase64TestCase(unittest.TestCase):
9-
def test_encodestring(self):
9+
def test_encodebytes(self):
1010
eq = self.assertEqual
11-
eq(base64.encodestring(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n")
12-
eq(base64.encodestring(b"a"), b"YQ==\n")
13-
eq(base64.encodestring(b"ab"), b"YWI=\n")
14-
eq(base64.encodestring(b"abc"), b"YWJj\n")
15-
eq(base64.encodestring(b""), b"")
16-
eq(base64.encodestring(b"abcdefghijklmnopqrstuvwxyz"
11+
eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n")
12+
eq(base64.encodebytes(b"a"), b"YQ==\n")
13+
eq(base64.encodebytes(b"ab"), b"YWI=\n")
14+
eq(base64.encodebytes(b"abc"), b"YWJj\n")
15+
eq(base64.encodebytes(b""), b"")
16+
eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz"
1717
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1818
b"0123456789!@#0^&*();:<>,. []{}"),
1919
b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
2020
b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
2121
b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
22-
self.assertRaises(TypeError, base64.encodestring, "")
22+
self.assertRaises(TypeError, base64.encodebytes, "")
2323

24-
def test_decodestring(self):
24+
def test_decodebytes(self):
2525
eq = self.assertEqual
26-
eq(base64.decodestring(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org")
27-
eq(base64.decodestring(b"YQ==\n"), b"a")
28-
eq(base64.decodestring(b"YWI=\n"), b"ab")
29-
eq(base64.decodestring(b"YWJj\n"), b"abc")
30-
eq(base64.decodestring(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
26+
eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org")
27+
eq(base64.decodebytes(b"YQ==\n"), b"a")
28+
eq(base64.decodebytes(b"YWI=\n"), b"ab")
29+
eq(base64.decodebytes(b"YWJj\n"), b"abc")
30+
eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
3131
b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
3232
b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
3333
b"abcdefghijklmnopqrstuvwxyz"
3434
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3535
b"0123456789!@#0^&*();:<>,. []{}")
36-
eq(base64.decodestring(b''), b'')
37-
self.assertRaises(TypeError, base64.decodestring, "")
36+
eq(base64.decodebytes(b''), b'')
37+
self.assertRaises(TypeError, base64.decodebytes, "")
3838

3939
def test_encode(self):
4040
eq = self.assertEqual

Lib/test/test_xmlrpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def test_string(self):
232232

233233
def test_decode(self):
234234
d = b'\x01\x02\x03abc123\xff\xfe'
235-
de = base64.encodestring(d)
235+
de = base64.encodebytes(d)
236236
t1 = xmlrpclib.Binary()
237237
t1.decode(de)
238238
self.assertEqual(str(t1), str(d, "latin-1"))

Lib/xmlrpc/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,11 @@ def __ne__(self, other):
419419
return self.data != other
420420

421421
def decode(self, data):
422-
self.data = base64.decodestring(data)
422+
self.data = base64.decodebytes(data)
423423

424424
def encode(self, out):
425425
out.write("<value><base64>\n")
426-
encoded = base64.encodestring(self.data)
426+
encoded = base64.encodebytes(self.data)
427427
out.write(encoded.decode('ascii'))
428428
out.write('\n')
429429
out.write("</base64></value>\n")
@@ -1100,7 +1100,7 @@ def get_host_info(self, host):
11001100
if auth:
11011101
import base64
11021102
auth = urllib.parse.unquote_to_bytes(auth)
1103-
auth = base64.encodestring(auth).decode("utf-8")
1103+
auth = base64.encodebytes(auth).decode("utf-8")
11041104
auth = "".join(auth.split()) # get rid of whitespace
11051105
extra_headers = [
11061106
("Authorization", "Basic " + auth)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #3613: base64.{encode,decode}string are now called
25+
base64.{encode,decode}bytes which reflects what type they accept and return.
26+
The old names are still there as deprecated aliases.
27+
2428
- Issue #5767: Remove sgmlop support from xmlrpc.client.
2529

2630
- Issue #6150: Fix test_unicode on wide-unicode builds.

0 commit comments

Comments
 (0)