@@ -45,14 +45,17 @@ def _bytes_from_decode_data(s):
4545
4646# Base64 encoding/decoding uses binascii
4747
48- def b64encode (s , altchars = None ):
48+ def b64encode (s , altchars = None , * , wrapcol = 0 ):
4949 """Encode the bytes-like object s using Base64 and return a bytes object.
5050
5151 Optional altchars should be a byte string of length 2 which specifies an
5252 alternative alphabet for the '+' and '/' characters. This allows an
5353 application to e.g. generate url or filesystem safe Base64 strings.
54+
55+ If wrapcol is non-zero, the output will be represented in lines of
56+ no more than wrapcol characters each, separated by a newline character.
5457 """
55- encoded = binascii .b2a_base64 (s , newline = False )
58+ encoded = binascii .b2a_base64 (s , wrapcol = wrapcol , newline = False )
5659 if altchars is not None :
5760 assert len (altchars ) == 2 , repr (altchars )
5861 return encoded .translate (bytes .maketrans (b'+/' , altchars ))
@@ -327,9 +330,8 @@ def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
327330 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
328331 feature is not supported by the "standard" Adobe encoding.
329332
330- wrapcol controls whether the output should have newline (b'\\ n') characters
331- added to it. If this is non-zero, each output line will be at most this
332- many characters long, excluding the trailing newline.
333+ If wrapcol is non-zero, the output will be represented in lines of
334+ no more than wrapcol characters each, separated by a newline character.
333335
334336 pad controls whether the input is padded to a multiple of 4 before
335337 encoding. Note that the btoa implementation always pads.
@@ -566,11 +568,10 @@ def encodebytes(s):
566568 """Encode a bytestring into a bytes object containing multiple lines
567569 of base-64 data."""
568570 _input_type_check (s )
569- pieces = []
570- for i in range (0 , len (s ), MAXBINSIZE ):
571- chunk = s [i : i + MAXBINSIZE ]
572- pieces .append (binascii .b2a_base64 (chunk ))
573- return b"" .join (pieces )
571+ result = binascii .b2a_base64 (s , wrapcol = MAXLINESIZE )
572+ if result == b'\n ' :
573+ return b''
574+ return result
574575
575576
576577def decodebytes (s ):
0 commit comments