mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
Bug #1163325: "special" decimals aren't hashable
This commit is contained in:
parent
141f41ae1a
commit
bea3f6f5c7
3 changed files with 10 additions and 0 deletions
|
|
@ -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):
|
||||||
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):
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,9 @@ Extension Modules
|
||||||
Library
|
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
|
- Patch #918101: Add tarfile open mode r|* for auto-detection of the
|
||||||
stream compression; add, for symmetry reasons, r:* as a synonym of r.
|
stream compression; add, for symmetry reasons, r:* as a synonym of r.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue