]> granicus.if.org Git - python/commitdiff
Issue #22088: Port base64 character ignoring doc and test from 857d9fe60169
authorMartin Panter <vadmium+py@gmail.com>
Mon, 14 Dec 2015 03:41:59 +0000 (03:41 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Mon, 14 Dec 2015 03:41:59 +0000 (03:41 +0000)
Doc/library/base64.rst
Lib/base64.py
Lib/test/test_base64.py

index 2a9840bee1d3556a74236f7a535cd4b9ed075e2d..e346efbbc1f222da9661203f79a556b12cd44d37 100644 (file)
@@ -45,9 +45,9 @@ The modern interface, which was introduced in Python 2.4, provides:
    length 2 (additional characters are ignored) which specifies the alternative
    alphabet used instead of the ``+`` and ``/`` characters.
 
-   The decoded string is returned.  A :exc:`TypeError` is raised if *s* were
-   incorrectly padded or if there are non-alphabet characters present in the
-   string.
+   The decoded string is returned.  A :exc:`TypeError` is raised if *s* is
+   incorrectly padded.  Non-base64-alphabet characters are
+   discarded prior to the padding check.
 
 
 .. function:: standard_b64encode(s)
index 82c112c56ac79aff8eaa8331ff8bed3624d073f8..844907feef56ae0e2fca5e106a8f7ac362b64feb 100755 (executable)
@@ -64,9 +64,9 @@ def b64decode(s, altchars=None):
     length 2 (additional characters are ignored) which specifies the
     alternative alphabet used instead of the '+' and '/' characters.
 
-    The decoded string is returned.  A TypeError is raised if s were
-    incorrectly padded or if there are non-alphabet characters present in the
-    string.
+    The decoded string is returned.  A TypeError is raised if s is
+    incorrectly padded.  Non-base64-alphabet characters are discarded prior
+    to the padding check.
     """
     if altchars is not None:
         s = s.translate(string.maketrans(altchars[:2], '+/'))
index 3f2cee4fded50d62b24cb0c8144c4ba37bfe4c89..5dd283bb995af9047f683d98656a47782d9f31c4 100644 (file)
@@ -137,9 +137,23 @@ class BaseXYTestCase(unittest.TestCase):
         # Non-bytes
         eq(base64.urlsafe_b64decode(bytearray('01a-b_cd')), '\xd3V\xbeo\xf7\x1d')
 
-    def test_b64decode_error(self):
+    def test_b64decode_padding_error(self):
         self.assertRaises(TypeError, base64.b64decode, 'abc')
 
+    def test_b64decode_invalid_chars(self):
+        # issue 1466065: Test some invalid characters.
+        tests = ((b'%3d==', b'\xdd'),
+                 (b'$3d==', b'\xdd'),
+                 (b'[==', b''),
+                 (b'YW]3=', b'am'),
+                 (b'3{d==', b'\xdd'),
+                 (b'3d}==', b'\xdd'),
+                 (b'@@', b''),
+                 (b'!', b''),
+                 (b'YWJj\nYWI=', b'abcab'))
+        for bstr, res in tests:
+            self.assertEqual(base64.b64decode(bstr), res)
+
     def test_b32encode(self):
         eq = self.assertEqual
         eq(base64.b32encode(''), '')