[pypy-commit] pypy default: push fromfloat failure categorization down into rbigint

pjenvey noreply at buildbot.pypy.org
Fri Oct 19 18:52:39 CEST 2012


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: 
Changeset: r58251:ffd5545e9972
Date: 2012-10-19 09:53 -0700
http://bitbucket.org/pypy/pypy/changeset/ffd5545e9972/

Log:	push fromfloat failure categorization down into rbigint

diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -98,12 +98,13 @@
     try:
         return W_LongObject.fromfloat(space, 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"))
+        raise OperationError(
+            space.w_OverflowError,
+            space.wrap("cannot convert float infinity to integer"))
+    except ValueError:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("cannot convert float NaN to integer"))
+
 def trunc__Float(space, w_floatobj):
     whole = math.modf(w_floatobj.floatval)[1]
     try:
@@ -308,7 +309,7 @@
             # Convert to long and use its hash.
             try:
                 w_lval = W_LongObject.fromfloat(space, v)
-            except OverflowError:
+            except (OverflowError, ValueError):
                 # can't convert to long int -- arbitrary
                 if v < 0:
                     return -271828
diff --git a/pypy/rlib/rbigint.py b/pypy/rlib/rbigint.py
--- a/pypy/rlib/rbigint.py
+++ b/pypy/rlib/rbigint.py
@@ -1,7 +1,7 @@
 from pypy.rlib.rarithmetic import LONG_BIT, intmask, longlongmask, r_uint, r_ulonglong, r_longlonglong
 from pypy.rlib.rarithmetic import ovfcheck, r_longlong, widen, is_valid_int
 from pypy.rlib.rarithmetic import most_neg_value_of_same_type
-from pypy.rlib.rfloat import isfinite
+from pypy.rlib.rfloat import isinf, isnan
 from pypy.rlib.debug import make_sure_not_resized, check_regular_int
 from pypy.rlib.objectmodel import we_are_translated, specialize
 from pypy.rlib import jit
@@ -207,10 +207,11 @@
     def fromfloat(dval):
         """ Create a new bigint object from a float """
         # This function is not marked as pure because it can raise
-        if isfinite(dval):
-            return rbigint._fromfloat_finite(dval)
-        else:
-            raise OverflowError
+        if isinf(dval):
+            raise OverflowError("cannot convert float infinity to integer")
+        if isnan(dval):
+            raise ValueError("cannot convert float NaN to integer")
+        return rbigint._fromfloat_finite(dval)
 
     @staticmethod
     @jit.elidable
diff --git a/pypy/rlib/test/test_rbigint.py b/pypy/rlib/test/test_rbigint.py
--- a/pypy/rlib/test/test_rbigint.py
+++ b/pypy/rlib/test/test_rbigint.py
@@ -4,6 +4,7 @@
 from random import random, randint, sample
 from pypy.rlib.rbigint import rbigint, SHIFT, MASK, KARATSUBA_CUTOFF
 from pypy.rlib.rbigint import _store_digit, _mask_digit
+from pypy.rlib.rfloat import NAN
 from pypy.rlib import rbigint as lobj
 from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong, intmask
 from pypy.rpython.test.test_llinterp import interpret
@@ -266,6 +267,7 @@
         x = 12345.6789e200
         x *= x
         assert raises(OverflowError, rbigint.fromfloat, x)
+        assert raises(ValueError, rbigint.fromfloat, NAN)
         #
         f1 = rbigint.fromfloat(9007199254740991.0)
         assert f1.tolong() == 9007199254740991


More information about the pypy-commit mailing list