Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-43475: Fix the Python implementation of hash of Decimal NaN #26679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ number, :class:`float`, or :class:`complex`::
"""Compute the hash of a float x."""

if math.isnan(x):
return super().__hash__()
return object.__hash__(x)
elif math.isinf(x):
return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
else:
Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def __hash__(self):
if self.is_snan():
raise TypeError('Cannot hash a signaling NaN value.')
elif self.is_nan():
return super().__hash__()
return object.__hash__(self)
else:
if self._sign:
return -_PyHASH_INF
Expand Down
23 changes: 15 additions & 8 deletions 23 Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,13 +1814,7 @@ def hashit(d):

# check that hash(d) == hash(int(d)) for integral values
for value in test_values:
self.assertEqual(hashit(value), hashit(int(value)))

#the same hash that to an int
self.assertEqual(hashit(Decimal(23)), hashit(23))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered by tests above.

self.assertRaises(TypeError, hash, Decimal('sNaN'))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to test_hash_method_nan.

self.assertTrue(hashit(Decimal('Inf')))
self.assertTrue(hashit(Decimal('-Inf')))
Comment on lines -1822 to -1823
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covered by tests below.

self.assertEqual(hashit(value), hash(int(value)))

# check that the hashes of a Decimal float match when they
# represent exactly the same values
Expand All @@ -1829,7 +1823,7 @@ def hashit(d):
for s in test_strings:
f = float(s)
d = Decimal(s)
self.assertEqual(hashit(f), hashit(d))
self.assertEqual(hashit(d), hash(f))

with localcontext() as c:
# check that the value of the hash doesn't depend on the
Expand All @@ -1850,6 +1844,19 @@ def hashit(d):
x = 1100 ** 1248
self.assertEqual(hashit(Decimal(x)), hashit(x))

def test_hash_method_nan(self):
Decimal = self.decimal.Decimal
self.assertRaises(TypeError, hash, Decimal('sNaN'))
value = Decimal('NaN')
self.assertEqual(hash(value), object.__hash__(value))
class H:
def __hash__(self):
return 42
class D(Decimal, H):
pass
value = D('NaN')
self.assertEqual(hash(value), object.__hash__(value))

def test_min_and_max_methods(self):
Decimal = self.decimal.Decimal

Expand Down
19 changes: 19 additions & 0 deletions 19 Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,25 @@ def test_float_pow(self):
#self.assertTrue(0.0 < pow_op(2.0, -1047) < 1e-315)
#self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315)

def test_hash(self):
for x in range(-30, 30):
self.assertEqual(hash(float(x)), hash(x))
self.assertEqual(hash(float(sys.float_info.max)),
hash(int(sys.float_info.max)))
self.assertEqual(hash(float('inf')), sys.hash_info.inf)
self.assertEqual(hash(float('-inf')), -sys.hash_info.inf)

def test_hash_nan(self):
value = float('nan')
self.assertEqual(hash(value), object.__hash__(value))
class H:
def __hash__(self):
return 42
class F(float, H):
pass
value = F('nan')
self.assertEqual(hash(value), object.__hash__(value))


@requires_setformat
class FormatFunctionsTestCase(unittest.TestCase):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.