[issue19307] Improve TypeError message in json.loads()

Ezio Melotti report at bugs.python.org
Sun Oct 20 07:16:39 CEST 2013


New submission from Ezio Melotti:

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.)

----------
assignee: ezio.melotti
components: Library (Lib)
files: json-error-msg.diff
keywords: patch
messages: 200545
nosy: ezio.melotti, ncoghlan, pitrou, rhettinger, serhiy.storchaka
priority: normal
severity: normal
stage: commit review
status: open
title: Improve TypeError message in json.loads()
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file32238/json-error-msg.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19307>
_______________________________________


More information about the Python-bugs-list mailing list