From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 22 Jul 2026 08:02:21 +0000 (+0200) Subject: [3.14] Test `object_hook` and pickleability of some JSON components (GH-154155) ... X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ad5ba577990e0e824ec866cc82a50792f79ad196;p=thirdparty%2FPython%2Fcpython.git [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 --- diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py index c8b21b8f582a..0672413bf43c 100644 --- a/Lib/test/test_json/test_decode.py +++ b/Lib/test/test_json/test_decode.py @@ -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 index 000000000000..517cfcdd41a3 --- /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)