-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-135487: fix reprlib.Repr.repr_int
when given very large integers
#135506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,7 +181,23 @@ 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))) | ||
# When math.log10(abs(x)) is overestimated or underestimated, | ||
# the number of digits should be k - 1 or k + 1 respectively. | ||
# For simplicity, we do not compute the exact number of digits. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can differ more than by 1 for large numbers with more than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, I actually didn't consider the possibility of having 2**53 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix :meth:`reprlib.Repr.repr_int <reprlib.Repr.repr_TYPE>` when given | ||
integers with more than :func:`sys.get_int_max_str_digits` digits. Patch by | ||
Bénédikt Tran. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this?
repr_TYPE
is not a real method name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was to make a link in the NEWS entry.