gh-108346: Fix failed benchmark in decimal (#108353)

Fix benchmark in decimal to work again after the int str conversion limits.
This commit is contained in:
Charlie Zhao 2023-09-13 12:17:55 +08:00 committed by GitHub
parent 388d91cd47
commit b39f65a495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 72 additions and 51 deletions

View File

@ -7,6 +7,8 @@
import time import time
import sys
from functools import wraps
from test.support.import_helper import import_fresh_module from test.support.import_helper import import_fresh_module
C = import_fresh_module('decimal', fresh=['_decimal']) C = import_fresh_module('decimal', fresh=['_decimal'])
@ -64,7 +66,21 @@ def factorial(n, m):
else: else:
return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m) return factorial(n, (n+m)//2) * factorial((n+m)//2 + 1, m)
# Fix failed test cases caused by CVE-2020-10735 patch.
# See gh-95778 for details.
def increase_int_max_str_digits(maxdigits):
def _increase_int_max_str_digits(func, maxdigits=maxdigits):
@wraps(func)
def wrapper(*args, **kwargs):
previous_int_limit = sys.get_int_max_str_digits()
sys.set_int_max_str_digits(maxdigits)
ans = func(*args, **kwargs)
sys.set_int_max_str_digits(previous_int_limit)
return ans
return wrapper
return _increase_int_max_str_digits
def test_calc_pi():
print("\n# ======================================================================") print("\n# ======================================================================")
print("# Calculating pi, 10000 iterations") print("# Calculating pi, 10000 iterations")
print("# ======================================================================\n") print("# ======================================================================\n")
@ -86,7 +102,8 @@ def factorial(n, m):
print("result: %s" % str(x)) print("result: %s" % str(x))
print("time: %fs\n" % (time.time()-start)) print("time: %fs\n" % (time.time()-start))
@increase_int_max_str_digits(maxdigits=10000000)
def test_factorial():
print("\n# ======================================================================") print("\n# ======================================================================")
print("# Factorial") print("# Factorial")
print("# ======================================================================\n") print("# ======================================================================\n")
@ -127,3 +144,7 @@ def factorial(n, m):
if C is not None: if C is not None:
assert(sx == sy) assert(sx == sy)
if __name__ == "__main__":
test_calc_pi()
test_factorial()