Skip to content

Commit a9a7d26

Browse files
committed
Address reviewer feedback
- Update docs to refer to "Base 32" and "Base32" - Update docs to better explain `binascii.a2b_base32()` - Inline helper function in `base64` - Add forgotten tests for presence of alphabet module globals
1 parent bf1308f commit a9a7d26

4 files changed

Lines changed: 26 additions & 22 deletions

File tree

Doc/library/binascii.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,21 @@ The :mod:`!binascii` module defines the following functions:
182182

183183
.. versionadded:: 3.15
184184

185+
185186
.. function:: a2b_base32(string, /, *, alphabet=BASE32_ALPHABET)
186187

187188
Convert base32 data back to binary and return the binary data.
188189

189-
Valid base32 data:
190+
Valid base32 data contains characters from the base32 alphabet specified
191+
in :rfc:`4648` in groups of eight (if necessary, the final group is padded
192+
to eight characters with ``=``). Each group encodes 40 bits of binary data
193+
in the range from ``0`` to ``2 ** 40 - 1``, inclusive.
190194

191-
* Conforms to :rfc:`4648`.
192-
* Contains only characters from the base32 alphabet.
193-
* Contains no excess data after padding (including excess padding, newlines, etc.).
194-
* Does not start with padding.
195+
.. note::
196+
By default, this function does not map lowercase characters (which are
197+
invalid in standard base32) to their uppercase counterparts, nor does
198+
it contextually map ``0`` to ``O`` and ``1`` to ``I``/``L`` as
199+
:rfc:`4648` allows.
195200

196201
Optional *alphabet* must be a :class:`bytes` object of length 32 which
197202
specifies an alternative alphabet.
@@ -202,7 +207,7 @@ The :mod:`!binascii` module defines the following functions:
202207

203208
.. function:: b2a_base32(data, /, *, alphabet=BASE32_ALPHABET)
204209

205-
Convert binary data to a line(s) of ASCII characters in base32 coding,
210+
Convert binary data to a line of ASCII characters in base32 coding,
206211
as specified in :rfc:`4648`. The return value is the converted line.
207212

208213
Optional *alphabet* must be a :term:`bytes-like object` of length 32 which
@@ -356,13 +361,13 @@ The :mod:`!binascii` module defines the following functions:
356361

357362
.. data:: BASE32_ALPHABET
358363

359-
The base32 alphabet according to :rfc:`4648`.
364+
The Base 32 alphabet according to :rfc:`4648`.
360365

361366
.. versionadded:: next
362367

363368
.. data:: BASE32HEX_ALPHABET
364369

365-
The "Extended Hex" base32hex alphabet according to :rfc:`4648`.
370+
The "Extended Hex" Base 32 alphabet according to :rfc:`4648`.
366371

367372
.. versionadded:: next
368373

Lib/base64.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ def urlsafe_b64decode(s):
207207
0 and 1 are not allowed in the input.
208208
'''
209209

210-
def _b32decode_prepare(s, casefold=False, map01=None):
210+
def b32encode(s):
211+
return binascii.b2a_base32(s)
212+
b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32')
213+
214+
def b32decode(s, casefold=False, map01=None):
211215
s = _bytes_from_decode_data(s)
212-
if len(s) % 8:
213-
raise binascii.Error('Incorrect padding')
214216
# Handle section 2.4 zero and one mapping. The flag map01 will be either
215217
# False, or the character to map the digit 1 (one) to. It should be
216218
# either L (el) or I (eye).
@@ -220,15 +222,6 @@ def _b32decode_prepare(s, casefold=False, map01=None):
220222
s = s.translate(bytes.maketrans(b'01', b'O' + map01))
221223
if casefold:
222224
s = s.upper()
223-
return s
224-
225-
226-
def b32encode(s):
227-
return binascii.b2a_base32(s)
228-
b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32')
229-
230-
def b32decode(s, casefold=False, map01=None):
231-
s = _b32decode_prepare(s, casefold, map01)
232225
return binascii.a2b_base32(s)
233226
b32decode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32',
234227
extra_args=_B32_DECODE_MAP01_DOCSTRING)
@@ -238,8 +231,10 @@ def b32hexencode(s):
238231
b32hexencode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32hex')
239232

240233
def b32hexdecode(s, casefold=False):
234+
s = _bytes_from_decode_data(s)
241235
# base32hex does not have the 01 mapping
242-
s = _b32decode_prepare(s, casefold)
236+
if casefold:
237+
s = s.upper()
243238
return binascii.a2b_base32(s, alphabet=binascii.BASE32HEX_ALPHABET)
244239
b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32hex',
245240
extra_args='')

Lib/test/test_binascii.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def test_constants(self):
7474
b'abcdefghijklmnopqrstuvwxyz'
7575
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
7676
b'.-:+=^!/*?&<>()[]{}@%$#')
77+
self.assertEqual(binascii.BASE32_ALPHABET,
78+
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')
79+
self.assertEqual(binascii.BASE32HEX_ALPHABET,
80+
b'0123456789ABCDEFGHIJKLMNOPQRSTUV')
7781

7882
def test_functions(self):
7983
# Check presence of all functions

Modules/binascii.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ binascii.b2a_base32
16721672
*
16731673
alphabet: Py_buffer(c_default="{NULL, NULL}") = BASE32_ALPHABET
16741674
1675-
base32-code line of data.
1675+
Base32-code line of data.
16761676
[clinic start generated code]*/
16771677

16781678
static PyObject *

0 commit comments

Comments
 (0)