Skip to content

Commit ea2c65d

Browse files
gh-146431: Add the wrapcol and ignorechars parameters for more Base XX codec
Add the wrapcol parameter to base64 functions b16encode(), b32encode(), b32hexencode(), b85encode() and z85encode(), and binascii functions b2a_base32() and b2a_base85(). Add the ignorechars parameter to base64 functions b16decode(), b32decode(), b32hexdecode(), b85decode() and z85decode(), and binascii functions a2b_hex(), unhexlify(), a2b_base32() and a2b_base85().
1 parent 9343518 commit ea2c65d

File tree

9 files changed

+800
-251
lines changed

9 files changed

+800
-251
lines changed

Doc/library/base64.rst

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,20 @@ POST request.
146146
Accepting the ``+`` and ``/`` characters is now deprecated.
147147

148148

149-
.. function:: b32encode(s)
149+
.. function:: b32encode(s, *, wrapcol=0)
150150

151151
Encode the :term:`bytes-like object` *s* using Base32 and return the
152152
encoded :class:`bytes`.
153153

154+
If *wrapcol* is non-zero, insert a newline (``b'\n'``) character
155+
after at most every *wrapcol* characters.
156+
If *wrapcol* is zero (default), do not add any newlines.
157+
158+
.. versionchanged:: next
159+
Added the *wrapcol* parameter.
160+
154161

155-
.. function:: b32decode(s, casefold=False, map01=None)
162+
.. function:: b32decode(s, casefold=False, map01=None, *, ignorechars=b'')
156163

157164
Decode the Base32 encoded :term:`bytes-like object` or ASCII string *s* and
158165
return the decoded :class:`bytes`.
@@ -168,20 +175,29 @@ POST request.
168175
digit 0 is always mapped to the letter O). For security purposes the default is
169176
``None``, so that 0 and 1 are not allowed in the input.
170177

178+
*ignorechars* should be a :term:`bytes-like object` containing characters
179+
to ignore from the input.
180+
171181
A :exc:`binascii.Error` is raised if *s* is
172182
incorrectly padded or if there are non-alphabet characters present in the
173183
input.
174184

185+
.. versionchanged:: next
186+
Added the *ignorechars* parameter.
187+
175188

176-
.. function:: b32hexencode(s)
189+
.. function:: b32hexencode(s, *, wrapcol=0)
177190

178191
Similar to :func:`b32encode` but uses the Extended Hex Alphabet, as defined in
179192
:rfc:`4648`.
180193

181194
.. versionadded:: 3.10
182195

196+
.. versionchanged:: next
197+
Added the *wrapcol* parameter.
198+
183199

184-
.. function:: b32hexdecode(s, casefold=False)
200+
.. function:: b32hexdecode(s, casefold=False, *, ignorechars=b'')
185201

186202
Similar to :func:`b32decode` but uses the Extended Hex Alphabet, as defined in
187203
:rfc:`4648`.
@@ -193,14 +209,24 @@ POST request.
193209

194210
.. versionadded:: 3.10
195211

212+
.. versionchanged:: next
213+
Added the *ignorechars* parameter.
214+
196215

197-
.. function:: b16encode(s)
216+
.. function:: b16encode(s, *, wrapcol=0)
198217

199218
Encode the :term:`bytes-like object` *s* using Base16 and return the
200219
encoded :class:`bytes`.
201220

221+
If *wrapcol* is non-zero, insert a newline (``b'\n'``) character
222+
after at most every *wrapcol* characters.
223+
If *wrapcol* is zero (default), do not add any newlines.
224+
225+
.. versionchanged:: next
226+
Added the *wrapcol* parameter.
202227

203-
.. function:: b16decode(s, casefold=False)
228+
229+
.. function:: b16decode(s, casefold=False, *, ignorechars=b'')
204230

