]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31373: remove overly strict float range checks (GH-3486) (#3495)
authorBenjamin Peterson <benjamin@python.org>
Tue, 12 Sep 2017 06:08:49 +0000 (23:08 -0700)
committerGitHub <noreply@github.com>
Tue, 12 Sep 2017 06:08:49 +0000 (23:08 -0700)
This undoes a853a8ba7850381d49b284295dd6f0dc491dbe44 except for the pytime.c
parts. We want to continue to allow IEEE 754 doubles larger than FLT_MAX to be
rounded into finite floats. Tests were added to very this behavior.
(cherry picked from commit 2bb69a5b4e7f96cb35d1b28aa7b7b3974b351f59)

Lib/test/test_float.py
Lib/test/test_getargs2.py
Objects/floatobject.c
Python/getargs.c

index 6491f458c3acc4dbd2daa90c6944f100baaaee79..c5ca50c8f71206d5b3e55a053bacd6f5e90817af 100644 (file)
@@ -613,6 +613,12 @@ class IEEEFormatTestCase(unittest.TestCase):
                           ('<f', LE_FLOAT_NAN)]:
             struct.unpack(fmt, data)
 
+    @support.requires_IEEE_754
+    def test_serialized_float_rounding(self):
+        from _testcapi import FLT_MAX
+        self.assertEqual(struct.pack("<f", 3.40282356e38), struct.pack("<f", FLT_MAX))
+        self.assertEqual(struct.pack("<f", -3.40282356e38), struct.pack("<f", -FLT_MAX))
+
 class FormatTestCase(unittest.TestCase):
 
     def test_format(self):
index 8a194aa03d42784685d674e2e8b3153bcdb8ac0a..d14a6ee31fd8b120c2292bb48132098c6dcd95d9 100644 (file)
@@ -377,6 +377,12 @@ class Float_TestCase(unittest.TestCase):
         r = getargs_f(NAN)
         self.assertNotEqual(r, r)
 
+    @support.requires_IEEE_754
+    def test_f_rounding(self):
+        from _testcapi import getargs_f
+        self.assertEqual(getargs_f(3.40282356e38), FLT_MAX)
+        self.assertEqual(getargs_f(-3.40282356e38), -FLT_MAX)
+
     def test_d(self):
         from _testcapi import getargs_d
         self.assertEqual(getargs_d(4.25), 4.25)
index 1803a683ba0547c9b6b45c0ef1aeecba52a20d67..1f134aa113154b982406b1063883d922e6e3b355 100644 (file)
@@ -2182,13 +2182,13 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
 
     }
     else {
+        float y = (float)x;
         int i, incr = 1;
 
-        if (fabs(x) > FLT_MAX && !Py_IS_INFINITY(x))
+        if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
             goto Overflow;
 
         unsigned char s[sizeof(float)];
-        float y = (float)x;
         memcpy(s, &y, sizeof(float));
 
         if ((float_format == ieee_little_endian_format && !le)
index 8fb19f34ecb211ff0381a823d652219c8262a27b..13819642060b4947f588f0de4f995d7396d1e7cd 100644 (file)
@@ -811,10 +811,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
         double dval = PyFloat_AsDouble(arg);
         if (PyErr_Occurred())
             RETURN_ERR_OCCURRED;
-        else if (dval > FLT_MAX)
-            *p = (float)INFINITY;
-        else if (dval < -FLT_MAX)
-            *p = (float)-INFINITY;
         else
             *p = (float) dval;
         break;