Kaydet (Commit) 3a3f9cc9 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Decimal special values did not hash properly.

üst 3b0dad06
...@@ -728,6 +728,10 @@ class Decimal(object): ...@@ -728,6 +728,10 @@ class Decimal(object):
# Decimal integers must hash the same as the ints # Decimal integers must hash the same as the ints
# Non-integer decimals are normalized and hashed as strings # Non-integer decimals are normalized and hashed as strings
# Normalization assures that hast(100E-1) == hash(10) # 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) i = int(self)
if self == Decimal(i): if self == Decimal(i):
return hash(i) return hash(i)
......
...@@ -811,6 +811,9 @@ class DecimalUsabilityTest(unittest.TestCase): ...@@ -811,6 +811,9 @@ class DecimalUsabilityTest(unittest.TestCase):
hash(Decimal(23)) hash(Decimal(23))
#the same hash that to an int #the same hash that to an int
self.assertEqual(hash(Decimal(23)), hash(23)) 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): def test_min_and_max_methods(self):
......
...@@ -12,6 +12,9 @@ What's New in Python 2.4.1c2? ...@@ -12,6 +12,9 @@ What's New in Python 2.4.1c2?
Library 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 - Bug #1160802: can't build Zope on Windows with 2.4.1c1. The
``MSVCCompiler`` class in distutils forgot to record that it was ``MSVCCompiler`` class in distutils forgot to record that it was
initialized, and continued adding redundant entries to the system initialized, and continued adding redundant entries to the system
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment