Philip Jenvey avatar Philip Jenvey committed 2de9445

add a newlong_from_float that handles app level exceptions

Comments (0)

Files changed (3)

pypy/objspace/std/floatobject.py

     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]

pypy/objspace/std/floattype.py

 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
 
         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)

pypy/objspace/std/longobject.py

             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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.