[pypy-svn] pypy default: Port fix (and test) of simplejson issue #57 to stdlib json library.
Bobby Impollonia
commits-noreply at bitbucket.org
Fri Jan 28 13:33:19 CET 2011
Author: Bobby Impollonia <bobbyi at gmail.com>
Branch:
Changeset: r41423:cb106f809c0f
Date: 2011-01-27 10:55 -0800
http://bitbucket.org/pypy/pypy/changeset/cb106f809c0f/
Log: Port fix (and test) of simplejson issue #57 to stdlib json library.
The bug is that loads('{}') gives [] instead of {}
http://code.google.com/p/simplejson/issues/detail?id=57
This issue doesn't arise in CPython 2.7.0 because it doesn't exist
in the _json C implementation that it uses to replace of the pure
Python implementation with the bug.
diff --git a/lib-python/modified-2.7.0/json/tests/test_decode.py b/lib-python/modified-2.7.0/json/tests/test_decode.py
--- a/lib-python/modified-2.7.0/json/tests/test_decode.py
+++ b/lib-python/modified-2.7.0/json/tests/test_decode.py
@@ -23,6 +23,14 @@
rval = json.loads('{ "key" : "value" , "k":"v" }')
self.assertEquals(rval, {"key":"value", "k":"v"})
+ def test_empty_objects(self):
+ s = '{}'
+ self.assertEqual(json.loads(s), eval(s))
+ s = '[]'
+ self.assertEqual(json.loads(s), eval(s))
+ s = '""'
+ self.assertEqual(json.loads(s), eval(s))
+
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-python/modified-2.7.0/json/decoder.py b/lib-python/modified-2.7.0/json/decoder.py
--- a/lib-python/modified-2.7.0/json/decoder.py
+++ b/lib-python/modified-2.7.0/json/decoder.py
@@ -161,6 +161,12 @@
nextchar = s[end:end + 1]
# Trivial empty object
if nextchar == '}':
+ if object_pairs_hook is not None:
+ result = object_pairs_hook(pairs)
+ return result, end
+ pairs = {}
+ if object_hook is not None:
+ pairs = object_hook(pairs)
return pairs, end + 1
elif nextchar != '"':
raise ValueError(errmsg("Expecting property name", s, end))
More information about the Pypy-commit
mailing list