gh-144001: Simplify Base64 decoding with altchars and ignorechars spe… · python/cpython@c3b61ef · GitHub
Skip to content

Commit c3b61ef

Browse files
gh-144001: Simplify Base64 decoding with altchars and ignorechars specified (GH-144324)
Treat "+" and "/" like other characters not in the alternative Base64 alphabet when both altchars and ignorechars are specified. E.g. discard them if they are not in altchars but are in ignorechars, and set error if they are not in altchars and not in ignorechars. Only emit warnings if ignorechars is not specified.
1 parent 15c9f24 commit c3b61ef

3 files changed

Lines changed: 41 additions & 50 deletions

File tree

Doc/library/base64.rst

Lines changed: 5 additions & 5 deletions

Lib/base64.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,49 +89,37 @@ def b64decode(s, altchars=None, validate=_NOT_SPECIFIED, *, ignorechars=_NOT_SPE
8989
s = _bytes_from_decode_data(s)
9090
if validate is _NOT_SPECIFIED:
9191
validate = ignorechars is not _NOT_SPECIFIED
92-
if ignorechars is _NOT_SPECIFIED:
93-
ignorechars = b''
9492
badchar = None
95-
badchar_strict = False
9693
if altchars is not None:
9794
altchars = _bytes_from_decode_data(altchars)
9895
if len(altchars) != 2:
9996
raise ValueError(f'invalid altchars: {altchars!r}')
100-
for b in b'+/':
101-
if b not in altchars and b in s:
102-
if badchar is None:
103-
badchar = b
104-
if not validate:
105-
break
106-
if not isinstance(ignorechars, (bytes, bytearray)):
107-
ignorechars = memoryview(ignorechars).cast('B')
108-
if b not in ignorechars:
109-
badchar_strict = True
97+
if ignorechars is _NOT_SPECIFIED:
98+
for b in b'+/':
99+
if b not in altchars and b in s:
110100
badchar = b
111101
break
112-
s = s.translate(bytes.maketrans(altchars, b'+/'))
102+
s = s.translate(bytes.maketrans(altchars, b'+/'))
103+
else:
104+
trans = bytes.maketrans(b'+/' + altchars, altchars + b'+/')
105+
s = s.translate(trans)
106+
ignorechars = ignorechars.translate(trans)
107+
if ignorechars is _NOT_SPECIFIED:
108+
ignorechars = b''
113109
result = binascii.a2b_base64(s, strict_mode=validate,
114110
ignorechars=ignorechars)
115111
if badchar is not None:
116112
import warnings
117-
if badchar_strict:
113+
if validate:
118114
warnings.warn(f'invalid character {chr(badchar)!a} in Base64 data '
119115
f'with altchars={altchars!r} and validate=True '
120116
f'will be an error in future Python versions',
121117
DeprecationWarning, stacklevel=2)
122118
else:
123-
ignorechars = bytes(ignorechars)
124-
if ignorechars:
125-
warnings.warn(f'invalid character {chr(badchar)!a} in Base64 data '
126-
f'with altchars={altchars!r} '
127-
f'and ignorechars={ignorechars!r} '
128-
f'will be discarded in future Python versions',
129-
FutureWarning, stacklevel=2)
130-
else:
131-
warnings.warn(f'invalid character {chr(badchar)!a} in Base64 data '
132-
f'with altchars={altchars!r} and validate=False '
133-
f'will be discarded in future Python versions',
134-
FutureWarning, stacklevel=2)
119+
warnings.warn(f'invalid character {chr(badchar)!a} in Base64 data '
120+
f'with altchars={altchars!r} and validate=False '
121+
f'will be discarded in future Python versions',
122+
FutureWarning, stacklevel=2)
135123
return result
136124

137125

Lib/test/test_base64.py

Lines changed: 21 additions & 18 deletions

0 commit comments

Comments
 (0)