[pypy-commit] pypy py3k: add a newlong_from_float that handles app level exceptions

pjenvey noreply at buildbot.pypy.org
Mon Dec 10 22:49:08 CET 2012


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r59385:2de9445698d1
Date: 2012-12-10 13:40 -0800
http://bitbucket.org/pypy/pypy/changeset/2de9445698d1/

Log:	add a newlong_from_float that handles app level exceptions

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
@@ -88,21 +88,14 @@
     return W_FloatObject(a)
 
 def int__Float(space, w_value):
+    from pypy.objspace.std.longobject import newlong_from_float
     try:
         value = ovfcheck_float_to_int(w_value.floatval)
     except OverflowError:
         pass
     else:
         return space.newint(value)
-    try:
-        return W_LongObject.fromfloat(space, w_value.floatval)
-    except OverflowError:
-        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"))
+    return newlong_from_float(space, w_value.floatval)
 
 def trunc__Float(space, w_floatobj):
     whole = math.modf(w_floatobj.floatval)[1]
diff --git a/pypy/objspace/std/floattype.py b/pypy/objspace/std/floattype.py
--- a/pypy/objspace/std/floattype.py
+++ b/pypy/objspace/std/floattype.py
@@ -276,7 +276,7 @@
 def descr___round__(space, w_float, w_ndigits=None):
     # Algorithm copied directly from CPython
     from pypy.objspace.std.floatobject import W_FloatObject
-    from pypy.objspace.std.longobject import W_LongObject
+    from pypy.objspace.std.longobject import newlong_from_float
     assert isinstance(w_float, W_FloatObject)
     x = w_float.floatval
 
@@ -286,16 +286,7 @@
         if math.fabs(x - rounded) == 0.5:
             # halfway case: round to even
             rounded = 2.0 * rfloat.round_away(x / 2.0)
-        try:
-            return W_LongObject.fromfloat(space, rounded)
-        except OverflowError:
-            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"))
+        return newlong_from_float(space, rounded)
 
     # interpret 2nd argument as a Py_ssize_t; clip on overflow
     ndigits = space.getindex_w(w_ndigits, None)
diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -86,6 +86,20 @@
             return W_SmallLongObject(z)
     return W_LongObject(bigint)
 
+def newlong_from_float(space, floatval):
+    """Return a W_LongObject from an RPython float.
+
+    Raises app-level exceptions on failure.
+    """
+    try:
+        return W_LongObject.fromfloat(space, floatval)
+    except OverflowError:
+        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"))
 
 # bool-to-long
 def delegate_Bool2Long(space, w_bool):


More information about the pypy-commit mailing list