Test `object_hook` and pickleability of some JSON components (GH-154155)

encukou <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/d333e5aa59f6cc20198502f4ff50b14eabde9b8b
commit: d333e5aa59f6cc20198502f4ff50b14eabde9b8b
branch: main
author: David <[email protected]>
committer: encukou <[email protected]>
date: 2026-07-21T17:08:01+02:00
summary:

Test `object_hook` and pickleability of some JSON components (GH-154155)

files:
A Lib/test/test_json/test_pickleable.py
M Lib/test/test_json/test_decode.py

diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py
index 33aeefa92f282ae..7bff16ddf2155e9 100644
--- a/Lib/test/test_json/test_decode.py
+++ b/Lib/test/test_json/test_decode.py
@@ -48,6 +48,26 @@ def test_empty_objects(self):
         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
index 000000000000000..517cfcdd41a3516
--- /dev/null
+++ b/Lib/test/test_json/test_pickleable.py
@@ -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)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.