[pypy-svn] r77625 - in pypy/branch/fast-forward/pypy: objspace/std objspace/std/test rlib

afa at codespeak.net afa at codespeak.net
Tue Oct 5 19:54:44 CEST 2010


Author: afa
Date: Tue Oct  5 19:54:42 2010
New Revision: 77625

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/floatobject.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py
   pypy/branch/fast-forward/pypy/rlib/rarithmetic.py
   pypy/branch/fast-forward/pypy/rlib/rbigint.py
Log:
Ensure that int(float('nan')) raises a ValueError


Modified: pypy/branch/fast-forward/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/floatobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/floatobject.py	Tue Oct  5 19:54:42 2010
@@ -74,21 +74,20 @@
     try:
         return W_LongObject.fromfloat(w_floatobj.floatval)
     except OverflowError:
+        if isnan(w_floatobj.floatval):
+            raise OperationError(
+                space.w_ValueError,
+                space.wrap("cannot convert float NaN to integer"))
         raise OperationError(space.w_OverflowError,
                              space.wrap("cannot convert float infinity to long"))
 def trunc__Float(space, w_floatobj):
     whole = math.modf(w_floatobj.floatval)[1]
     try:
-        i = ovfcheck_float_to_int(whole)
+        value = ovfcheck_float_to_int(whole)
     except OverflowError:
-        pass
+        return long__Float(space, w_floatobj)
     else:
-        return space.wrap(i)
-    try:
-        return W_LongObject.fromfloat(w_floatobj.floatval)
-    except OverflowError:
-        raise OperationError(space.w_OverflowError,
-                             space.wrap("cannot convert infinity to long"))
+        return space.newint(value)
 
 def float_w__Float(space, w_float):
     return w_float.floatval

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py	Tue Oct  5 19:54:42 2010
@@ -89,6 +89,11 @@
         assert int(42.1234) == 42
         assert int(4e10) == 40000000000L
 
+        raises(OverflowError, int, float('inf'))
+        raises(OverflowError, long, float('inf'))
+        raises(ValueError, int, float('nan'))
+        raises(ValueError, long, float('nan'))
+
     def test_float_string(self):
         assert 42 == float("42")
         assert 42.25 == float("42.25")

Modified: pypy/branch/fast-forward/pypy/rlib/rarithmetic.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rarithmetic.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rarithmetic.py	Tue Oct  5 19:54:42 2010
@@ -217,6 +217,8 @@
 # successfully be casted to an int.
 if sys.maxint == 2147483647:
     def ovfcheck_float_to_int(x):
+        if isnan(x):
+            raise OverflowError
         if -2147483649.0 < x < 2147483648.0:
             return int(x)
         raise OverflowError
@@ -225,6 +227,8 @@
     # Note the "<= x <" here, as opposed to "< x <" above.
     # This is justified by test_typed in translator/c/test.
     def ovfcheck_float_to_int(x):
+        if isnan(x):
+            raise OverflowError
         if -9223372036854776832.0 <= x < 9223372036854775296.0:
             return int(x)
         raise OverflowError

Modified: pypy/branch/fast-forward/pypy/rlib/rbigint.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rbigint.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rbigint.py	Tue Oct  5 19:54:42 2010
@@ -1,5 +1,5 @@
 from pypy.rlib.rarithmetic import LONG_BIT, intmask, r_uint, r_ulonglong
-from pypy.rlib.rarithmetic import ovfcheck, r_longlong, widen
+from pypy.rlib.rarithmetic import ovfcheck, r_longlong, widen, isinf, isnan
 from pypy.rlib.debug import make_sure_not_resized
 
 import math, sys
@@ -109,7 +109,7 @@
     def fromfloat(dval):
         """ Create a new bigint object from a float """
         neg = 0
-        if isinf(dval):
+        if isinf(dval) or isnan(dval):
             raise OverflowError
         if dval < 0.0:
             neg = 1
@@ -1259,9 +1259,6 @@
     assert x > 0.0
     return x * sign, exponent
 
-def isinf(x):
-    return x != 0.0 and x / 2 == x
-
 ##def ldexp(x, exp):
 ##    assert type(x) is float
 ##    lb1 = LONG_BIT - 1



More information about the Pypy-commit mailing list