From 3a3f9cc9c1a1ba9348b98be954d0dc433aee6479 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 15 Mar 2005 23:36:19 +0000 Subject: Decimal special values did not hash properly. --- Lib/decimal.py | 4 ++++ Lib/test/test_decimal.py | 3 +++ Misc/NEWS | 3 +++ 3 files changed, 10 insertions(+) diff --git a/Lib/decimal.py b/Lib/decimal.py index 05bdc99..539fe8d 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 f523a72..fc1e048 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 fa88cad..9fe3cfa 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 -- cgit v0.12