cpython: d90f25e1a705 (original) (raw)

--- a/Doc/library/base64.rst +++ b/Doc/library/base64.rst @@ -27,6 +27,10 @@ byte strings, but only using the Base64 ASCII-only Unicode strings are now accepted by the decoding functions of the modern interface. +.. versionchanged:: 3.4

The modern interface provides: .. function:: b64encode(s, altchars=None)

--- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1208,36 +1208,41 @@ mappings. .. tabularcolumns:: |l|L|L| -+----------------------+---------------------------+------------------------------+ -| Codec | Purpose | Encoder/decoder | -+======================+===========================+==============================+ -| base64_codec [#b64]_ | Convert operand to MIME | :meth:base64.b64encode, | -| | base64 (the result always | :meth:base64.b64decode | -| | includes a trailing | | -| | '\n') | | -+----------------------+---------------------------+------------------------------+ -| bz2_codec | Compress the operand | :meth:bz2.compress, | -| | using bz2 | :meth:bz2.decompress | -+----------------------+---------------------------+------------------------------+ -| hex_codec | Convert operand to | :meth:base64.b16encode, | -| | hexadecimal | :meth:base64.b16decode | -| | representation, with two | | -| | digits per byte | | -+----------------------+---------------------------+------------------------------+ -| quopri_codec | Convert operand to MIME | :meth:quopri.encodestring, | -| | quoted printable | :meth:quopri.decodestring | -+----------------------+---------------------------+------------------------------+ -| uu_codec | Convert the operand using | :meth:uu.encode, | -| | uuencode | :meth:uu.decode | -+----------------------+---------------------------+------------------------------+ -| zlib_codec | Compress the operand | :meth:zlib.compress, | -| | using gzip | :meth:zlib.decompress | -+----------------------+---------------------------+------------------------------+ ++----------------------+------------------------------+------------------------------+ +| Codec | Purpose | Encoder / decoder | ++======================+==============================+==============================+ +| base64_codec [#b64]_ | Convert operand to MIME | :meth:base64.b64encode / | +| | base64 (the result always | :meth:base64.b64decode | +| | includes a trailing | | +| | '\n') | | +| | | | +| | .. versionchanged:: 3.4 | | +| | accepts any | | +| | :term:bytes-like object | | +| | as input for encoding and | | +| | decoding | | ++----------------------+------------------------------+------------------------------+ +| bz2_codec | Compress the operand | :meth:bz2.compress / | +| | using bz2 | :meth:bz2.decompress | ++----------------------+------------------------------+------------------------------+ +| hex_codec | Convert operand to | :meth:base64.b16encode / | +| | hexadecimal | :meth:base64.b16decode | +| | representation, with two | | +| | digits per byte | | ++----------------------+------------------------------+------------------------------+ +| quopri_codec | Convert operand to MIME | :meth:quopri.encodestring /| +| | quoted printable | :meth:quopri.decodestring | ++----------------------+------------------------------+------------------------------+ +| uu_codec | Convert the operand using | :meth:uu.encode / | +| | uuencode | :meth:uu.decode | ++----------------------+------------------------------+------------------------------+ +| zlib_codec | Compress the operand | :meth:zlib.compress / | +| | using gzip | :meth:zlib.decompress | ++----------------------+------------------------------+------------------------------+ -.. [#b64] Rather than accepting any :term:bytes-like object,

The following codecs provide :class:str to :class:str mappings.

--- a/Lib/base64.py +++ b/Lib/base64.py @@ -35,11 +35,13 @@ def _bytes_from_decode_data(s): return s.encode('ascii') except UnicodeEncodeError: raise ValueError('string argument should contain only ASCII characters')

-

Base64 encoding/decoding uses binascii

@@ -54,14 +56,9 @@ def b64encode(s, altchars=None): The encoded byte string is returned. """

+def _input_type_check(s):

+ def encodebytes(s): """Encode a bytestring into a bytestring containing multiple lines of base-64 data."""

@@ -328,8 +337,7 @@ def encodestring(s): def decodebytes(s): """Decode a bytestring of base-64 data into a bytestring."""

--- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -5,10 +5,21 @@ import binascii import os import sys import subprocess - +import struct +from array import array class LegacyBase64TestCase(unittest.TestCase): +

+ def test_encodebytes(self): eq = self.assertEqual eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") @@ -24,7 +35,9 @@ class LegacyBase64TestCase(unittest.Test b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") # Non-bytes eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n')

def test_decodebytes(self): eq = self.assertEqual @@ -41,7 +54,9 @@ class LegacyBase64TestCase(unittest.Test eq(base64.decodebytes(b''), b'') # Non-bytes eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc')

def test_encode(self): eq = self.assertEqual @@ -73,6 +88,38 @@ class LegacyBase64TestCase(unittest.Test class BaseXYTestCase(unittest.TestCase): +

+

+

+

+

+ + def test_b64encode(self): eq = self.assertEqual # Test default alphabet @@ -90,13 +137,16 @@ class BaseXYTestCase(unittest.TestCase): b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") # Test with arbitrary alternative characters eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'$'), b'01ab$cd')

@@ -110,15 +160,15 @@ class BaseXYTestCase(unittest.TestCase): b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") # Non-bytes

def test_b64decode(self): eq = self.assertEqual @@ -141,7 +191,8 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.b64decode(data), res) eq(base64.b64decode(data.decode('ascii')), res) # Non-bytes

# Test with arbitrary alternative characters tests_altchars = {(b'01ab$cd', b'$'): b'\xd3V\xbeo\xf7\x1d', @@ -160,7 +211,8 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.standard_b64decode(data), res) eq(base64.standard_b64decode(data.decode('ascii')), res) # Non-bytes

# Test with 'URL safe' alternative characters tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', @@ -170,7 +222,9 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.urlsafe_b64decode(data), res) eq(base64.urlsafe_b64decode(data.decode('ascii')), res) # Non-bytes

def test_b64decode_padding_error(self): self.assertRaises(binascii.Error, base64.b64decode, b'abc') @@ -205,8 +259,8 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.b32encode(b'abcd'), b'MFRGGZA=') eq(base64.b32encode(b'abcde'), b'MFRGGZDF') # Non-bytes

def test_b32decode(self): eq = self.assertEqual @@ -222,7 +276,8 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.b32decode(data), res) eq(base64.b32decode(data.decode('ascii')), res) # Non-bytes

def test_b32decode_casefold(self): eq = self.assertEqual @@ -277,8 +332,9 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') eq(base64.b16encode(b'\x00'), b'00') # Non-bytes

def test_b16decode(self): eq = self.assertEqual @@ -293,7 +349,15 @@ class BaseXYTestCase(unittest.TestCase): eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') # Non-bytes

def test_decode_nonascii_str(self): decode_funcs = (base64.b64decode,

--- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -2285,6 +2285,24 @@ class TransformCodecTest(unittest.TestCa sout = reader.readline() self.assertEqual(sout, b"\x80")

+ + @unittest.skipUnless(sys.platform == 'win32', 'code pages are specific to Windows')

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -20,6 +20,10 @@ Core and Builtins Library ------- +- Issue #17839: base64.decodebytes and base64.encodebytes now accept any