-
-
Notifications
You must be signed in to change notification settings - Fork 33.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
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. | ||
|
||
| 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. |
Uh oh!
There was an error while loading. Please reload this page.