This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve TypeError message in json.loads()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: barry, ezio.melotti, ncoghlan, pitrou, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-10-20 05:16 by ezio.melotti, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
json-error-msg.diff ezio.melotti, 2013-10-20 05:16 review
Messages (5)
msg200545 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-10-20 05:16
Currently the error raised when bytes are passed to json.loads() is not very clear:
>>> json.loads(b'')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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 "<stdin>", line 1, in <module>
  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.)
msg200557 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2013-10-20 10:09
Looks good to me: +1 :)
msg200623 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-20 19:43
LGTM. But perhaps it's worth to test json.load() with binary file. I don't see much sense in testing json.loads() with numbers.
msg200647 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-20 22:53
New changeset 241996bd3c6b by Ezio Melotti in branch 'default':
#19307: Improve error message for json.load(s) while passing objects of the wrong type.
http://hg.python.org/cpython/rev/241996bd3c6b
msg200648 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-10-20 22:59
I added a test with json.load() too.  json.load(fp) just calls json.loads(fp.read()), but it's better to avoid white-box testing.
History
Date User Action Args
2022-04-11 14:57:52adminsetgithub: 63506
2013-10-20 22:59:40ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg200648

stage: commit review -> resolved
2013-10-20 22:53:07python-devsetnosy: + python-dev
messages: + msg200647
2013-10-20 19:43:22serhiy.storchakasetmessages: + msg200623
2013-10-20 12:24:54barrysetnosy: + barry
2013-10-20 10:09:54ncoghlansetmessages: + msg200557
2013-10-20 05:24:07ezio.melottilinkissue18958 dependencies
2013-10-20 05:16:39ezio.melotticreate