[pypy-commit] pypy fastjson: do the correct thing when we have a float number with a large number of digits

antocuni noreply at buildbot.pypy.org
Fri Jun 28 12:08:10 CEST 2013


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: fastjson
Changeset: r65052:09970bd41cfe
Date: 2013-06-28 12:04 +0200
http://bitbucket.org/pypy/pypy/changeset/09970bd41cfe/

Log:	do the correct thing when we have a float number with a large number
	of digits

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
@@ -163,14 +163,26 @@
             return self.space.wrap(intval)
 
     def decode_numeric_slow(self, i):
+        is_float = False
         start = i
         if self.ll_chars[i] == '-':
             i += 1
+        # skip the integral part
         while self.ll_chars[i].isdigit():
             i += 1
+        if self.ll_chars[i] == '.':
+            is_float = True
+            i += 1
+            # skip the fractional part
+            while self.ll_chars[i].isdigit():
+                i += 1
         s = self.getslice(start, i)
         self.pos = i
-        w_res = self.space.call_function(self.space.w_int, self.space.wrap(s))
+        if is_float:
+            w_func = self.space.w_float
+        else:
+            w_func = self.space.w_int
+        w_res = self.space.call_function(w_func, self.space.wrap(s))
         #assert w_res is not None # XXX check if this brings any speedup in pypy-c
         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
@@ -99,6 +99,7 @@
 
 
     def test_decode_numeric(self):
+        import sys
         import _fastjson
         def check(s, val):
             res = _fastjson.loads(s)
@@ -120,6 +121,8 @@
         check('5E0001', 50.0)
         check(str(1 << 32), 1 << 32)
         check(str(1 << 64), 1 << 64)
+        x = str(sys.maxint+1) + '.123'
+        check(x, float(x))
 
     def test_decode_numeric_invalid(self):
         import _fastjson


More information about the pypy-commit mailing list