Skip to content

Commit c30cfee

Browse files
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.
1 parent a95ee3a commit c30cfee

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

Doc/library/base64.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ POST request.
6969
after at most every *wrapcol* characters.
7070
If *wrapcol* is zero (default), do not insert any newlines.
7171

72-
May assert or raise a :exc:`ValueError` if the length of *altchars* is not 2. Raises a
73-
:exc:`TypeError` if *altchars* is not a :term:`bytes-like object`.
74-
7572
.. versionchanged:: 3.15
7673
Added the *padded* and *wrapcol* parameters.
7774

Lib/base64.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def b32decode(s, casefold=False, map01=None, *, padded=True, ignorechars=b''):
237237
# either L (el) or I (eye).
238238
if map01 is not None:
239239
map01 = _bytes_from_decode_data(map01)
240-
assert len(map01) == 1, repr(map01)
241240
s = s.translate(bytes.maketrans(b'01', b'O' + map01))
242241
if casefold:
243242
s = s.upper()

Lib/test/test_base64.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,11 @@ def test_b32decode_map01(self):
607607
self.assertRaises(binascii.Error, base64.b32decode, b'M1O23456')
608608
self.assertRaises(binascii.Error, base64.b32decode, b'ML023456')
609609
self.assertRaises(binascii.Error, base64.b32decode, b'MI023456')
610+
self.assertRaises(ValueError, base64.b32decode, b'', map01=b'')
611+
self.assertRaises(ValueError, base64.b32decode, b'', map01=b'LI')
612+
self.assertRaises(TypeError, base64.b32decode, b'', map01=0)
613+
eq(base64.b32decode(b'MLO23456', map01=None), res_L)
614+
self.assertRaises(binascii.Error, base64.b32decode, b'M1023456', map01=None)
610615

611616
data = b'M1023456'
612617
data_str = data.decode('ascii')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`base64.b32encode` now always raises :exc:`ValueError` instead of
2+
:exc:`AssertionError` for the value of *map01* with invalid length.

0 commit comments

Comments
 (0)