gh-145968: fix base64.b64decode altchars translation in specific cases#145969
Merged
serhiy-storchaka merged 3 commits intopython:mainfrom Mar 15, 2026
Merged
gh-145968: fix base64.b64decode altchars translation in specific cases#145969serhiy-storchaka merged 3 commits intopython:mainfrom
serhiy-storchaka merged 3 commits intopython:mainfrom
Conversation
…c cases When `altchars` overlaps with the standard ones, the translation does not always yield to the expected outcome. This commit updates `bytes.maketrans` arguments to take those overlap cases into account.
Contributor
Author
Lib/base64.py
Outdated
Comment on lines
+103
to
+113
| altchars_out = ( | ||
| altchars[0] if altchars[0] not in b'+/' else altchars[1], | ||
| altchars[1] if altchars[1] not in b'+/' else altchars[0], | ||
| ) | ||
| trans_in = bytearray(altchars) | ||
| trans_out = bytearray(b'+/') | ||
| for b, b_out in zip(b'+/', altchars_out): | ||
| if b not in altchars: | ||
| trans_in.append(b) | ||
| trans_out.append(b_out) | ||
| trans = bytes.maketrans(trans_in, trans_out) |
Member
There was a problem hiding this comment.
It can be simply:
Suggested change
| altchars_out = ( | |
| altchars[0] if altchars[0] not in b'+/' else altchars[1], | |
| altchars[1] if altchars[1] not in b'+/' else altchars[0], | |
| ) | |
| trans_in = bytearray(altchars) | |
| trans_out = bytearray(b'+/') | |
| for b, b_out in zip(b'+/', altchars_out): | |
| if b not in altchars: | |
| trans_in.append(b) | |
| trans_out.append(b_out) | |
| trans = bytes.maketrans(trans_in, trans_out) | |
| trans = bytes.maketrans(altchars + bytes(set(b'+/') - set(altchars)), | |
| b'+/' + bytes(set(altchars) - set(b'+/'))) |
Contributor
Author
There was a problem hiding this comment.
I commited the suggestion but after further reflection, the sets are unordered and thus the translation might not be correct when altchars and standard ones do not overlap (although this might be hard to catch in tests ?).
I'll fix that later
Member
There was a problem hiding this comment.
You are right. What about the following?
trans_in = altchars
trans_out = b'+/'
if b'+' not in altchars and b'-' not in altchars:
trans_in += b'+/'
trans_out += altchars
elif b'+' not in altchars or b'-' not in altchars:
trans_in += bytes(set(b'+/') - set(altchars))
trans_out += bytes(set(altchars) - set(b'+/'))You can also return your original code. #145981 also fixes this issue, so this code can be replaced, but your tests will remain.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When
altcharsoverlaps with the standard ones, the translation does not always yield to the expected outcome.This updates
bytes.maketransarguments to take those overlap cases into account.