mirror of
https://github.com/python/cpython.git
synced 2025-11-03 15:11:34 +00:00
[3.14] gh-135487: fix reprlib.Repr.repr_int when given very large integers (GH-135506) (#135887)
gh-135487: fix `reprlib.Repr.repr_int` when given very large integers (GH-135506)
(cherry picked from commit e5f03b94b6)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
961dd80e0e
commit
3cdb659a0c
3 changed files with 49 additions and 8 deletions
|
|
@ -181,7 +181,22 @@ def repr_str(self, x, level):
|
|||
return s
|
||||
|
||||
def repr_int(self, x, level):
|
||||
s = builtins.repr(x) # XXX Hope this isn't too slow...
|
||||
try:
|
||||
s = builtins.repr(x)
|
||||
except ValueError as exc:
|
||||
assert 'sys.set_int_max_str_digits()' in str(exc)
|
||||
# Those imports must be deferred due to Python's build system
|
||||
# where the reprlib module is imported before the math module.
|
||||
import math, sys
|
||||
# Integers with more than sys.get_int_max_str_digits() digits
|
||||
# are rendered differently as their repr() raises a ValueError.
|
||||
# See https://github.com/python/cpython/issues/135487.
|
||||
k = 1 + int(math.log10(abs(x)))
|
||||
# Note: math.log10(abs(x)) may be overestimated or underestimated,
|
||||
# but for simplicity, we do not compute the exact number of digits.
|
||||
max_digits = sys.get_int_max_str_digits()
|
||||
return (f'<{x.__class__.__name__} instance with roughly {k} '
|
||||
f'digits (limit at {max_digits}) at 0x{id(x):x}>')
|
||||
if len(s) > self.maxlong:
|
||||
i = max(0, (self.maxlong-3)//2)
|
||||
j = max(0, self.maxlong-3-i)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue