Author: Antonio Cuni <[email protected]>
Branch: fastjson
Changeset: r65116:c64aefd4f08d
Date: 2013-06-30 01:24 +0200
http://bitbucket.org/pypy/pypy/changeset/c64aefd4f08d/

Log:    add support for non-standard constants Infinity, -Infinity and NaN

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
@@ -97,7 +97,15 @@
             return self.decode_true(i+1)
         elif ch == 'f':
             return self.decode_false(i+1)
-        elif ch.isdigit() or ch == '-':
+        elif ch == 'I':
+            return self.decode_infinity(i+1)
+        elif ch == 'N':
+            return self.decode_nan(i+1)
+        elif ch == '-':
+            if self.ll_chars[i+1] == 'I':
+                return self.decode_infinity(i+2, sign=-1)
+            return self.decode_numeric(i)
+        elif ch.isdigit():
             return self.decode_numeric(i)
         else:
             self._raise("No JSON object could be decoded: unexpected '%s' at 
char %d",
@@ -128,6 +136,25 @@
             return self.space.w_False
         self._raise("Error when decoding false at char %d", i)
 
+    def decode_infinity(self, i, sign=1):
+        if (self.ll_chars[i]   == 'n' and
+            self.ll_chars[i+1] == 'f' and
+            self.ll_chars[i+2] == 'i' and
+            self.ll_chars[i+3] == 'n' and
+            self.ll_chars[i+4] == 'i' and
+            self.ll_chars[i+5] == 't' and
+            self.ll_chars[i+6] == 'y'):
+            self.pos = i+7
+            return self.space.wrap(rfloat.INFINITY * sign)
+        self._raise("Error when decoding Infinity at char %d", i)
+
+    def decode_nan(self, i):
+        if (self.ll_chars[i]   == 'a' and
+            self.ll_chars[i+1] == 'N'):
+            self.pos = i+2
+            return self.space.wrap(rfloat.NAN)
+        self._raise("Error when decoding NaN at char %d", i)
+
     def decode_numeric(self, i):
         start = i
         i, ovf_maybe, intval = self.parse_integer(i)
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
@@ -98,7 +98,6 @@
         s = '"\xe0"' # this is an invalid UTF8 sequence inside a string
         raises(UnicodeDecodeError, "_fastjson.loads(s)")
 
-
     def test_decode_numeric(self):
         import sys
         import _fastjson
@@ -131,6 +130,15 @@
         check(x, float(x))
         #
         check('1E400', float('inf'))
+        ## # these are non-standard but supported by CPython json
+        check('Infinity', float('inf'))
+        check('-Infinity', float('-inf'))
+
+    def test_nan(self):
+        import math
+        import _fastjson
+        res = _fastjson.loads('NaN')
+        assert math.isnan(res)
 
     def test_decode_numeric_invalid(self):
         import _fastjson
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to