Commit a0e768cc authored by Ezio Melotti's avatar Ezio Melotti

#19307: Improve error message for json.load(s) while passing objects of the wrong type.

parent 4ea16e56
...@@ -310,6 +310,9 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, ...@@ -310,6 +310,9 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
The ``encoding`` argument is ignored and deprecated. The ``encoding`` argument is ignored and deprecated.
""" """
if not isinstance(s, str):
raise TypeError('the JSON object must be str, not {!r}'.format(
s.__class__.__name__))
if (cls is None and object_hook is None and if (cls is None and object_hook is None and
parse_int is None and parse_float is None and parse_int is None and parse_float is None and
parse_constant is None and object_pairs_hook is None and not kw): parse_constant is None and object_pairs_hook is None and not kw):
......
import decimal import decimal
from io import StringIO from io import StringIO, BytesIO
from collections import OrderedDict from collections import OrderedDict
from test.test_json import PyTest, CTest from test.test_json import PyTest, CTest
...@@ -70,5 +70,12 @@ class TestDecode: ...@@ -70,5 +70,12 @@ class TestDecode:
msg = 'escape' msg = 'escape'
self.assertRaisesRegex(ValueError, msg, self.loads, s) self.assertRaisesRegex(ValueError, msg, self.loads, s)
def test_invalid_input_type(self):
msg = 'the JSON object must be str'
for value in [1, 3.14, b'bytes', b'\xff\x00', [], {}, None]:
self.assertRaisesRegex(TypeError, msg, self.loads, value)
with self.assertRaisesRegex(TypeError, msg):
self.json.load(BytesIO(b'[1,2,3]'))
class TestPyDecode(TestDecode, PyTest): pass class TestPyDecode(TestDecode, PyTest): pass
class TestCDecode(TestDecode, CTest): pass class TestCDecode(TestDecode, CTest): pass
...@@ -62,6 +62,9 @@ Core and Builtins ...@@ -62,6 +62,9 @@ Core and Builtins
Library Library
------- -------
- Issue #19307: Improve error message for json.load(s) while passing objects
of the wrong type.
- Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by - Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by
limiting the call to readline(). Original patch by Michał limiting the call to readline(). Original patch by Michał
Jastrzębski and Giampaolo Rodola. Jastrzębski and Giampaolo Rodola.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment