gh-144001: Add ignorechars parameter to Base64 decoder by moneebullah25 · Pull Request #144009 · python/cpython
Summary
- Add the
ignorecharsparameter tobinascii.a2b_base64()andbase64.b64decode() - When provided, only characters in this set will be silently ignored during decoding
- Other non-base64 characters will cause a
binascii.Error - Similar to the existing
ignorecharsparameter inbase64.a85decode()
This addresses the feature request in #144001 where the existing strict_mode/validate parameters are "all-or-nothing" - either all non-alphabet characters are rejected, or all are ignored. The new ignorechars parameter allows selective filtering (e.g., ignoring whitespace while rejecting other invalid characters).
API
# Ignore only whitespace, reject other invalid chars base64.b64decode(b'YWJj\n', ignorechars=b'\n') # OK: b'abc' base64.b64decode(b'YWJj!', ignorechars=b'\n') # Error # Strict: reject all non-base64 base64.b64decode(b'YWJj\n', ignorechars=b'') # Error # Default behavior unchanged base64.b64decode(b'YWJj\n') # OK: b'abc' (ignores all)
Closes #144001