Title: `str()` of an int with exactly `sys.get_int_max_str_digits()` digits raises `ValueError` when its leading digits are large · Issue #9797 · RustPython/RustPython · GitHub
Open Graph Title: `str()` of an int with exactly `sys.get_int_max_str_digits()` digits raises `ValueError` when its leading digits are large · Issue #9797 · RustPython/RustPython
X Title: `str()` of an int with exactly `sys.get_int_max_str_digits()` digits raises `ValueError` when its leading digits are large · Issue #9797 · RustPython/RustPython
Description: Summary Converting an int to a decimal string raises ValueError: Exceeds the limit (4300 digits) for integer string conversion for many ints that have exactly as many digits as the limit. CPython raises only when the result would have mo...
Open Graph Description: Summary Converting an int to a decimal string raises ValueError: Exceeds the limit (4300 digits) for integer string conversion for many ints that have exactly as many digits as the limit. CPython r...
X Description: Summary Converting an int to a decimal string raises ValueError: Exceeds the limit (4300 digits) for integer string conversion for many ints that have exactly as many digits as the limit. CPython r...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9797
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`str()` of an int with exactly `sys.get_int_max_str_digits()` digits raises `ValueError` when its leading digits are large","articleBody":"## Summary\n\nConverting an int to a decimal string raises `ValueError: Exceeds the limit (4300 digits) for integer string conversion` for many ints that have exactly as many digits as the limit. CPython raises only when the result would have more digits than the limit. With the default limit of 4300, `int()` accepts any 4300-digit string, but `str()` of the result fails for every value from `2**14284` (about `8.17e4299`) up, which is about the top 20% of 4300-digit values. So `str(int(s))` does not round-trip for those values. With `sys.set_int_max_str_digits(5000)` the failures start at `2**16609` (about `6.4e4999`), about 40% of 5000-digit values.\n\nThe same happens in `repr()`, f-strings, `format(x, \"d\")`, `format(x, \",\")`, `\"%d\" % x` and `b\"%d\" % x`, and for negative values. Smaller values with the same number of digits, like `2**14284 - 1`, still convert, and so do all values with fewer digits than the limit.\n\nThe [docs](https://docs.python.org/3/library/stdtypes.html#int-max-str-digits) say: \"The limit is applied to the number of digit characters in the input or output string ... When an operation would exceed the limit, a ValueError is raised\".\n\n## Reproducer\n\n```python\nimport sys\n\nprint(sys.get_int_max_str_digits()) # the default limit, 4300\nn = int(\"9\" * 4300) # parsing 4300 digits is allowed\nfor x in (n, 2**14284 - 1, 2**14284):\n try:\n s = str(x)\n print(\"ok,\", len(s), \"digits, starts with\", s[:4])\n except ValueError as e:\n print(\"ValueError:\", e)\n```\n\n## Expected\n\nCPython 3.15.2 (same on 3.13.18 and 3.12.16):\n\n```\n4300\nok, 4300 digits, starts with 9999\nok, 4300 digits, starts with 8174\nok, 4300 digits, starts with 8174\n```\n\n## Actual\n\nRustPython main (dd8c343), same on a8bd43e and b66534d:\n\n```\n4300\nValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit\nok, 4300 digits, starts with 8174\nValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit\n```\n\n## Cause\n\n`check_int_to_str_digits` ([int.rs#L831-L853](https://github.com/RustPython/RustPython/blob/a8bd43ea1119d660af14528106b659a59837ee0a/crates/vm/src/builtins/int.rs#L831-L853)), added in #7688, estimates the digit count from the bit length as `bits * 30103 / 100_000 + 1` and raises whenever that estimate is over the limit. The estimate is an upper bound and is one too high for the larger values of each digit count. For example, `2**14284` has 14285 bits, so the estimate is 4301, but the value has 4300 digits. CPython also starts with a quick check, but that check only rejects values that are certainly too long. The real comparison is made against the exact length of the converted string ([longobject.c#L2120-L2134](https://github.com/python/cpython/blob/v3.15.2/Objects/longobject.c#L2120-L2134), [#L2207-L2217](https://github.com/python/cpython/blob/v3.15.2/Objects/longobject.c#L2207-L2217)).\n\nThe value has either `digits_upper` or `digits_upper - 1` digits, so one way to fix it is to raise right away only when `digits_upper - 1 > limit`. When `digits_upper == limit + 1`, compare exactly: raise only if `abs(value) >= 10**limit`. I tried this locally. It matched the exact rule (raise only when `len(str(abs(x))) > limit`) on 1,280 values around the boundary for limits 640, 1000, 4300, 5000 and 12345, and `test_int` still passes.\n\nThe existing tests do not catch this. `test_max_str_digits_edge_cases` only converts `10 ** (maxdigits - 1)`, the smallest value with that many digits, and the snippet in `extra_tests/snippets/builtin_int.py` uses `10**4001` (4002 digits) with a limit of 4000.\n\n\u003csub>Drafted with help from Claude Code; I reviewed and checked it before posting.\u003c/sub>\n","author":{"url":"https://github.com/taxor03","@type":"Person","name":"taxor03"},"datePublished":"2026-11-11T08:27:21Z","url":"https://github.com/RustPython/RustPython/issues/9797"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b00839ce-3cc3-5731-9ec2-558560eb4267 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D5B6:3D97C8:2DC8647:3CF6156:6B01D410 |
| html-safe-nonce | ce30d19125ded043ef0b500c70d7d62de15a1b3a6ac5ea0826b94ef4146a1765 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENUI2OjNEOTdDODoyREM4NjQ3OjNDRjYxNTY6NkIwMUQ0MTAiLCJ2aXNpdG9yX2lkIjoiMzMyMjMxNTQ0ODAwNDAwNDAxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | cf04e5dbad74a05e5b68baa4f9a028b3a8acd61f14b4dcf0109ee7fc6b6dd1f5 |
| hovercard-subject-tag | issue:6324603433 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9797/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d109cf84ad0cb8bedda2e1c367db6d497c8f2110444ec5ffa9df175532b5cfaa/RustPython/RustPython/issues/9797 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d109cf84ad0cb8bedda2e1c367db6d497c8f2110444ec5ffa9df175532b5cfaa/RustPython/RustPython/issues/9797 |
| og:image:alt | Summary Converting an int to a decimal string raises ValueError: Exceeds the limit (4300 digits) for integer string conversion for many ints that have exactly as many digits as the limit. CPython r... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | taxor03 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width