[pypy-commit] pypy default: Fix a crash (escaped ValueError) in various division methods on floats with an inf LHS.
alex_gaynor
noreply at buildbot.pypy.org
Sat Sep 3 17:24:37 CEST 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r47047:7c36492d9f9d
Date: 2011-09-03 11:24 -0400
http://bitbucket.org/pypy/pypy/changeset/7c36492d9f9d/
Log: Fix a crash (escaped ValueError) in various division methods on
floats with an inf LHS.
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
@@ -355,9 +355,13 @@
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)
@@ -366,7 +370,10 @@
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
diff --git a/pypy/objspace/std/test/test_floatobject.py b/pypy/objspace/std/test/test_floatobject.py
--- a/pypy/objspace/std/test/test_floatobject.py
+++ b/pypy/objspace/std/test/test_floatobject.py
@@ -767,3 +767,19 @@
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)
\ No newline at end of file
More information about the pypy-commit
mailing list