GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-145968: fix base64.b64decode altchars translation in specific cases by mayeut · Pull Request #145969 · python/cpython

Conversation

@mayeut

@mayeut mayeut commented Mar 15, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • Issue: base64.b64decode error raised on valid input #145968

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mayeut
pythongh-145968: fix base64.b64decode altchars translation in specifi…
d484023
…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.
@python-cla-bot

python-cla-bot Bot commented Mar 15, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot mentioned this pull request Mar 15, 2026
base64.b64decode error raised on valid input #145968
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Mar 15, 2026
@mayeut

mayeut commented Mar 15, 2026

Copy link
Copy Markdown
Contributor Author

cc @serhiy-storchaka

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Mar 15, 2026
View reviewed changes
Comment thread 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)

@serhiy-storchaka serhiy-storchaka Mar 15, 2026

Copy link
Copy Markdown
Member

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.

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'+/')))

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mayeut mayeut Mar 15, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Mar 15, 2026

Copy link
Copy Markdown
Member

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mayeut and others added 2 commits March 15, 2026 11:36
@mayeut @serhiy-storchaka
simplification from review
081d550
Co-authored-by: Serhiy Storchaka <[email protected]>
@mayeut
don't use the results of an unordered 2 element set
e956fe6
serhiy-storchaka
serhiy-storchaka approved these changes Mar 15, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

LGTM. 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Mar 15, 2026
@serhiy-storchaka
serhiy-storchaka merged commit ec5e3a5 into python:main Mar 15, 2026
51 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 15, 2026
@mayeut
mayeut deleted the base64-bad-translation branch March 29, 2026 17:21
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@mayeut @ljfp
pythongh-145968: Fix base64.b64decode altchars translation in specifi…
9534f5b
…c cases (pythonGH-145969)

When altchars overlaps with the standard ones, the translation does not
always yield to the expected outcome.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@mayeut @serhiy-storchaka
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.