summaryrefslogtreecommitdiff
path: root/Modules/_decimal/tests
diff options
context:
space:
mode:
authorStefan Krah <skrah@bytereef.org>2012-08-22 19:11:50 +0200
committerStefan Krah <skrah@bytereef.org>2012-08-22 19:11:50 +0200
commitad5b43995e758c7a1f81ce6cf2cd798b48712808 (patch)
tree807a78b4da935f606bfa066dde471a210a64a81a /Modules/_decimal/tests
parent2fd502f6a183fde7d8b4847d27e09884bf8006c7 (diff)
downloadcpython-git-ad5b43995e758c7a1f81ce6cf2cd798b48712808.tar.gz
In the 32-bit build, dec_hash() raised InvalidOperation if the operand
had a coefficient with MAX_PREC=425000000 digits and a negative exponent. Increasing the context limits above the official values fixes the issue and is safe (in this case!).
Diffstat (limited to 'Modules/_decimal/tests')
-rw-r--r--Modules/_decimal/tests/bignum.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/Modules/_decimal/tests/bignum.py b/Modules/_decimal/tests/bignum.py
new file mode 100644
index 0000000000..9e9e769b65
--- /dev/null
+++ b/Modules/_decimal/tests/bignum.py
@@ -0,0 +1,45 @@
+#
+# These tests require gmpy and test the limits of the 32-bit build. The
+# limits of the 64-bit build are so large that they cannot be tested
+# on accessible hardware.
+#
+
+import sys
+from decimal import *
+from gmpy import mpz
+
+
+_PyHASH_MODULUS = sys.hash_info.modulus
+# hash values to use for positive and negative infinities, and nans
+_PyHASH_INF = sys.hash_info.inf
+_PyHASH_NAN = sys.hash_info.nan
+
+# _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
+_PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
+
+def xhash(coeff, exp):
+ sign = 1
+ if coeff < 0:
+ sign = -1
+ coeff = -coeff
+ if exp >= 0:
+ exp_hash = pow(10, exp, _PyHASH_MODULUS)
+ else:
+ exp_hash = pow(_PyHASH_10INV, -exp, _PyHASH_MODULUS)
+ hash_ = coeff * exp_hash % _PyHASH_MODULUS
+ ans = hash_ if sign == 1 else -hash_
+ return -2 if ans == -1 else ans
+
+
+x = mpz(10) ** 425000000 - 1
+coeff = int(x)
+
+d = Decimal('9' * 425000000 + 'e-849999999')
+
+h1 = xhash(coeff, -849999999)
+h2 = hash(d)
+
+assert h2 == h1
+
+
+