bpo-43475: Fix worst case collision behavior for NaN instances (GH-25493)

This commit is contained in:
Raymond Hettinger 2021-04-22 08:34:57 -07:00 committed by GitHub
parent accea7dc2b
commit a07da09ad5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 25 additions and 21 deletions

View file

@ -692,10 +692,9 @@ Here are the rules in detail:
as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with
``-2``.
- The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf``
and ``sys.hash_info.nan`` are used as hash values for positive
infinity, negative infinity, or nans (respectively). (All hashable
nans have the same hash value.)
- The particular values ``sys.hash_info.inf`` and ``-sys.hash_info.inf``
are used as hash values for positive
infinity or negative infinity (respectively).
- For a :class:`complex` number ``z``, the hash values of the real
and imaginary parts are combined by computing ``hash(z.real) +
@ -740,7 +739,7 @@ number, :class:`float`, or :class:`complex`::
"""Compute the hash of a float x."""
if math.isnan(x):
return sys.hash_info.nan
return super().__hash__()
elif math.isinf(x):
return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
else:

View file

@ -855,7 +855,7 @@ always available.
+---------------------+--------------------------------------------------+
| :const:`inf` | hash value returned for a positive infinity |
+---------------------+--------------------------------------------------+
| :const:`nan` | hash value returned for a nan |
| :const:`nan` | (this attribute is no longer used) |
+---------------------+--------------------------------------------------+
| :const:`imag` | multiplier used for the imaginary part of a |
| | complex number |