classification
Title: Decimal hash depends on current context
Type: Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6, Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: facundobatista Nosy List: facundobatista, mark.dickinson
Priority: normal Keywords:

Created on 2008-01-07 21:03 by mark.dickinson, last changed 2008-01-08 12:25 by facundobatista. This issue is now closed.

Files
File name Uploaded Description Edit
issue1757.patch mark.dickinson, 2008-01-08 01:12
Messages (3)
msg59492 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-01-07 21:03
The value of the Decimal hash() depends on the current context:

>>> from decimal import *
>>> x = Decimal("123456789.1")
>>> hash(x)
1989332493
>>> getcontext().prec = 6
>>> hash(x)
-2034270682

This has nasty consequences;  e.g.:

>>> s = set([x])
>>> x in s
True
>>> getcontext().prec = 28
>>> x in s
False

hashing a Decimal can also set flags in the context;  again, I think
this is undesirable.

The cause of this bug is clear:  __hash__ calls normalize, which rounds
its argument to the current context.  I'll post a fix when I get around
to it.
msg59514 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-01-08 01:12
Here's a patch for the trunk.
msg59531 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-01-08 12:25
Fixed in r59852.
History
Date User Action Args
2008-01-08 12:25:45facundobatistasetstatus: open -> closed
resolution: fixed
messages: + msg59531
2008-01-08 01:12:48mark.dickinsonsetfiles: + issue1757.patch
messages: + msg59514
2008-01-08 00:21:23christian.heimessetpriority: normal
2008-01-07 21:31:20facundobatistasetassignee: facundobatista
nosy: + facundobatista
2008-01-07 21:03:57mark.dickinsoncreate