Issue 19307: Improve TypeError message in json.loads() (original) (raw)
Currently the error raised when bytes are passed to json.loads() is not very clear:
json.loads(b'') Traceback (most recent call last): File "", line 1, in File "/home/wolf/dev/py/py3k/Lib/json/init.py", line 316, in loads return _default_decoder.decode(s) File "/home/wolf/dev/py/py3k/Lib/json/decoder.py", line 344, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: can't use a string pattern on a bytes-like object
The attached patch changes the error message to:
json.loads(b'') Traceback (most recent call last): File "", line 1, in File "/home/wolf/dev/py/py3k/Lib/json/init.py", line 315, in loads s.class.name)) TypeError: the JSON object must be str, not 'bytes'
(This came up on #18958, and it's required in order to check for a UTF-8 BOM without producing an even more misleading error message.)