Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
52b25795
Kaydet (Commit)
52b25795
authored
Ock 08, 2008
tarafından
Facundo Batista
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #1757: The hash of a Decimal instance is no longer affected
by the current context. Thanks Mark Dickinson.
üst
f66f95d4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
3 deletions
+31
-3
decimal.py
Lib/decimal.py
+11
-3
test_decimal.py
Lib/test/test_decimal.py
+17
-0
NEWS
Misc/NEWS
+3
-0
No files found.
Lib/decimal.py
Dosyayı görüntüle @
52b25795
...
...
@@ -788,8 +788,10 @@ class Decimal(object):
def
__hash__
(
self
):
"""x.__hash__() <==> hash(x)"""
# Decimal integers must hash the same as the ints
# Non-integer decimals are normalized and hashed as strings
# Normalization assures that hash(100E-1) == hash(10)
#
# The hash of a nonspecial noninteger Decimal must depend only
# on the value of that Decimal, and not on its representation.
# For example: hash(Decimal("100E-1")) == hash(Decimal("10")).
if
self
.
_is_special
:
if
self
.
_isnan
():
raise
TypeError
(
'Cannot hash a NaN value.'
)
...
...
@@ -805,7 +807,13 @@ class Decimal(object):
# 2**64-1. So we can replace hash((-1)**s*c*10**e) with
# hash((-1)**s*c*pow(10, e, 2**64-1).
return
hash
((
-
1
)
**
op
.
sign
*
op
.
int
*
pow
(
10
,
op
.
exp
,
2
**
64
-
1
))
return
hash
(
str
(
self
.
normalize
()))
# The value of a nonzero nonspecial Decimal instance is
# faithfully represented by the triple consisting of its sign,
# its adjusted exponent, and its coefficient with trailing
# zeros removed.
return
hash
((
self
.
_sign
,
self
.
_exp
+
len
(
self
.
_int
),
self
.
_int
.
rstrip
(
'0'
)))
def
as_tuple
(
self
):
"""Represents the number as a triple tuple.
...
...
Lib/test/test_decimal.py
Dosyayı görüntüle @
52b25795
...
...
@@ -980,6 +980,23 @@ class DecimalUsabilityTest(unittest.TestCase):
self
.
assert_
(
hash
(
Decimal
(
'Inf'
)))
self
.
assert_
(
hash
(
Decimal
(
'-Inf'
)))
# check that the value of the hash doesn't depend on the
# current context (issue #1757)
c
=
getcontext
()
old_precision
=
c
.
prec
x
=
Decimal
(
"123456789.1"
)
c
.
prec
=
6
h1
=
hash
(
x
)
c
.
prec
=
10
h2
=
hash
(
x
)
c
.
prec
=
16
h3
=
hash
(
x
)
self
.
assertEqual
(
h1
,
h2
)
self
.
assertEqual
(
h1
,
h3
)
c
.
prec
=
old_precision
def
test_min_and_max_methods
(
self
):
d1
=
Decimal
(
'15.32'
)
...
...
Misc/NEWS
Dosyayı görüntüle @
52b25795
...
...
@@ -348,6 +348,9 @@ Core and builtins
Library
-------
- Issue #1757: The hash of a Decimal instance is no longer affected by
the current context.
- Patch #467924: add ZipFile.extract() and ZipFile.extractall() in the
zipfile module.
...
...
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