From bea3f6f5c788eb4f0f7639913ff89654152864c0 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 15 Mar 2005 04:59:17 +0000 Subject: Bug #1163325: "special" decimals aren't hashable --- 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 7f71b83..fb11e8f 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 19ce8f4..c91fb17 100644 --- 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. -- cgit v0.12