Unexpected nested exception with b64decode
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
The b64decode function of the base64 standard library module can behave in unexpected ways with invalid inputs.
Take this example:
#!/usr/bin/python3
import base64
base64.b64decode('a\xd6==')
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:
Traceback (most recent call last):
File "/usr/lib/python3.12/base64.py", line 37, in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode character '\xd6' in position 1: ordinal not in range(128)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/./test", line 3, in <module>
base64.b64decode('a\xd6==')
File "/usr/lib/python3.12/base64.py", line 83, in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/base64.py", line 39, in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
ValueError: string argument should contain only ASCII characters
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the non-ASCII input example, then read base64.py around b64decode and _bytes_from_decode_data to compare the observed exceptions with the documented behavior. Done means invalid input follows the documented exception behavior without the unexpected nested traceback.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100