Skip to content

Update base64 3.13.5 #5872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions Lib/base64.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'b64encode', 'b64decode', 'b32encode', 'b32decode',
'b32hexencode', 'b32hexdecode', 'b16encode', 'b16decode',
# Base85 and Ascii85 encodings
'b85encode', 'b85decode', 'a85encode', 'a85decode',
'b85encode', 'b85decode', 'a85encode', 'a85decode', 'z85encode', 'z85decode',
# Standard Base64 encoding
'standard_b64encode', 'standard_b64decode',
# Some common Base64 alternatives. As referenced by RFC 3458, see thread
Expand Down Expand Up @@ -164,7 +164,6 @@ def urlsafe_b64decode(s):
_b32rev = {}

def _b32encode(alphabet, s):
global _b32tab2
# Delay the initialization of the table to not waste memory
# if the function is never called
if alphabet not in _b32tab2:
Expand Down Expand Up @@ -200,7 +199,6 @@ def _b32encode(alphabet, s):
return bytes(encoded)

def _b32decode(alphabet, s, casefold=False, map01=None):
global _b32rev
# Delay the initialization of the table to not waste memory
# if the function is never called
if alphabet not in _b32rev:
Expand Down Expand Up @@ -334,7 +332,7 @@ def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):

wrapcol controls whether the output should have newline (b'\\n') characters
added to it. If this is non-zero, each output line will be at most this
many characters long.
many characters long, excluding the trailing newline.

pad controls whether the input is padded to a multiple of 4 before
encoding. Note that the btoa implementation always pads.
Expand Down Expand Up @@ -499,6 +497,33 @@ def b85decode(b):
result = result[:-padding]
return result

