From d0e204fb1da2ba7956ec2dff6501c3374eee1d43 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 6 Apr 2026 13:47:27 +0300 Subject: [PATCH] gh-148153: Do not use assert for parameter validation in base64 (GH-148154) base64.b32encode() now always raises ValueError instead of AssertionError for the value of map01 with invalid length. --- Doc/library/base64.rst | 3 --- Lib/base64.py | 1 - Lib/test/test_base64.py | 5 +++++ .../Library/2026-04-06-11-20-24.gh-issue-148153.ZtsuTl.rst | 2 ++ 4 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-04-06-11-20-24.gh-issue-148153.ZtsuTl.rst diff --git a/Doc/library/base64.rst b/Doc/library/base64.rst index 425dff8f2a9a..6e6e5d603e37 100644 --- a/Doc/library/base64.rst +++ b/Doc/library/base64.rst @@ -69,9 +69,6 @@ POST request. after at most every *wrapcol* characters. If *wrapcol* is zero (default), do not insert any newlines. - May assert or raise a :exc:`ValueError` if the length of *altchars* is not 2. Raises a - :exc:`TypeError` if *altchars* is not a :term:`bytes-like object`. - .. versionchanged:: 3.15 Added the *padded* and *wrapcol* parameters. diff --git a/Lib/base64.py b/Lib/base64.py index a94bec4d031c..7f39c68070b5 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -237,7 +237,6 @@ def b32decode(s, casefold=False, map01=None, *, padded=True, ignorechars=b''): # either L (el) or I (eye). if map01 is not None: map01 = _bytes_from_decode_data(map01) - assert len(map01) == 1, repr(map01) s = s.translate(bytes.maketrans(b'01', b'O' + map01)) if casefold: s = s.upper() diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 1a4dd56a553f..868abcfee24e 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -607,6 +607,11 @@ class BaseXYTestCase(unittest.TestCase): self.assertRaises(binascii.Error, base64.b32decode, b'M1O23456') self.assertRaises(binascii.Error, base64.b32decode, b'ML023456') self.assertRaises(binascii.Error, base64.b32decode, b'MI023456') + self.assertRaises(ValueError, base64.b32decode, b'', map01=b'') + self.assertRaises(ValueError, base64.b32decode, b'', map01=b'LI') + self.assertRaises(TypeError, base64.b32decode, b'', map01=0) + eq(base64.b32decode(b'MLO23456', map01=None), res_L) + self.assertRaises(binascii.Error, base64.b32decode, b'M1023456', map01=None) data = b'M1023456' data_str = data.decode('ascii') diff --git a/Misc/NEWS.d/next/Library/2026-04-06-11-20-24.gh-issue-148153.ZtsuTl.rst b/Misc/NEWS.d/next/Library/2026-04-06-11-20-24.gh-issue-148153.ZtsuTl.rst new file mode 100644 index 000000000000..7fd30562739f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-04-06-11-20-24.gh-issue-148153.ZtsuTl.rst @@ -0,0 +1,2 @@ +:func:`base64.b32encode` now always raises :exc:`ValueError` instead of +:exc:`AssertionError` for the value of *map01* with invalid length. -- 2.47.3