[pypy-commit] pypy fastjson: parsing of null, true and false
antocuni
noreply at buildbot.pypy.org
Wed Jun 5 16:52:47 CEST 2013
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: fastjson
Changeset: r64802:5e34f815e7f7
Date: 2013-06-05 16:51 +0200
http://bitbucket.org/pypy/pypy/changeset/5e34f815e7f7/
Log: parsing of null, true and false
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
@@ -78,10 +78,47 @@
elif ch == '{':
self.next()
return self.decode_object()
+ elif ch == 'n':
+ self.next()
+ return self.decode_null()
+ elif ch == 't':
+ self.next()
+ return self.decode_true()
+ elif ch == 'f':
+ self.next()
+ return self.decode_false()
else:
self._raise("No JSON object could be decoded: unexpected '%s' at char %d",
ch, self.i)
+ def decode_null(self):
+ N = len('ull')
+ if (self.i+N <= len(self.s) and
+ self.next() == 'u' and
+ self.next() == 'l' and
+ self.next() == 'l'):
+ return self.space.w_None
+ self._raise("Error when decoding null at char %d", self.i)
+
+ def decode_true(self):
+ N = len('rue')
+ if (self.i+N <= len(self.s) and
+ self.next() == 'r' and
+ self.next() == 'u' and
+ self.next() == 'e'):
+ return self.space.w_True
+ self._raise("Error when decoding true at char %d", self.i)
+
+ def decode_false(self):
+ N = len('alse')
+ if (self.i+N <= len(self.s) and
+ self.next() == 'a' and
+ self.next() == 'l' and
+ self.next() == 's' and
+ self.next() == 'e'):
+ return self.space.w_False
+ self._raise("Error when decoding false at char %d", self.i)
+
def decode_numeric(self):
intval = self.parse_integer()
#
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,6 +19,28 @@
class AppTest(object):
spaceconfig = {"objspace.usemodules._fastjson": True}
+ def test_decode_constants(self):
+ import _fastjson
+ assert _fastjson.loads('null') is None
+ raises(ValueError, _fastjson.loads, 'nul')
+ raises(ValueError, _fastjson.loads, 'nu')
+ raises(ValueError, _fastjson.loads, 'n')
+ raises(ValueError, _fastjson.loads, 'nuXX')
+ #
+ assert _fastjson.loads('true') is True
+ raises(ValueError, _fastjson.loads, 'tru')
+ raises(ValueError, _fastjson.loads, 'tr')
+ raises(ValueError, _fastjson.loads, 't')
+ raises(ValueError, _fastjson.loads, 'trXX')
+ #
+ assert _fastjson.loads('false') is False
+ raises(ValueError, _fastjson.loads, 'fals')
+ raises(ValueError, _fastjson.loads, 'fal')
+ raises(ValueError, _fastjson.loads, 'fa')
+ raises(ValueError, _fastjson.loads, 'f')
+ raises(ValueError, _fastjson.loads, 'falXX')
+
+
def test_decode_string(self):
import _fastjson
res = _fastjson.loads('"hello"')
More information about the pypy-commit
mailing list