From: Raymond Hettinger Date: Tue, 15 Mar 2005 23:36:19 +0000 (+0000) Subject: Decimal special values did not hash properly. X-Git-Tag: v2.4.1c2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3a3f9cc9c1a1ba9348b98be954d0dc433aee6479;p=thirdparty%2FPython%2Fcpython.git Decimal special values did not hash properly. --- diff --git a/Lib/decimal.py b/Lib/decimal.py index 05bdc99f4e0d..539fe8dff8d6 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -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) diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index f523a721316a..fc1e04828465 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index fa88cad929c6..9fe3cfaa660c 100644 --- 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