Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-145968: fix base64.b64decode altchars translation in specific cases#145969

Merged
serhiy-storchaka merged 3 commits intopython:mainfrom
mayeut:base64-bad-translation
Mar 15, 2026
Merged

gh-145968: fix base64.b64decode altchars translation in specific cases#145969
serhiy-storchaka merged 3 commits intopython:mainfrom
mayeut:base64-bad-translation

Conversation

@mayeut
Copy link
Contributor

@mayeutmayeut commentedMar 15, 2026
edited by bedevere-appbot
Loading

Whenaltchars overlaps with the standard ones, the translation does not always yield to the expected outcome.
This updatesbytes.maketrans arguments to take those overlap cases into account.

…c casesWhen `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.
@python-cla-bot
Copy link

python-cla-botbot commentedMar 15, 2026
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@mayeut
Copy link
ContributorAuthor

cc@serhiy-storchaka

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)

Choose a reason for hiding this comment

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

It can be simply:

Suggested change
altchars_out= (
altchars[0]ifaltchars[0]notinb'+/'elsealtchars[1],
altchars[1]ifaltchars[1]notinb'+/'elsealtchars[0],
)
trans_in=bytearray(altchars)
trans_out=bytearray(b'+/')
forb,b_outinzip(b'+/',altchars_out):
ifbnotinaltchars:
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'+/')))

Copy link
ContributorAuthor

@mayeutmayeutMar 15, 2026
edited
Loading

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

You are right. What about the following?

trans_in=altcharstrans_out=b'+/'ifb'+'notinaltcharsandb'-'notinaltchars:trans_in+=b'+/'trans_out+=altcharselifb'+'notinaltcharsorb'-'notinaltchars: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.

mayeutand others added2 commitsMarch 15, 2026 11:36
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchakaserhiy-storchaka merged commitec5e3a5 intopython:mainMar 15, 2026
51 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@mayeut@serhiy-storchaka

[8]ページ先頭

©2009-2026 Movatter.jp