]> granicus.if.org Git - python/commitdiff
Use a more robust infinity check in _Py_HashDouble.
authorMark Dickinson <dickinsm@gmail.com>
Mon, 5 Apr 2010 18:07:51 +0000 (18:07 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Mon, 5 Apr 2010 18:07:51 +0000 (18:07 +0000)
This fixes a test_decimal failure on FreeBSD 8.0.  (modf apparently
doesn't follow C99 Annex F on FreeBSD.)

Lib/test/test_float.py
Objects/object.c

index b0f353b1f79f9cfe6655fb21cdd33b70241d1fec..bf9beece88e1f1b8186c64fbe67cce1057cb2dcd 100644 (file)
@@ -948,6 +948,15 @@ class InfNanTest(unittest.TestCase):
         self.assertFalse(NAN.is_inf())
         self.assertFalse((0.).is_inf())
 
+    def test_hash_inf(self):
+        # the actual values here should be regarded as an
+        # implementation detail, but they need to be
+        # identical to those used in the Decimal module.
+        self.assertEqual(hash(float('inf')), 314159)
+        self.assertEqual(hash(float('-inf')), -271828)
+        self.assertEqual(hash(float('nan')), 0)
+
+
 fromHex = float.fromhex
 toHex = float.hex
 class HexFloatTestCase(unittest.TestCase):
index 60dc1881cc0cfefc6e1498b8b4acce97dc3fb212..8417a993aba8cfcae1654eff3a81382db74fd8b2 100644 (file)
@@ -1025,15 +1025,15 @@ _Py_HashDouble(double v)
         * of mapping keys will turn out weird.
         */
 
+       if (Py_IS_INFINITY(v))
+               /* can't convert to long int -- arbitrary */
+               v = v < 0 ? -271828.0 : 314159.0;
        fractpart = modf(v, &intpart);
        if (fractpart == 0.0) {
                /* This must return the same hash as an equal int or long. */
                if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) {
                        /* Convert to long and use its hash. */
                        PyObject *plong;        /* converted to Python long */
-                       if (Py_IS_INFINITY(intpart))
-                               /* can't convert to long int -- arbitrary */
-                               v = v < 0 ? -271828.0 : 314159.0;
                        plong = PyLong_FromDouble(v);
                        if (plong == NULL)
                                return -1;