Skip to content

Convert ints with exactly the maximum number of digits in str() - #1395

Open
taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-int-str-digit-limit-boundary
Open

taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-int-str-digit-limit-boundary

Conversation

@taxor03

@taxor03 taxor03 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1391

The pre-check in IntBuiltins.StrNode.doPInt rejected every int whose bit length was >= the bit length of 10**limit. Ints from 2**(b-1) up to 10**limit - 1 share that bit length but have only limit digits, so str(), repr() and f-strings raised ValueError for them (with the default limit, every 4300-digit value from 2**14284 up). CPython raises only when the result has more digits than the limit (longobject.c#L2170-L2180).

AS-IS

>>> len(str(2**14284))
ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit

TO-BE

4300

(matching CPython)

Changes

  • In IntBuiltins.StrNode.doPInt, reject before toString() only ints whose bit length is greater than that of 10**limit; those are at least 2**b > 10**limit. Ints of exactly that bit length are left to the existing digit count after toString().
  • Add test_str_with_max_str_digits to tests/test_int.py: with the limit at 4300, 2**14284, 10**4300 - 1 and its negation convert with str() and repr() and round-trip through int(), and 10**4300 and its negation still raise ValueError. It passes on CPython 3.12, 3.13 and 3.14.

Testing

mx build on darwin-aarch64 (master 3176851), then mx graalpytest test_int (55 passed) and --tagged test_int (37 passed). Without the change, the new test fails at str(2 ** 14284). mx checkstyle --primary and mx eclipseformat --primary report nothing.

Signed-off-by: Sungmin Park <ryan040@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: str() of an int with exactly sys.get_int_max_str_digits() digits raises ValueError for the larger such values, so str(int(s)) fails

1 participant