Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
dc817b22
Commit
dc817b22
authored
Nov 17, 2010
by
Stefan Krah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #10356: Decimal(-1).__hash__() should equal hash(Decimal(-1)).
parent
3ec60183
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
15 deletions
+25
-15
Lib/decimal.py
Lib/decimal.py
+2
-1
Lib/test/test_decimal.py
Lib/test/test_decimal.py
+21
-14
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/decimal.py
View file @
dc817b22
...
@@ -957,7 +957,8 @@ class Decimal(object):
...
@@ -957,7 +957,8 @@ class Decimal(object):
else
:
else
:
exp_hash
=
pow
(
_PyHASH_10INV
,
-
self
.
_exp
,
_PyHASH_MODULUS
)
exp_hash
=
pow
(
_PyHASH_10INV
,
-
self
.
_exp
,
_PyHASH_MODULUS
)
hash_
=
int
(
self
.
_int
)
*
exp_hash
%
_PyHASH_MODULUS
hash_
=
int
(
self
.
_int
)
*
exp_hash
%
_PyHASH_MODULUS
return
hash_
if
self
>=
0
else
-
hash_
ans
=
hash_
if
self
>=
0
else
-
hash_
return
-
2
if
ans
==
-
1
else
ans
def
as_tuple
(
self
):
def
as_tuple
(
self
):
"""Represents the number as a triple tuple.
"""Represents the number as a triple tuple.
...
...
Lib/test/test_decimal.py
View file @
dc817b22
...
@@ -1299,19 +1299,26 @@ class DecimalUsabilityTest(unittest.TestCase):
...
@@ -1299,19 +1299,26 @@ class DecimalUsabilityTest(unittest.TestCase):
self
.
assertEqual
(
id
(
dc
),
id
(
d
))
self
.
assertEqual
(
id
(
dc
),
id
(
d
))
def
test_hash_method
(
self
):
def
test_hash_method
(
self
):
def
hashit
(
d
):
a
=
hash
(
d
)
b
=
d
.
__hash__
()
self
.
assertEqual
(
a
,
b
)
return
a
#just that it's hashable
#just that it's hashable
hash
(
Decimal
(
23
))
hash
it
(
Decimal
(
23
))
hash
(
Decimal
(
'Infinity'
))
hash
it
(
Decimal
(
'Infinity'
))
hash
(
Decimal
(
'-Infinity'
))
hash
it
(
Decimal
(
'-Infinity'
))
hash
(
Decimal
(
'nan123'
))
hash
it
(
Decimal
(
'nan123'
))
hash
(
Decimal
(
'-NaN'
))
hash
it
(
Decimal
(
'-NaN'
))
test_values
=
[
Decimal
(
sign
*
(
2
**
m
+
n
))
test_values
=
[
Decimal
(
sign
*
(
2
**
m
+
n
))
for
m
in
[
0
,
14
,
15
,
16
,
17
,
30
,
31
,
for
m
in
[
0
,
14
,
15
,
16
,
17
,
30
,
31
,
32
,
33
,
62
,
63
,
64
,
65
,
66
]
32
,
33
,
6
1
,
6
2
,
63
,
64
,
65
,
66
]
for
n
in
range
(
-
10
,
10
)
for
n
in
range
(
-
10
,
10
)
for
sign
in
[
-
1
,
1
]]
for
sign
in
[
-
1
,
1
]]
test_values
.
extend
([
test_values
.
extend
([
Decimal
(
"-1"
),
# ==> -2
Decimal
(
"-0"
),
# zeros
Decimal
(
"-0"
),
# zeros
Decimal
(
"0.00"
),
Decimal
(
"0.00"
),
Decimal
(
"-0.000"
),
Decimal
(
"-0.000"
),
...
@@ -1335,13 +1342,13 @@ class DecimalUsabilityTest(unittest.TestCase):
...
@@ -1335,13 +1342,13 @@ class DecimalUsabilityTest(unittest.TestCase):
# check that hash(d) == hash(int(d)) for integral values
# check that hash(d) == hash(int(d)) for integral values
for
value
in
test_values
:
for
value
in
test_values
:
self
.
assertEqual
(
hash
(
value
),
hash
(
int
(
value
)))
self
.
assertEqual
(
hash
it
(
value
),
hashit
(
int
(
value
)))
#the same hash that to an int
#the same hash that to an int
self
.
assertEqual
(
hash
(
Decimal
(
23
)),
hash
(
23
))
self
.
assertEqual
(
hash
it
(
Decimal
(
23
)),
hashit
(
23
))
self
.
assertRaises
(
TypeError
,
hash
,
Decimal
(
'sNaN'
))
self
.
assertRaises
(
TypeError
,
hash
,
Decimal
(
'sNaN'
))
self
.
assertTrue
(
hash
(
Decimal
(
'Inf'
)))
self
.
assertTrue
(
hash
it
(
Decimal
(
'Inf'
)))
self
.
assertTrue
(
hash
(
Decimal
(
'-Inf'
)))
self
.
assertTrue
(
hash
it
(
Decimal
(
'-Inf'
)))
# check that the hashes of a Decimal float match when they
# check that the hashes of a Decimal float match when they
# represent exactly the same values
# represent exactly the same values
...
@@ -1350,7 +1357,7 @@ class DecimalUsabilityTest(unittest.TestCase):
...
@@ -1350,7 +1357,7 @@ class DecimalUsabilityTest(unittest.TestCase):
for
s
in
test_strings
:
for
s
in
test_strings
:
f
=
float
(
s
)
f
=
float
(
s
)
d
=
Decimal
(
s
)
d
=
Decimal
(
s
)
self
.
assertEqual
(
hash
(
f
),
hash
(
d
))
self
.
assertEqual
(
hash
it
(
f
),
hashit
(
d
))
# check that the value of the hash doesn't depend on the
# check that the value of the hash doesn't depend on the
# current context (issue #1757)
# current context (issue #1757)
...
@@ -1359,11 +1366,11 @@ class DecimalUsabilityTest(unittest.TestCase):
...
@@ -1359,11 +1366,11 @@ class DecimalUsabilityTest(unittest.TestCase):
x
=
Decimal
(
"123456789.1"
)
x
=
Decimal
(
"123456789.1"
)
c
.
prec
=
6
c
.
prec
=
6
h1
=
hash
(
x
)
h1
=
hash
it
(
x
)
c
.
prec
=
10
c
.
prec
=
10
h2
=
hash
(
x
)
h2
=
hash
it
(
x
)
c
.
prec
=
16
c
.
prec
=
16
h3
=
hash
(
x
)
h3
=
hash
it
(
x
)
self
.
assertEqual
(
h1
,
h2
)
self
.
assertEqual
(
h1
,
h2
)
self
.
assertEqual
(
h1
,
h3
)
self
.
assertEqual
(
h1
,
h3
)
...
...
Misc/NEWS
View file @
dc817b22
...
@@ -78,6 +78,8 @@ Core and Builtins
...
@@ -78,6 +78,8 @@ Core and Builtins
Library
Library
-------
-------
- Issue #10356: Decimal.__hash__(-1) should return -2.
- Issue #1553375: logging: Added stack_info kwarg to display stack information.
- Issue #1553375: logging: Added stack_info kwarg to display stack information.
- Issue #5111: IPv6 Host in the Header is wrapped inside [ ]. Patch by Chandru.
- Issue #5111: IPv6 Host in the Header is wrapped inside [ ]. Patch by Chandru.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment