[issue7279] decimal.py: == and != comparisons involving NaNs

Mark Dickinson report at bugs.python.org
Tue Mar 23 20:33:09 CET 2010

Mark Dickinson <dickinsm at gmail.com> added the comment:

> I think hash(Decimal("snan")) should also succeed

On second thoughts, this would be bad, since it would lead to unpredictable results for sets of dicts containing a signaling nan:

>>> from decimal import Decimal
[69536 refs]
>>> s = Decimal('snan'); h = hash(s)
[69551 refs]
>>> {s, h+1}    # can put most integers into a set with an sNaN
{Decimal('sNaN'), 373955814}
[69561 refs]
>>> {s, h}      # but not if that integer hashes equal to the sNaN...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dickinsm/python/svn/py3k/Lib/decimal.py", line 864, in __eq__
    ans = self._check_nans(other, context)
  File "/Users/dickinsm/python/svn/py3k/Lib/decimal.py", line 746, in _check_nans
  File "/Users/dickinsm/python/svn/py3k/Lib/decimal.py", line 3842, in _raise_error
    raise error(explanation)
decimal.InvalidOperation: sNaN
[69698 refs]

So if __eq__ with an sNaN raises an exception, there's little choice but to prohibit putting sNaNs into sets and dicts, and the obvious way to do this is to make __hash__ raise too.


Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list