]> granicus.if.org Git - python/commitdiff
Bug #1163325: "special" decimals aren't hashable
authorRaymond Hettinger <python@rcn.com>
Tue, 15 Mar 2005 04:59:17 +0000 (04:59 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 15 Mar 2005 04:59:17 +0000 (04:59 +0000)
Lib/decimal.py
Lib/test/test_decimal.py
Misc/NEWS

index 7f71b83559b534f5b2dae9d3d7ec45f17b61a965..fb11e8f3e4fee5c08be6794ef6dc54ba3340eaf7 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 19ce8f43ec6eaf0ba20ebee6a4c3b4eaf5442dae..c91fb17ce62f4f0f2c7e37fb69c67e2b009ad199 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -86,6 +86,9 @@ Extension Modules
 Library
 -------
 
+- Bug #1163325:  Decimal infinities failed to hash.  Attempting to
+  hash a NaN raised an InvalidOperation instead of a TypeError.
+
 - Patch #918101: Add tarfile open mode r|* for auto-detection of the 
   stream compression; add, for symmetry reasons, r:* as a synonym of r.