summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2013-03-12 23:52:34 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2013-03-12 23:52:34 (GMT)
commita7d64a6f4c0307f9c58ea170705b1552580d93eb (patch)
treedec59285d3c60b71c205dc3f62cb52aa4f5d7d52
parent7343cb0790d3ac722ee788d5c2044cbda770a140 (diff)
downloadcpython-a7d64a6f4c0307f9c58ea170705b1552580d93eb.zip
cpython-a7d64a6f4c0307f9c58ea170705b1552580d93eb.tar.gz
cpython-a7d64a6f4c0307f9c58ea170705b1552580d93eb.tar.bz2
#17368: Fix an off-by-one error in the Python JSON decoder that caused a failure while decoding empty object literals when object_pairs_hook was specified.
-rw-r--r--Lib/json/decoder.py2
-rw-r--r--Lib/test/json_tests/test_decode.py14
-rw-r--r--Misc/NEWS4
3 files changed, 15 insertions, 5 deletions
diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py
index 0c59edd..938ebff 100644
--- a/Lib/json/decoder.py
+++ b/Lib/json/decoder.py
@@ -167,7 +167,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
if nextchar == '}':
if object_pairs_hook is not None:
result = object_pairs_hook(pairs)
- return result, end
+ return result, end + 1
pairs = {}
if object_hook is not None:
pairs = object_hook(pairs)
diff --git a/Lib/test/json_tests/test_decode.py b/Lib/test/json_tests/test_decode.py
index 0a55a33..15a427f 100644
--- a/Lib/test/json_tests/test_decode.py
+++ b/Lib/test/json_tests/test_decode.py
@@ -25,16 +25,22 @@ class TestDecode:
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
("qrt", 5), ("pad", 6), ("hoy", 7)]
self.assertEqual(self.loads(s), eval(s))
- self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)
+ self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
self.assertEqual(self.json.load(StringIO(s),
object_pairs_hook=lambda x: x), p)
- od = self.loads(s, object_pairs_hook = OrderedDict)
+ od = self.loads(s, object_pairs_hook=OrderedDict)
self.assertEqual(od, OrderedDict(p))
self.assertEqual(type(od), OrderedDict)
# the object_pairs_hook takes priority over the object_hook
- self.assertEqual(self.loads(s, object_pairs_hook = OrderedDict,
- object_hook = lambda x: None),
+ self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict,
+ object_hook=lambda x: None),
OrderedDict(p))
+ # check that empty objects literals work (see #17368)
+ self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict),
+ OrderedDict())
+ self.assertEqual(self.loads('{"empty": {}}',
+ object_pairs_hook=OrderedDict),
+ OrderedDict([('empty', OrderedDict())]))
def test_decoder_optimizations(self):
# Several optimizations were made that skip over calls to
diff --git a/Misc/NEWS b/Misc/NEWS
index c4da800..9b4aa52 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -233,6 +233,10 @@ Core and Builtins
Library
-------
+- Issue #17368: Fix an off-by-one error in the Python JSON decoder that caused
+ a failure while decoding empty object literals when object_pairs_hook was
+ specified.
+
- Issue #14645: The email generator classes now produce output using the
specified linesep throughout. Previously if the prolog, epilog, or
body were stored with a different linesep, that linesep was used. This