[pypy-svn] r14839 - in pypy/dist/pypy: annotation objspace/std rpython rpython/test

pedronis at codespeak.net pedronis at codespeak.net
Thu Jul 21 02:04:48 CEST 2005


Author: pedronis
Date: Thu Jul 21 02:04:45 2005
New Revision: 14839

Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/objspace/std/strutil.py
   pypy/dist/pypy/rpython/rarithmetic.py
   pypy/dist/pypy/rpython/test/test_rarithmetic.py
Log:
ovfcheck(int()) is not supported. Implemented alternative.



Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Thu Jul 21 02:04:45 2005
@@ -229,6 +229,9 @@
 def math_frexp(x):
     return SomeTuple((SomeFloat(), SomeInteger()))
 
+def math_modf(x):
+    return SomeTuple((SomeFloat(), SomeFloat()))
+
 def math_any(*args):
     return SomeFloat()
 
@@ -295,6 +298,7 @@
 BUILTIN_ANALYZERS[math.exp] = math_any
 BUILTIN_ANALYZERS[math.ldexp] = math_any
 BUILTIN_ANALYZERS[math.frexp] = math_frexp
+BUILTIN_ANALYZERS[math.modf] = math_modf
 BUILTIN_ANALYZERS[sys.getrefcount] = count
 BUILTIN_ANALYZERS[sys.getdefaultencoding] = conf
 import unicodedata

Modified: pypy/dist/pypy/objspace/std/strutil.py
==============================================================================
--- pypy/dist/pypy/objspace/std/strutil.py	(original)
+++ pypy/dist/pypy/objspace/std/strutil.py	Thu Jul 21 02:04:45 2005
@@ -2,7 +2,7 @@
 Pure Python implementation of string utilities.
 """
 
-from pypy.rpython.rarithmetic import r_uint, ovfcheck
+from pypy.rpython.rarithmetic import r_uint, ovfcheck, ovfcheck_float_to_int
 
 # XXX factor more functions out of stringobject.py.
 # This module is independent from PyPy.
@@ -345,7 +345,7 @@
     except (ParseStringOverflowError, OverflowError):
         fe = string_to_float(exponent) + dexp
         try:
-            e = ovfcheck(int(fe))
+            e = ovfcheck_float_to_int(fe)
         except OverflowError:
             # 4) check the exponent for overflow and truncate to +-400.
             if exponent[0] == '-':

Modified: pypy/dist/pypy/rpython/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rarithmetic.py	Thu Jul 21 02:04:45 2005
@@ -24,6 +24,7 @@
 
 
 """
+import math
 
 class r_int(int):
     """ fake integer implementation in order to make sure that
@@ -190,6 +191,16 @@
 def ovfcheck_lshift(a, b):
     return _local_ovfcheck(int(long(a) << b))
 
+FL_MAXINT = float(LONG_TEST-1)
+FL_MININT = float(-LONG_TEST)
+
+def ovfcheck_float_to_int(x):
+    _, intp = math.modf(x)
+    if FL_MININT <= intp <= FL_MAXINT:
+        return int(intp)
+    raise OverflowError
+
+
 def _widen(self, other, value):
     """
     if one argument is int or long, the other type wins.

Modified: pypy/dist/pypy/rpython/test/test_rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rarithmetic.py	Thu Jul 21 02:04:45 2005
@@ -235,3 +235,31 @@
         pass
     else:
         assert False    
+
+def test_ovfcheck_float_to_int():
+    assert ovfcheck_float_to_int(1.0) == 1
+    assert ovfcheck_float_to_int(0.0) == 0
+    assert ovfcheck_float_to_int(13.0) == 13
+    assert ovfcheck_float_to_int(-1.0) == -1
+    assert ovfcheck_float_to_int(-13.0) == -13
+    assert ovfcheck_float_to_int(float(sys.maxint-1)) == sys.maxint-1
+    assert ovfcheck_float_to_int(float(sys.maxint)) == sys.maxint
+    assert ovfcheck_float_to_int(float(-sys.maxint)) == -sys.maxint
+    assert ovfcheck_float_to_int(float(-sys.maxint-1)) == -sys.maxint-1
+
+    try:
+        ovfcheck_float_to_int(float(-sys.maxint-1)-1)
+    except OverflowError:
+        pass
+    else:
+        assert False
+
+    try:
+        ovfcheck_float_to_int(float(sys.maxint)+1)
+    except OverflowError:
+        pass
+    else:
+        assert False
+
+
+



More information about the Pypy-commit mailing list