]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-149056: Properly pass array_hook in json.load() to json.loads() (GH-149057)
authorThomas Kowalski <thom.kowa@gmail.com>
Fri, 29 May 2026 19:53:21 +0000 (21:53 +0200)
committerGitHub <noreply@github.com>
Fri, 29 May 2026 19:53:21 +0000 (22:53 +0300)
Lib/json/__init__.py
Lib/test/test_json/test_decode.py
Misc/NEWS.d/next/Library/2026-04-29-08-10-17.gh-issue-149056.jnaD4W.rst [new file with mode: 0644]

index 9681a8fe53ec4806d81fd8d812fb1d71b9200520..37a86831ff94838758fe824064abc54066624403 100644 (file)
@@ -307,7 +307,7 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None,
         cls=cls, object_hook=object_hook,
         parse_float=parse_float, parse_int=parse_int,
         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
-        array_hook=None, **kw)
+        array_hook=array_hook, **kw)
 
 
 def loads(s, *, cls=None, object_hook=None, parse_float=None,
index d846c8af7ec4345930b4d3a0b4456a63bee69a4e..1d51fb2de0e69e4aa458724223f2561dddbea428 100644 (file)
@@ -87,6 +87,13 @@ class TestDecode:
 
         self.assertEqual(self.loads('[]', array_hook=tuple), ())
 
+    def test_load_array_hook(self):
+        # json.load must forward array_hook to loads
+        fp = StringIO('[10, 20, 30]')
+        result = self.json.load(fp, array_hook=tuple)
+        self.assertEqual(result, (10, 20, 30))
+        self.assertEqual(type(result), tuple)
+
     def test_decoder_optimizations(self):
         # Several optimizations were made that skip over calls to
         # the whitespace regex, so this test is designed to try and
diff --git a/Misc/NEWS.d/next/Library/2026-04-29-08-10-17.gh-issue-149056.jnaD4W.rst b/Misc/NEWS.d/next/Library/2026-04-29-08-10-17.gh-issue-149056.jnaD4W.rst
new file mode 100644 (file)
index 0000000..0026d02
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`json.load` not forwarding the *array_hook* argument to
+:func:`json.loads`. Patch by Thomas Kowalski.