_z85alphabet = (b'0123456789abcdefghijklmnopqrstuvwxyz'
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#')
# Translating b85 valid but z85 invalid chars to b'\x00' is required
# to prevent them from being decoded as b85 valid chars.
_z85_b85_decode_diff = b';_`|~'
_z85_decode_translation = bytes.maketrans(
_z85alphabet + _z85_b85_decode_diff,
_b85alphabet + b'\x00' * len(_z85_b85_decode_diff)
)
_z85_encode_translation = bytes.maketrans(_b85alphabet, _z85alphabet)
Comment on lines +500 to +509
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Fix PEP 8 formatting and verify translation table logic.

The Z85 alphabet definition and translation tables look correct, but there are formatting issues that need to be addressed.

Apply this diff to fix the formatting issues:

+
 _z85alphabet = (b'0123456789abcdefghijklmnopqrstuvwxyz'
                 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#')
 # Translating b85 valid but z85 invalid chars to b'\x00' is required
 # to prevent them from being decoded as b85 valid chars.
 _z85_b85_decode_diff = b';_`|~'
 _z85_decode_translation = bytes.maketrans(
     _z85alphabet + _z85_b85_decode_diff,
     _b85alphabet + b'\x00' * len(_z85_b85_decode_diff)
 )
 _z85_encode_translation = bytes.maketrans(_b85alphabet, _z85alphabet)
+

The translation logic correctly handles the differences between Base85 and Z85 alphabets by mapping invalid Z85 characters to null bytes during decoding.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_z85alphabet = (b'0123456789abcdefghijklmnopqrstuvwxyz'
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#')
# Translating b85 valid but z85 invalid chars to b'\x00' is required
# to prevent them from being decoded as b85 valid chars.
_z85_b85_decode_diff = b';_`|~'
_z85_decode_translation = bytes.maketrans(
_z85alphabet + _z85_b85_decode_diff,
_b85alphabet + b'\x00' * len(_z85_b85_decode_diff)
)
_z85_encode_translation = bytes.maketrans(_b85alphabet, _z85alphabet)
_z85alphabet = (b'0123456789abcdefghijklmnopqrstuvwxyz'
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#')
# Translating b85 valid but z85 invalid chars to b'\x00' is required
# to prevent them from being decoded as b85 valid chars.
_z85_b85_decode_diff = b';_`|~'
_z85_decode_translation = bytes.maketrans(
_z85alphabet + _z85_b85_decode_diff,
_b85alphabet + b'\x00' * len(_z85_b85_decode_diff)
)
_z85_encode_translation = bytes.maketrans(_b85alphabet, _z85alphabet)
🧰 Tools
🪛 Flake8 (7.2.0)

[error] 500-500: expected 2 blank lines after class or function definition, found 1

(E305)

🤖 Prompt for AI Agents
In Lib/base64.py around lines 500 to 509, fix the PEP 8 formatting issues by
ensuring proper line breaks and indentation for the _z85alphabet definition and
the translation table assignments. Verify that the translation tables correctly
map the Z85 alphabet and the differing Base85 characters, maintaining the logic
that translates invalid Z85 characters to null bytes during decoding. Adjust the
code formatting without altering the existing translation logic.


def z85encode(s):
"""Encode bytes-like object b in z85 format and return a bytes object."""
return b85encode(s).translate(_z85_encode_translation)
Comment on lines +511 to +513
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix PEP 8 formatting for function definition.

The z85encode function implementation is correct but needs proper spacing.

Apply this diff to fix the formatting:

+
 def z85encode(s):
     """Encode bytes-like object b in z85 format and return a bytes object."""
     return b85encode(s).translate(_z85_encode_translation)
+
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def z85encode(s):
"""Encode bytes-like object b in z85 format and return a bytes object."""
return b85encode(s).translate(_z85_encode_translation)
def z85encode(s):
"""Encode bytes-like object b in z85 format and return a bytes object."""
return b85encode(s).translate(_z85_encode_translation)
🧰 Tools
🪛 Flake8 (7.2.0)

[error] 511-511: expected 2 blank lines, found 1

(E302)

🤖 Prompt for AI Agents
In Lib/base64.py around lines 511 to 513, the function definition for z85encode
lacks proper spacing according to PEP 8 standards. Add a blank line before the
function definition to separate it from preceding code or comments, ensuring
consistent vertical spacing and improving readability.


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

The result is returned as a bytes object.
"""
s = _bytes_from_decode_data(s)
s = s.translate(_z85_decode_translation)
try:
return b85decode(s)
except ValueError as e:
raise ValueError(e.args[0].replace('base85', 'z85')) from None
Comment on lines +515 to +525
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix PEP 8 formatting and improve docstring consistency.

The z85decode function implementation is correct but needs formatting fixes and a minor docstring improvement.

Apply this diff to fix the formatting and improve consistency:

+
 def z85decode(s):
-    """Decode the z85-encoded bytes-like object or ASCII string b
+    """Decode the z85-encoded bytes-like object or ASCII string s
 
     The result is returned as a bytes object.
     """
     s = _bytes_from_decode_data(s)
     s = s.translate(_z85_decode_translation)
     try:
         return b85decode(s)
     except ValueError as e:
         raise ValueError(e.args[0].replace('base85', 'z85')) from None

The error message translation approach is clever - it reuses the existing b85decode error handling while customizing the error messages for Z85 context.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def z85decode(s):
"""Decode the z85-encoded bytes-like object or ASCII string b
The result is returned as a bytes object.
"""
s = _bytes_from_decode_data(s)
s = s.translate(_z85_decode_translation)
try:
return b85decode(s)
except ValueError as e:
raise ValueError(e.args[0].replace('base85', 'z85')) from None
def z85decode(s):
"""Decode the z85-encoded bytes-like object or ASCII string s
The result is returned as a bytes object.
"""
s = _bytes_from_decode_data(s)
s = s.translate(_z85_decode_translation)
try:
return b85decode(s)
except ValueError as e:
raise ValueError(e.args[0].replace('base85', 'z85')) from None
🧰 Tools
🪛 Flake8 (7.2.0)

[error] 515-515: expected 2 blank lines, found 1

(E302)

🤖 Prompt for AI Agents
In Lib/base64.py around lines 515 to 525, fix the PEP 8 formatting issues by
ensuring proper indentation and spacing throughout the z85decode function. Also,
improve the docstring by correcting the description to be more consistent and
clear, for example, clarify the input type and output in the docstring format.
Make sure the function signature and body follow PEP 8 style guidelines,
including blank lines and line length.


# Legacy interface. This code could be cleaned up since I don't believe
# binascii has any line length limitations. It just doesn't seem worth it
# though. The files should be opened in binary mode.
Expand Down
90 changes: 89 additions & 1 deletion Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,40 @@ def test_b85encode(self):
self.check_other_types(base64.b85encode, b"www.python.org",
b'cXxL#aCvlSZ*DGca%T')

def test_z85encode(self):
eq = self.assertEqual

tests = {
b'': b'',
b'www.python.org': b'CxXl-AcVLsz/dgCA+t',
bytes(range(255)): b"""009c61o!#m2NH?C3>iWS5d]J*6CRx17-skh9337x"""
b"""ar.{NbQB=+c[cR@eg&FcfFLssg=mfIi5%2YjuU>)kTv.7l}6Nnnj=AD"""
b"""oIFnTp/ga?r8($2sxO*itWpVyu$0IOwmYv=xLzi%y&a6dAb/]tBAI+J"""
b"""CZjQZE0{D[FpSr8GOteoH(41EJe-<UKDCY&L:dM3N3<zjOsMmzPRn9P"""
b"""Q[%@^ShV!$TGwUeU^7HuW6^uKXvGh.YUh4]Z})[9-kP:p:JqPF+*1CV"""
b"""^9Zp<!yAd4/Xb0k*$*&A&nJXQ<MkK!>&}x#)cTlf[Bu8v].4}L}1:^-"""
b"""@qDP""",
b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"""
b"""0123456789!@#0^&*();:<>,. []{}""":
b"""vpA.SwObN*x>?B1zeKohADlbxB-}$ND3R+ylQTvjm[uizoh55PpF:[^"""
b"""q=D:$s6eQefFLssg=mfIi5@cEbqrBJdKV-ciY]OSe*aw7DWL""",
b'no padding..': b'zF{UpvpS[.zF7NO',
b'zero compression\x00\x00\x00\x00': b'Ds.bnay/tbAb]JhB7]Mg00000',
b'zero compression\x00\x00\x00': b'Ds.bnay/tbAb]JhB7]Mg0000',
b"""Boundary:\x00\x00\x00\x00""": b"""lt}0:wmoI7iSGcW00""",
b'Space compr: ': b'q/DePwGUG3ze:IRarR^H',
b'\xff': b'@@',
b'\xff'*2: b'%nJ',
b'\xff'*3: b'%nS9',
b'\xff'*4: b'%nSc0',
}

for data, res in tests.items():
eq(base64.z85encode(data), res)

self.check_other_types(base64.z85encode, b"www.python.org",
b'CxXl-AcVLsz/dgCA+t')

def test_a85decode(self):
eq = self.assertEqual

Expand Down Expand Up @@ -586,6 +620,7 @@ def test_a85decode(self):
eq(base64.a85decode(b'y+<Vd', foldspaces=True, adobe=False), b' '*7)
eq(base64.a85decode(b'y+<U', foldspaces=True, adobe=False), b' '*6)
eq(base64.a85decode(b'y+9', foldspaces=True, adobe=False), b' '*5)
eq(base64.a85decode(b'aaaaay', foldspaces=True), b'\xc9\x80\x0b@ ')

self.check_other_types(base64.a85decode, b'GB\\6`E-ZP=Df.1GEb>',
b"www.python.org")
Expand Down Expand Up @@ -625,6 +660,41 @@ def test_b85decode(self):
self.check_other_types(base64.b85decode, b'cXxL#aCvlSZ*DGca%T',
b"www.python.org")

def test_z85decode(self):
eq = self.assertEqual

tests = {
b'': b'',
b'CxXl-AcVLsz/dgCA+t': b'www.python.org',
b"""009c61o!#m2NH?C3>iWS5d]J*6CRx17-skh9337x"""
b"""ar.{NbQB=+c[cR@eg&FcfFLssg=mfIi5%2YjuU>)kTv.7l}6Nnnj=AD"""
b"""oIFnTp/ga?r8($2sxO*itWpVyu$0IOwmYv=xLzi%y&a6dAb/]tBAI+J"""
b"""CZjQZE0{D[FpSr8GOteoH(41EJe-<UKDCY&L:dM3N3<zjOsMmzPRn9P"""
b"""Q[%@^ShV!$TGwUeU^7HuW6^uKXvGh.YUh4]Z})[9-kP:p:JqPF+*1CV"""
b"""^9Zp<!yAd4/Xb0k*$*&A&nJXQ<MkK!>&}x#)cTlf[Bu8v].4}L}1:^-"""
b"""@qDP""": bytes(range(255)),
b"""vpA.SwObN*x>?B1zeKohADlbxB-}$ND3R+ylQTvjm[uizoh55PpF:[^"""
b"""q=D:$s6eQefFLssg=mfIi5@cEbqrBJdKV-ciY]OSe*aw7DWL""":
b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"""
b"""0123456789!@#0^&*();:<>,. []{}""",
b'zF{UpvpS[.zF7NO': b'no padding..',
b'Ds.bnay/tbAb]JhB7]Mg00000': b'zero compression\x00\x00\x00\x00',
b'Ds.bnay/tbAb]JhB7]Mg0000': b'zero compression\x00\x00\x00',
b"""lt}0:wmoI7iSGcW00""": b"""Boundary:\x00\x00\x00\x00""",
b'q/DePwGUG3ze:IRarR^H': b'Space compr: ',
b'@@': b'\xff',
b'%nJ': b'\xff'*2,
b'%nS9': b'\xff'*3,
b'%nSc0': b'\xff'*4,
}

for data, res in tests.items():
eq(base64.z85decode(data), res)
eq(base64.z85decode(data.decode("ascii")), res)

self.check_other_types(base64.z85decode, b'CxXl-AcVLsz/dgCA+t',
b'www.python.org')

def test_a85_padding(self):
eq = self.assertEqual

Expand Down Expand Up @@ -689,6 +759,8 @@ def test_a85decode_errors(self):
self.assertRaises(ValueError, base64.a85decode, b's8W', adobe=False)
self.assertRaises(ValueError, base64.a85decode, b's8W-', adobe=False)
self.assertRaises(ValueError, base64.a85decode, b's8W-"', adobe=False)
self.assertRaises(ValueError, base64.a85decode, b'aaaay',
foldspaces=True)

def test_b85decode_errors(self):
illegal = list(range(33)) + \
Expand All @@ -704,14 +776,30 @@ def test_b85decode_errors(self):
self.assertRaises(ValueError, base64.b85decode, b'|NsC')
self.assertRaises(ValueError, base64.b85decode, b'|NsC1')

def test_z85decode_errors(self):
illegal = list(range(33)) + \
list(b'"\',;_`|\\~') + \
list(range(128, 256))
for c in illegal:
with self.assertRaises(ValueError, msg=bytes([c])):
base64.z85decode(b'0000' + bytes([c]))

# b'\xff\xff\xff\xff' encodes to b'%nSc0', the following will overflow:
self.assertRaises(ValueError, base64.z85decode, b'%')
self.assertRaises(ValueError, base64.z85decode, b'%n')
self.assertRaises(ValueError, base64.z85decode, b'%nS')
self.assertRaises(ValueError, base64.z85decode, b'%nSc')
self.assertRaises(ValueError, base64.z85decode, b'%nSc1')

def test_decode_nonascii_str(self):
decode_funcs = (base64.b64decode,
base64.standard_b64decode,
base64.urlsafe_b64decode,
base64.b32decode,
base64.b16decode,
base64.b85decode,
base64.a85decode)
base64.a85decode,
base64.z85decode)
for f in decode_funcs:
self.assertRaises(ValueError, f, 'with non-ascii \xcb')

Expand Down
Loading