https://github.com/python/cpython/commit/ec5e3a5a073507089b0b5908a5a298f8845bb2e4
commit: ec5e3a5a073507089b0b5908a5a298f8845bb2e4
branch: main
author: Matthieu Darbois <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-03-15T19:46:44+02:00
summary:
gh-145968: Fix base64.b64decode altchars translation in specific cases
(GH-145969)
When altchars overlaps with the standard ones, the translation does not
always yield to the expected outcome.
files:
A Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst
M Lib/base64.py
M Lib/test/test_base64.py
diff --git a/Lib/base64.py b/Lib/base64.py
index 36688ce43917ce..dcfcbcc95a39be 100644
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -100,7 +100,13 @@ def b64decode(s, altchars=None, validate=_NOT_SPECIFIED,
*, ignorechars=_NOT_SPE
break
s = s.translate(bytes.maketrans(altchars, b'+/'))
else:
- trans = bytes.maketrans(b'+/' + altchars, altchars + b'+/')
+ trans_in = set(b'+/') - set(altchars)
+ if len(trans_in) == 2:
+ # we can't use the reqult of unordered sets here
+ trans = bytes.maketrans(altchars + b'+/', b'+/' + altchars)
+ else:
+ trans = bytes.maketrans(altchars + bytes(trans_in),
+ b'+/' + bytes(set(altchars) -
set(b'+/')))
s = s.translate(trans)
ignorechars = ignorechars.translate(trans)
if ignorechars is _NOT_SPECIFIED:
diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py
index 69aa628db7c34c..9648624b267a54 100644
--- a/Lib/test/test_base64.py
+++ b/Lib/test/test_base64.py
@@ -293,6 +293,13 @@ def test_b64decode_altchars(self):
eq(base64.b64decode(data_str, altchars=altchars_str), res)
eq(base64.b64decode(data, altchars=altchars, ignorechars=b'\n'),
res)
+ eq(base64.b64decode(b'/----', altchars=b'-+', ignorechars=b'/'),
b'\xfb\xef\xbe')
+ eq(base64.b64decode(b'/----', altchars=b'+-', ignorechars=b'/'),
b'\xff\xff\xff')
+ eq(base64.b64decode(b'+----', altchars=b'-/', ignorechars=b'+'),
b'\xfb\xef\xbe')
+ eq(base64.b64decode(b'+----', altchars=b'/-', ignorechars=b'+'),
b'\xff\xff\xff')
+ eq(base64.b64decode(b'+/+/', altchars=b'/+', ignorechars=b''),
b'\xff\xef\xfe')
+ eq(base64.b64decode(b'/+/+', altchars=b'+/', ignorechars=b''),
b'\xff\xef\xfe')
+
self.assertRaises(ValueError, base64.b64decode, b'', altchars=b'+')
self.assertRaises(ValueError, base64.b64decode, b'', altchars=b'+/-')
self.assertRaises(ValueError, base64.b64decode, '', altchars='+')
diff --git
a/Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst
b/Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst
new file mode 100644
index 00000000000000..9eae1dc400838a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-03-15-10-17-51.gh-issue-145968.gZexry.rst
@@ -0,0 +1,2 @@
+Fix translation in :func:`base64.b64decode` when altchars overlaps with the
+standard ones.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]