205231
Decode the Base16 encoded :term:`bytes-like object` or ASCII string *s* and
206232
return the decoded :class:`bytes`.
@@ -209,10 +235,17 @@ POST request.
209235
lowercase alphabet is acceptable as input. For security purposes, the default
210236
is ``False``.
211237

238+
*ignorechars* should be a :term:`bytes-like object` containing characters
239+
to ignore from the input.
240+
212241
A :exc:`binascii.Error` is raised if *s* is
213242
incorrectly padded or if there are non-alphabet characters present in the
214243
input.
215244

245+
.. versionchanged:: next
246+
Added the *ignorechars* parameter.
247+
248+
216249
.. _base64-base-85:
217250

218251
Base85 Encodings
@@ -277,27 +310,40 @@ Refer to the documentation of the individual functions for more information.
277310
.. versionadded:: 3.4
278311

279312

280-
.. function:: b85encode(b, pad=False)
313+
.. function:: b85encode(b, pad=False, *, wrapcol=0)
281314

282315
Encode the :term:`bytes-like object` *b* using base85 (as used in e.g.
283316
git-style binary diffs) and return the encoded :class:`bytes`.
284317

285318
If *pad* is true, the input is padded with ``b'\0'`` so its length is a
286319
multiple of 4 bytes before encoding.
287320

321+
If *wrapcol* is non-zero, insert a newline (``b'\n'``) character
322+
after at most every *wrapcol* characters.
323+
If *wrapcol* is zero (default), do not add any newlines.
324+
288325
.. versionadded:: 3.4
289326

327+
.. versionchanged:: next
328+
Added the *wrapcol* parameter.
290329

291-
.. function:: b85decode(b)
330+
331+
.. function:: b85decode(b, *, ignorechars=b'')
292332

293333
Decode the base85-encoded :term:`bytes-like object` or ASCII string *b* and
294334
return the decoded :class:`bytes`. Padding is implicitly removed, if
295335
necessary.
296336

337+
*ignorechars* should be a :term:`bytes-like object` containing characters
338+
to ignore from the input.
339+
297340
.. versionadded:: 3.4
298341

342+
.. versionchanged:: next
343+
Added the *ignorechars* parameter.
344+
299345

300-
.. function:: z85encode(s, pad=False)
346+
.. function:: z85encode(s, pad=False, *, wrapcol=0)
301347

302348
Encode the :term:`bytes-like object` *s* using Z85 (as used in ZeroMQ)
303349
and return the encoded :class:`bytes`. See `Z85 specification
@@ -306,20 +352,33 @@ Refer to the documentation of the individual functions for more information.
306352
If *pad* is true, the input is padded with ``b'\0'`` so its length is a
307353
multiple of 4 bytes before encoding.
308354

355+
If *wrapcol* is non-zero, insert a newline (``b'\n'``) character
356+
after at most every *wrapcol* characters.
357+
If *wrapcol* is zero (default), do not add any newlines.
358+
309359
.. versionadded:: 3.13
310360

311361
.. versionchanged:: 3.15
312362
The *pad* parameter was added.
313363

364+
.. versionchanged:: next
365+
Added the *wrapcol* parameter.
366+
314367

315-
.. function:: z85decode(s)
368+
.. function:: z85decode(s, *, ignorechars=b'')
316369

317370
Decode the Z85-encoded :term:`bytes-like object` or ASCII string *s* and
318371
return the decoded :class:`bytes`. See `Z85 specification
319372
<https://rfc.zeromq.org/spec/32/>`_ for more information.
320373

374+
*ignorechars* should be a :term:`bytes-like object` containing characters
375+
to ignore from the input.
376+
321377
.. versionadded:: 3.13
322378

379+
.. versionchanged:: next
380+
Added the *ignorechars* parameter.
381+
323382

324383
.. _base64-legacy:
325384

Doc/library/binascii.rst

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ The :mod:`!binascii` module defines the following functions:
101101
Added the *alphabet* and *wrapcol* parameters.
102102

