# Commits

committed 7c36492

Fix a crash (escaped ValueError) in various division methods on floats with an inf LHS.

• Participants
• Parent commits c3d09f0

# pypy/objspace/std/floatobject.py

y = w_float2.floatval
if y == 0.0:
raise FailedToImplementArgs(space.w_ZeroDivisionError, space.wrap("float modulo"))
-    mod = math.fmod(x, y)
-    if (mod and ((y < 0.0) != (mod < 0.0))):
-        mod += y
+    try:
+        mod = math.fmod(x, y)
+    except ValueError:
+        mod = rfloat.NAN
+    else:
+        if (mod and ((y < 0.0) != (mod < 0.0))):
+            mod += y

return W_FloatObject(mod)

y = w_float2.floatval
if y == 0.0:
raise FailedToImplementArgs(space.w_ZeroDivisionError, space.wrap("float modulo"))
-    mod = math.fmod(x, y)
+    try:
+        mod = math.fmod(x, y)
+    except ValueError:
+        return [W_FloatObject(rfloat.NAN), W_FloatObject(rfloat.NAN)]
# fmod is typically exact, so vx-mod is *mathematically* an
# exact multiple of wx.  But this is fp arithmetic, and fp
# vx - mod is an approximation; the result is that div may

# pypy/objspace/std/test/test_floatobject.py

def test_invalid(self):
raises(ValueError, float.fromhex, "0P")
+
+    def test_division_edgecases(self):
+        import math
+
+        # inf
+        inf = float("inf")
+        assert math.isnan(inf % 3)
+        assert math.isnan(inf // 3)
+        x, y = divmod(inf, 3)
+        assert math.isnan(x)
+        assert math.isnan(y)
+
+        # divide by 0
+        raises(ZeroDivisionError, lambda: inf % 0)
+        raises(ZeroDivisionError, lambda: inf // 0)
+        raises(ZeroDivisionError, divmod, inf, 0)