-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Update base64 3.13.5 #5872
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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: | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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: | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix PEP 8 formatting for function definition. The 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
Suggested change
🧰 Tools🪛 Flake8 (7.2.0)[error] 511-511: expected 2 blank lines, found 1 (E302) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix PEP 8 formatting and improve docstring consistency. The 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
Suggested change
🧰 Tools🪛 Flake8 (7.2.0)[error] 515-515: expected 2 blank lines, found 1 (E302) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
# 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. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
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 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:
The translation logic correctly handles the differences between Base85 and Z85 alphabets by mapping invalid Z85 characters to null bytes during decoding.
📝 Committable suggestion
🧰 Tools
🪛 Flake8 (7.2.0)
[error] 500-500: expected 2 blank lines after class or function definition, found 1
(E305)
🤖 Prompt for AI Agents