Raymond Hettinger  committed 4db5ea9

Decimal special values did not hash properly.

  • Participants
  • Parent commits 7f7cbdb
  • Branches 2.4

Comments (0)

Files changed (3)

File Lib/

         # 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)

File Lib/test/

         #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):
+- 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