Skip to content

Commit c73a3fd

Browse files
committed
unix-ffi/json: Fix json.loads() of a bytes object.
The conversion from bytes to string previosuly only if some other argument was also given to `loads`. This makes even a basic call with no other arguments work.
1 parent 6ae440a commit c73a3fd

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

unix-ffi/json/json/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ def loads(
391391
The ``encoding`` argument is ignored and deprecated.
392392
393393
"""
394+
if isinstance(s, (bytes, bytearray)):
395+
s = s.decode('utf-8')
394396
if (
395397
cls is None
396398
and object_hook is None
@@ -413,6 +415,4 @@ def loads(
413415
kw["parse_int"] = parse_int
414416
if parse_constant is not None:
415417
kw["parse_constant"] = parse_constant
416-
if isinstance(s, (bytes, bytearray)):
417-
s = s.decode('utf-8')
418418
return cls(**kw).decode(s)

unix-ffi/json/test_json.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@
1111

1212
# Doesn't work because JSON doesn't have tuples
1313
# assert inp == outp
14+
15+
b = b'["foo", {"bar": ["baz", null, 1, 2]}]'
16+
outp2 = json.loads(b)
17+
18+
assert b.decode('utf-8') == s
19+
assert outp == outp2

0 commit comments

Comments
 (0)