diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-12-14 03:41:59 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-12-14 03:41:59 (GMT) |
commit | d77fe94b4e2840f9031ba50d2205e97dd9115160 (patch) | |
tree | 189d324cbd3e0b229037a2bf2b97c78ca755f8e4 /Lib | |
parent | edd6a817d1794c9e26d3d215ac8f90048fb2a6df (diff) | |
download | cpython-d77fe94b4e2840f9031ba50d2205e97dd9115160.zip cpython-d77fe94b4e2840f9031ba50d2205e97dd9115160.tar.gz cpython-d77fe94b4e2840f9031ba50d2205e97dd9115160.tar.bz2 |
Issue #22088: Port base64 character ignoring doc and test from 857d9fe60169
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/base64.py | 6 | ||||
-rw-r--r-- | Lib/test/test_base64.py | 16 |
2 files changed, 18 insertions, 4 deletions
diff --git a/Lib/base64.py b/Lib/base64.py index 82c112c..844907f 100755 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -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], '+/')) diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 3f2cee4..5dd283b 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -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(''), '') |