]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Test `object_hook` and pickleability of some JSON components (GH-154155)
authorDavid <slavicek.david29@gmail.com>
Tue, 21 Jul 2026 15:08:01 +0000 (17:08 +0200)
committerGitHub <noreply@github.com>
Tue, 21 Jul 2026 15:08:01 +0000 (17:08 +0200)
Lib/test/test_json/test_decode.py
Lib/test/test_json/test_pickleable.py [new file with mode: 0644]

index 33aeefa92f282ae71738fa1ef829bb7cfecd583e..7bff16ddf2155e9ad33111067d264c2191c52ddc 100644 (file)
@@ -48,6 +48,26 @@ class TestDecode:
         self.assertEqual(self.loads('[]'), [])
         self.assertEqual(self.loads('""'), "")
 
+    def test_object_hook(self):
+        s = '{"a":{"b":{}}}'
+
+        expected_result = {"a":{"b":{"x":1}, "x":1}, "x":1}
+        expected_hook_arguments = [
+            {}, {"b": {"x":1}}, {"a": {"b": {"x":1}, "x":1}}
+        ]
+
+        hook_arguments = []
+
+        def hook(x):
+            hook_arguments.append(x)
+            return {**x, "x":1}
+
+        result = self.loads(s, object_hook=hook)
+
+        self.assertEqual(result, expected_result)
+        self.assertEqual(hook_arguments, expected_hook_arguments)
+
+
     def test_object_pairs_hook(self):
         s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
         p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
diff --git a/Lib/test/test_json/test_pickleable.py b/Lib/test/test_json/test_pickleable.py
new file mode 100644 (file)
index 0000000..517cfcd
--- /dev/null
@@ -0,0 +1,14 @@
+import json
+import pickle
+import unittest
+
+class TestPickleable(unittest.TestCase):
+    def test_json_decode_error_is_pickleable(self):
+        e = json.JSONDecodeError(msg="abc", doc="def", pos=7)
+
+        pickled = pickle.dumps(e)
+        unpickled = pickle.loads(pickled)
+
+        self.assertEqual(unpickled.msg, e.msg)
+        self.assertEqual(unpickled.doc, e.doc)
+        self.assertEqual(unpickled.pos, e.pos)