]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-148153: Do not use assert for parameter validation in base64 (GH-148154)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 6 Apr 2026 10:47:27 +0000 (13:47 +0300)
committerGitHub <noreply@github.com>
Mon, 6 Apr 2026 10:47:27 +0000 (13:47 +0300)
base64.b32encode() now always raises ValueError instead of
AssertionError for the value of map01 with invalid length.

Doc/library/base64.rst
Lib/base64.py
Lib/test/test_base64.py
Misc/NEWS.d/next/Library/2026-04-06-11-20-24.gh-issue-148153.ZtsuTl.rst [new file with mode: 0644]

index 425dff8f2a9ad10cf273386a11611b0045990705..6e6e5d603e37b17a2519ad2608057c58c8d6c618 100644 (file)
@@ -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.
 
index a94bec4d031c525b148388455699ca7a56b78d71..7f39c68070b5da8ab5af6aba9bb9bcd8de8e6150 100644 (file)
@@ -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()
index 1a4dd56a553f4d3138c7440028f0d2a3f57205a9..868abcfee24e1018c99cbe98a80604f647f4d49f 100644 (file)
@@ -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 (file)
index 0000000..7fd3056
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`base64.b32encode` now always raises :exc:`ValueError` instead of
+:exc:`AssertionError` for the value of *map01* with invalid length.