From c30cfeeafd2e762ed22d1a9502539e00ddc9c200 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 6 Apr 2026 11:29:33 +0300 Subject: [PATCH] gh-148153: Do not use assert for parameter validation in base64 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 425dff8f2a9ad1..6e6e5d603e37b1 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 a94bec4d031c52..7f39c68070b5da 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 1a4dd56a553f4d..868abcfee24e10 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -607,6 +607,11 @@ def test_b32decode_map01(self): 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 00000000000000..7fd30562739fee --- /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.