Skip to content

Commit febebea

Browse files
committed
Added pad argument to base64.z85encode
This makes it analogous to `a85encode` and `b85encode` and allows the user to more easily meet the Z85 specification, which requires input lengths to be a multiple of 4.
1 parent c4ab024 commit febebea

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

Doc/library/base64.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,20 @@ Refer to the documentation of the individual functions for more information.
267267
.. versionadded:: 3.4
268268

269269

270-
.. function:: z85encode(s)
270+
.. function:: z85encode(s, pad=False)
271271

272272
Encode the :term:`bytes-like object` *s* using Z85 (as used in ZeroMQ)
273273
and return the encoded :class:`bytes`. See `Z85 specification
274274
<https://rfc.zeromq.org/spec/32/>`_ for more information.
275275

276+
If *pad* is true, the input is padded with ``b'\0'`` so its length is a
277+
multiple of 4 bytes before encoding.
278+
276279
.. versionadded:: 3.13
277280

281+
.. versionchanged:: 3.15
282+
The *pad* argument was added.
283+
278284

279285
.. function:: z85decode(s)
280286

Lib/base64.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ def b85decode(b):
508508
)
509509
_z85_encode_translation = bytes.maketrans(_b85alphabet, _z85alphabet)
510510

511-
def z85encode(s):
511+
def z85encode(s, pad=False):
512512
"""Encode bytes-like object b in z85 format and return a bytes object."""
513-
return b85encode(s).translate(_z85_encode_translation)
513+
return b85encode(s, pad=pad).translate(_z85_encode_translation)
514514

515515
def z85decode(s):
516516
"""Decode the z85-encoded bytes-like object or ASCII string b

Lib/test/test_base64.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ def test_z85encode(self):
665665

666666
tests = {
667667
b'': b'',
668+
b'\x86\x4F\xD2\x6F\xB5\x59\xF7\x5B': b'HelloWorld',
668669
b'www.python.org': b'CxXl-AcVLsz/dgCA+t',
669670
bytes(range(255)): b"""009c61o!#m2NH?C3>iWS5d]J*6CRx17-skh9337x"""
670671
b"""ar.{NbQB=+c[cR@eg&FcfFLssg=mfIi5%2YjuU>)kTv.7l}6Nnnj=AD"""
@@ -840,6 +841,22 @@ def test_b85_padding(self):
840841
eq(base64.b85decode(b'czAet'), b"xxxx")
841842
eq(base64.b85decode(b'czAetcmMzZ'), b"xxxxx\x00\x00\x00")
842843

844+
def test_z85_padding(self):
845+
eq = self.assertEqual
846+
847+
eq(base64.z85encode(b"x", pad=True), b'CMmZz')
848+
eq(base64.z85encode(b"xx", pad=True), b'CZ6h*')
849+
eq(base64.z85encode(b"xxx", pad=True), b'CZaDk')
850+
eq(base64.z85encode(b"xxxx", pad=True), b'CZaET')
851+
eq(base64.z85encode(b"xxxxx", pad=True), b'CZaETCMmZz')
852+
853+
eq(base64.z85decode(b'CMmZz'), b"x\x00\x00\x00")
854+
eq(base64.z85decode(b'CZ6h*'), b"xx\x00\x00")
855+
eq(base64.z85decode(b'CZaDk'), b"xxx\x00")
856+
eq(base64.z85decode(b'CZaET'), b"xxxx")
857+
eq(base64.z85decode(b'CZaETCMmZz'), b"xxxxx\x00\x00\x00")
858+
859+
843860
def test_a85decode_errors(self):
844861
illegal = (set(range(32)) | set(range(118, 256))) - set(b' \t\n\r\v')
845862
for c in illegal:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ Lisandro Dalcin
418418
Darren Dale
419419
Andrew Dalke
420420
Lars Damerow
421+
Hauke Dämpfling
421422
Evan Dandrea
422423
Eric Daniel
423424
Scott David Daniels
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`base64.z85encode` now has a ``pad`` argument.

0 commit comments

Comments
 (0)