https://github.com/python/cpython/commit/ad5ba577990e0e824ec866cc82a50792f79ad196
commit: ad5ba577990e0e824ec866cc82a50792f79ad196
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: tomasr8 <[email protected]>
date: 2026-07-22T10:02:21+02:00
summary:

[3.14] Test `object_hook` and pickleability of some JSON components (GH-154155) 
(#154375)

Test `object_hook` and pickleability of some JSON components (GH-154155)
(cherry picked from commit d333e5aa59f6cc20198502f4ff50b14eabde9b8b)

Co-authored-by: David <[email protected]>

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 c8b21b8f582a7ef..0672413bf43c24f 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]

Reply via email to