-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-101178: Add Ascii85, base85, and Z85 support to binascii #102753
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
Open
kangtastic
wants to merge
12
commits into
python:main
Choose a base branch
from
kangtastic:gh-101178-rework-base85
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
05ae5ad
Add Ascii85, base85, and Z85 support to binascii
kangtastic aa06c5d
Restore base64.py
kangtastic 6377440
Create _base64 module with wrappers for accelerated functions
kangtastic 6c0e4a3
Test both Python and C codepaths in base64
kangtastic ce4773c
Match behavior between Python and C base 85 functions
kangtastic 4072e3b
Add Z85 tests to binascii
kangtastic bc9217f
Update generated files
kangtastic 2c40ba0
Avoid importing functools
kangtastic fd9eaf7
Avoid circular import in _base64
kangtastic 4746d18
Do not use a decorator for changing exception type
kangtastic d075593
Test Python and C codepaths in base64 using mixins
kangtastic 6d65fec
Remove leading underscore from functions in private module
kangtastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""C accelerator wrappers for originally pure-Python parts of base64.""" | ||
|
||
from binascii import Error, a2b_ascii85, a2b_base85, b2a_ascii85, b2a_base85 | ||
|
||
|
||
# Base 85 functions in base64 silently convert input to bytes. | ||
# Copy the conversion logic from base64 to avoid circular imports. | ||
|
||
bytes_types = (bytes, bytearray) # Types acceptable as binary data | ||
|
||
|
||
def _bytes_from_decode_data(s): | ||
if isinstance(s, str): | ||
try: | ||
return s.encode('ascii') | ||
except UnicodeEncodeError: | ||
raise ValueError('string argument should contain only ASCII characters') | ||
if isinstance(s, bytes_types): | ||
return s | ||
try: | ||
return memoryview(s).tobytes() | ||
except TypeError: | ||
raise TypeError("argument should be a bytes-like object or ASCII " | ||
"string, not %r" % s.__class__.__name__) from None | ||
|
||
|
||
def _bytes_from_encode_data(b): | ||
return b if isinstance(b, bytes_types) else memoryview(b).tobytes() | ||
|
||
|
||
# Functions in binascii raise binascii.Error instead of ValueError. | ||
|
||
def _a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False): | ||
b = _bytes_from_encode_data(b) | ||
try: | ||
return b2a_ascii85(b, fold_spaces=foldspaces, | ||
wrap=adobe, width=wrapcol, pad=pad) | ||
except Error as e: | ||
raise ValueError(e) from None | ||
|
||
|
||
def _a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v'): | ||
b = _bytes_from_decode_data(b) | ||
try: | ||
return a2b_ascii85(b, fold_spaces=foldspaces, | ||
wrap=adobe, ignore=ignorechars) | ||
except Error as e: | ||
raise ValueError(e) from None | ||
|
||
def _b85encode(b, pad=False): | ||
b = _bytes_from_encode_data(b) | ||
try: | ||
return b2a_base85(b, pad=pad, newline=False) | ||
except Error as e: | ||
raise ValueError(e) from None | ||
|
||
|
||
def _b85decode(b): | ||
b = _bytes_from_decode_data(b) | ||
try: | ||
return a2b_base85(b, strict_mode=True) | ||
except Error as e: | ||
raise ValueError(e) from None | ||
|
||
|
||
def _z85encode(s): | ||
s = _bytes_from_encode_data(s) | ||
try: | ||
return b2a_base85(s, newline=False, z85=True) | ||
except Error as e: | ||
raise ValueError(e) from None | ||
|
||
|
||
def _z85decode(s): | ||
s = _bytes_from_decode_data(s) | ||
try: | ||
return a2b_base85(s, strict_mode=True, z85=True) | ||
except Error as e: | ||
raise ValueError(e) from None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Given these are already in a private module, you can remove the prefix. That means the
_copy_attributes
function only needs to copy__doc__
, and__module__
can be set to the static'base64'
.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.
Done.