-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected nested exception with b64decode #129505
Comments
This is the intended error; the error message tells you exactly what's going on. (And the earlier The docs already say the function needs an “encoded bytes-like object or ASCII string”. Perhaps the validate section should be clarified, do you want to send a PR? |
I don't think the validate section is the issue (note this happens with validate=False). If this is intended behavior, I'd add something to the last sentence, like this: "May assert or raise a ValueError if the length of altchars is not 2 or the input is a string and contains non-ascii characters." Shall I send a PR for that? |
Or maybe better, as we shouldn't expect an assert for invalid input chars (at least I hope so): "May assert or raise a ValueError if the length of altchars is not 2. May also raise a ValueError if the input is a string with non-ascii characters." |
This report is similar to Issue #105193. I don’t think b64decode('\xD6') is a valid call, even though b64decode(b'\xD6') would be valid (non-alphabet character guaranteed to be discarded in that case). I don’t see much value in the documentation suggesting a possible behaviour with a non-ASCII text argument, which I interpret as a programming error. Is the documentation perhaps not clear enough about what arguments provide supported behaviour (binascii.Error exception or successful decoding) and what is unsupported (wrong data type, wrong text characters, etc)? |
IMO, documenting the ValueError would be good: wrong data type is programming error than not validating string input. Do you want to send a PR? |
Bug report
Bug description:
The b64decode function of the base64 standard library module can behave in unexpected ways with invalid inputs.
Take this example:
According to the docs, I would expect that invalid characters in the input cause a binascii.Error exception. However, this example gives me the following:
So not only do I get an exception that I don't expect according to the docs, it appears there is a bug in the internal exception handling causing an exception (ValueError) during another exception (UnicodeEncodeError).
CPython versions tested on:
3.13
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: