Skip to content

gh-74379: allow json.loads with memoryview arguments #131114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions Lib/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,11 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,


def detect_encoding(b):
bstartswith = b.startswith
if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
if b[:4] in (codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE):
return 'utf-32'
if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
if b[:2] in (codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE):
return 'utf-16'
if bstartswith(codecs.BOM_UTF8):
if b[:3] == codecs.BOM_UTF8:
return 'utf-8-sig'

if len(b) >= 4:
Expand Down Expand Up @@ -298,8 +297,8 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None,

def loads(s, *, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
"""Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
containing a JSON document) to a Python object.
"""Deserialize ``s`` (a ``str``, ``bytes``, ``bytearray`` or ``memoryview``
instance containing a JSON document) to a Python object.

``object_hook`` is an optional function that will be called with the
result of any object literal decode (a ``dict``). The return value of
Expand Down Expand Up @@ -335,10 +334,10 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None,
raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
s, 0)
else:
if not isinstance(s, (bytes, bytearray)):
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
if not isinstance(s, (bytes, bytearray, memoryview)):
raise TypeError(f'the JSON object must be str, bytes, bytearray or memoryview, '
f'not {s.__class__.__name__}')
s = s.decode(detect_encoding(s), 'surrogatepass')
s = str(s, detect_encoding(s), 'surrogatepass')

if (cls is None and object_hook is None and
parse_int is None and parse_float is None and
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_json/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def test_limit_int(self):
with self.assertRaises(ValueError):
self.loads('1' * (maxdigits + 1))

def test_memoryview(self):
bom_json = "[1,2,3]".encode('utf-8-sig')
self.assertEqual(self.json.loads(memoryview(bom_json)), [1,2,3])


class TestPyDecode(TestDecode, PyTest): pass
class TestCDecode(TestDecode, CTest): pass
Loading