103103

104-
.. function:: a2b_ascii85(string, /, *, foldspaces=False, adobe=False, ignorechars=b"")
104+
.. function:: a2b_ascii85(string, /, *, foldspaces=False, adobe=False, ignorechars=b'')
105105

106106
Convert Ascii85 data back to binary and return the binary data.
107107

@@ -151,7 +151,7 @@ The :mod:`!binascii` module defines the following functions:
151151
.. versionadded:: 3.15
152152

153153

154-
.. function:: a2b_base85(string, /, *, alphabet=BASE85_ALPHABET)
154+
.. function:: a2b_base85(string, /, *, alphabet=BASE85_ALPHABET, ignorechars=b'')
155155

156156
Convert Base85 data back to binary and return the binary data.
157157
More than one line may be passed at a time.
@@ -164,26 +164,33 @@ The :mod:`!binascii` module defines the following functions:
164164
Optional *alphabet* must be a :class:`bytes` object of length 85 which
165165
specifies an alternative alphabet.
166166

167+
*ignorechars* should be a :term:`bytes-like object` containing characters
168+
to ignore from the input.
169+
167170
Invalid Base85 data will raise :exc:`binascii.Error`.
168171

169172
.. versionadded:: 3.15
170173

171174

172-
.. function:: b2a_base85(data, /, *, alphabet=BASE85_ALPHABET, pad=False)
175+
.. function:: b2a_base85(data, /, *, alphabet=BASE85_ALPHABET, wrapcol=0, pad=False)
173176

174177
Convert binary data to a line of ASCII characters in Base85 coding.
175178
The return value is the converted line.
176179

177180
Optional *alphabet* must be a :term:`bytes-like object` of length 85 which
178181
specifies an alternative alphabet.
179182

183+
If *wrapcol* is non-zero, insert a newline (``b'\n'``) character
184+
after at most every *wrapcol* characters.
185+
If *wrapcol* is zero (default), do not insert any newlines.
186+
180187
If *pad* is true, the input is padded with ``b'\0'`` so its length is a
181188
multiple of 4 bytes before encoding.
182189

183190
.. versionadded:: 3.15
184191

185192

186-
.. function:: a2b_base32(string, /, *, alphabet=BASE32_ALPHABET)
193+
.. function:: a2b_base32(string, /, *, alphabet=BASE32_ALPHABET, ignorechars=b'')
187194

188195
Convert base32 data back to binary and return the binary data.
189196

@@ -201,18 +208,28 @@ The :mod:`!binascii` module defines the following functions:
201208
Optional *alphabet* must be a :class:`bytes` object of length 32 which
202209
specifies an alternative alphabet.
203210

211+
*ignorechars* should be a :term:`bytes-like object` containing characters
212+
to ignore from the input.
213+
If *ignorechars* contains the pad character ``'='``, the pad characters
214+
presented before the end of the encoded data and the excess pad characters
215+
will be ignored.
216+
204217
Invalid base32 data will raise :exc:`binascii.Error`.
205218

206219
.. versionadded:: next
207220

208-
.. function:: b2a_base32(data, /, *, alphabet=BASE32_ALPHABET)
221+
.. function:: b2a_base32(data, /, *, alphabet=BASE32_ALPHABET, wrapcol=0)
209222

210223
Convert binary data to a line of ASCII characters in base32 coding,
211224
as specified in :rfc:`4648`. The return value is the converted line.
212225

213226
Optional *alphabet* must be a :term:`bytes-like object` of length 32 which
214227
specifies an alternative alphabet.
215228

229+
If *wrapcol* is non-zero, insert a newline (``b'\n'``) character
230+
after at most every *wrapcol* characters.
231+
If *wrapcol* is zero (default), do not insert any newlines.
232+
216233
.. versionadded:: next
217234

218235
.. function:: a2b_qp(data, header=False)
@@ -288,18 +305,25 @@ The :mod:`!binascii` module defines the following functions:
288305
.. versionchanged:: 3.8
289306
The *sep* and *bytes_per_sep* parameters were added.
290307

291-
.. function:: a2b_hex(hexstr)
292-
unhexlify(hexstr)
308+
.. function:: a2b_hex(hexstr, *, ignorechars=b'')
309+
unhexlify(hexstr, *, ignorechars=b'')
293310
294311
Return the binary data represented by the hexadecimal string *hexstr*. This
295312
function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
296313
of hexadecimal digits (which can be upper or lower case), otherwise an
297314
:exc:`Error` exception is raised.
298315

316+
*ignorechars* should be a :term:`bytes-like object` containing characters
317+
to ignore from the input.
318+
299319
Similar functionality (accepting only text string arguments, but more
300320
liberal towards whitespace) is also accessible using the
301321
:meth:`bytes.fromhex` class method.
302322

323+
.. versionchanged:: next
324+
Added the *ignorechars* parameter.
325+
326+
303327
.. exception:: Error
304328

305329
Exception raised on errors. These are usually programming errors.

Doc/whatsnew/3.15.rst

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -635,16 +635,28 @@ base64
635635
* Added the *pad* parameter in :func:`~base64.z85encode`.
636636
(Contributed by Hauke Dämpfling in :gh:`143103`.)
637637

638-
* Added the *wrapcol* parameter in :func:`~base64.b64encode`.
639-
(Contributed by Serhiy Storchaka in :gh:`143214`.)
638+
* Added the *wrapcol* parameter in :func:`~base64.b16encode`,
639+
:func:`~base64.b32encode`, :func:`~base64.b32hexencode`,
640+
:func:`~base64.b64encode`, :func:`~base64.b85encode`, and
641+
:func:`~base64.z85encode`.
642+
(Contributed by Serhiy Storchaka in :gh:`143214` and :gh:`xxxxxx`.)
640643

641-
* Added the *ignorechars* parameter in :func:`~base64.b64decode`.
642-
(Contributed by Serhiy Storchaka in :gh:`144001`.)
644+
* Added the *ignorechars* parameter in :func:`~base64.b16decode`,
645+
:func:`~base64.b32decode`, :func:`~base64.b32hexdecode`,
646+
:func:`~base64.b64decode`, :func:`~base64.b85decode`, and
647+
:func:`~base64.z85decode`.
648+
(Contributed by Serhiy Storchaka in :gh:`144001` and :gh:`xxxxxx`.)
643649

644650

645651
binascii
646652
--------
647653

654+
* Added functions for Base32 encoding:
655+
656+
- :func:`~binascii.b2a_base32` and :func:`~binascii.a2b_base32`
657+
658+
(Contributed by James Seo in :gh:`146192`.)
659+
648660
* Added functions for Ascii85, Base85, and Z85 encoding:
649661

650662
- :func:`~binascii.b2a_ascii85` and :func:`~binascii.a2b_ascii85`
@@ -659,14 +671,9 @@ binascii
659671
:func:`~binascii.a2b_base64`.
660672
(Contributed by Serhiy Storchaka in :gh:`145980`.)
661673

662-
* Added the *ignorechars* parameter in :func:`~binascii.a2b_base64`.
663-
(Contributed by Serhiy Storchaka in :gh:`144001`.)
664-
665-
* Added functions for Base32 encoding:
666-
667-
- :func:`~binascii.b2a_base32` and :func:`~binascii.a2b_base32`
668-
669-
(Contributed by James Seo in :gh:`146192`.)
674+
* Added the *ignorechars* parameter in :func:`~binascii.a2b_hex`,
675+
:func:`~binascii.unhexlify`, and :func:`~binascii.a2b_base64`.
676+
(Contributed by Serhiy Storchaka in :gh:`144001` and :gh:`xxxxxx`.)
670677

671678

672679
calendar

0 commit comments

Comments
 (0)