]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Decimal special values did not hash properly.
authorRaymond Hettinger <python@rcn.com>
Tue, 15 Mar 2005 23:36:19 +0000 (23:36 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 15 Mar 2005 23:36:19 +0000 (23:36 +0000)
Lib/decimal.py
Lib/test/test_decimal.py
Misc/NEWS

index 05bdc99f4e0da60b8623aa0d66e0c0da6adf8bc7..539fe8dff8d63b9789288c0e1d5819ba7980dd25 100644 (file)
@@ -728,6 +728,10 @@ class Decimal(object):
         # Decimal integers must hash the same as the ints
         # Non-integer decimals are normalized and hashed as strings
         # Normalization assures that hast(100E-1) == hash(10)
+        if self._is_special:
+            if self._isnan():
+                raise TypeError('Cannot hash a NaN value.')
+            return hash(str(self))
         i = int(self)
         if self == Decimal(i):
             return hash(i)
index f523a721316a5d5762d635921ce72794010eb975..fc1e0482846521bc1a5fb901c73743ee4839f2ed 100644 (file)
@@ -811,6 +811,9 @@ class DecimalUsabilityTest(unittest.TestCase):
         hash(Decimal(23))
         #the same hash that to an int
         self.assertEqual(hash(Decimal(23)), hash(23))
+        self.assertRaises(TypeError, hash, Decimal('NaN'))
+        self.assert_(hash(Decimal('Inf')))
+        self.assert_(hash(Decimal('-Inf')))
 
     def test_min_and_max_methods(self):
 
index fa88cad929c6dfa64a7c7d0e25fe9b750182ea32..9fe3cfaa660c2a5125f2c14265ea14bce9ec75d5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4.1c2?
 Library
 -------
 
+- Bug #1163325:  Decimal infinities failed to hash.  Attempting to
+  hash a NaN raised an InvalidOperation instead of a TypeError.
+
 - Bug #1160802: can't build Zope on Windows with 2.4.1c1.  The
   ``MSVCCompiler`` class in distutils forgot to record that it was
   initialized, and continued adding redundant entries to the system