Author: Antonio Cuni <[email protected]>
Branch: fastjson
Changeset: r64769:51df42a6fd1c
Date: 2013-06-04 15:39 +0200
http://bitbucket.org/pypy/pypy/changeset/51df42a6fd1c/

Log:    skip the whitespace at the end and complain if there are extra chars

diff --git a/pypy/module/_fastjson/interp_decoder.py 
b/pypy/module/_fastjson/interp_decoder.py
--- a/pypy/module/_fastjson/interp_decoder.py
+++ b/pypy/module/_fastjson/interp_decoder.py
@@ -1,4 +1,4 @@
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.interpreter import unicodehelper
 
@@ -60,6 +60,10 @@
 @unwrap_spec(s=str)
 def loads(space, s):
     decoder = JSONDecoder(space, s)
-    return decoder.decode_any()
-
-
+    w_res = decoder.decode_any()
+    decoder.skip_whitespace()
+    if not decoder.eof():
+        start = decoder.i
+        end = len(decoder.s)
+        raise operationerrfmt(space.w_ValueError, "Extra data: char %d - %d", 
start, end)
+    return w_res
diff --git a/pypy/module/_fastjson/test/test__fastjson.py 
b/pypy/module/_fastjson/test/test__fastjson.py
--- a/pypy/module/_fastjson/test/test__fastjson.py
+++ b/pypy/module/_fastjson/test/test__fastjson.py
@@ -19,14 +19,21 @@
 class AppTest(object):
     spaceconfig = {"objspace.usemodules._fastjson": True}
 
-    def test_load_string(self):
+    def test_decode_string(self):
         import _fastjson
         res = _fastjson.loads('"hello"')
         assert res == u'hello'
         assert type(res) is unicode
 
-    def test_load_string_utf8(self):
+    def test_decode_string_utf8(self):
         import _fastjson
         s = u'&#224;&#232;&#236;&#242;&#249;'
         res = _fastjson.loads('"%s"' % s.encode('utf-8'))
         assert res == s
+
+    def test_skip_whitespace(self):
+        import _fastjson
+        s = '   "hello"   '
+        assert _fastjson.loads(s) == u'hello'
+        s = '   "hello"   extra'
+        raises(ValueError, "_fastjson.loads(s)")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to