Title: FR: Change `int` repr on huge values to automatically use hexadecimal · Issue #96601 · python/cpython · GitHub
Open Graph Title: FR: Change `int` repr on huge values to automatically use hexadecimal · Issue #96601 · python/cpython
X Title: FR: Change `int` repr on huge values to automatically use hexadecimal · Issue #96601 · python/cpython
Description: Problem Now that 95778 is in, the repr of an int can fail with a ValueError based on its size because repr and str are the same for int thus huge values cannot have a repr. We discussed this while working on that security fix but deemed ...
Open Graph Description: Problem Now that 95778 is in, the repr of an int can fail with a ValueError based on its size because repr and str are the same for int thus huge values cannot have a repr. We discussed this while ...
X Description: Problem Now that 95778 is in, the repr of an int can fail with a ValueError based on its size because repr and str are the same for int thus huge values cannot have a repr. We discussed this while ...
Opengraph URL: https://github.com/python/cpython/issues/96601
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"FR: Change `int` repr on huge values to automatically use hexadecimal","articleBody":"# Problem\r\n\r\nNow that [95778](https://github.com/python/cpython/issues/95778) is in, the `repr` of an `int` can fail with a ValueError based on its size because `repr` and `str` are the same for `int` thus huge values cannot have a repr.\r\n\r\nWe discussed this while working on that security fix but deemed that changing a repr was way beyond reason for a patch release bugfix. Raising the ValueError exception highlights the point in the code that potentially needs specific attention rather than allowing a new unexpected format of data to start showing up where it hadn't previously as a result of a patch release.\r\n\r\n# Enhancement Proposal\r\n\r\nWe could fix this annoyance if we are willing to change `int`'s repr. For huge values we could automatically repr them as hexadecimal. **`str` behavior would not change.**\r\n\r\nThe auto-hex repr point needs to be at less bits than required to represent a `sys.int_info.str_digits_check_threshold` decimal digit value so that there exists no scenario in which `repr` of an `int` could fail.\r\n\r\n```python\r\n\u003e\u003e\u003e int('1'+('0'*(sys.int_info.str_digits_check_threshold-1))).bit_count()\r\n738\r\n\u003e\u003e\u003e int('1'+('0'*(sys.get_int_max_str_digits()-1))).bit_count()\r\n4966\r\n```\r\n\r\nPerhaps all integers \u003e512 bits (to pick an arbitrary nice threshold) could repr to hexadecimal:\r\n\r\n```python\r\n\u003e\u003e\u003e 2**511\r\n6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042048\r\n\u003e\u003e\u003e 2**513\r\n0x200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\r\n\u003e\u003e\u003e str(2**513)\r\n'26815615859885194199148049996411692254958731641184786755447122887443528060147093953603748596333806855380063716372972101707507765623893139892867298012168192'\r\n```\r\n\r\nEffectively this behavior:\r\n\r\n```python\r\ndef proposed_int_repr(value: int):\r\n if value.bit_count() \u003c= 512:\r\n return repr(value)\r\n else:\r\n return hex(value)\r\n```\r\n\r\n## Potential wins\r\n\r\n1) We return to **always** being able to `repr` an `int` other than a MemoryError.\r\n2) Less hacky code is needed to see the actual value of an `int` when it is huge. Notebook users for example would see the result of their huge `int` computation instead of a ValueError. It'd just be in hex. (REPLs emit the repr)\r\n On the other hand, I expect notebooks may choose to implement this in their own REPL repr code long before it is released into a CPython version that they're run on top of.\r\n3) People don't need to check for `int` and implement their own specialized repr when they _always want_ a value.\r\n4) _Minor_: People start using hexadecimal constants for huge values in code rather than decimal when they pasted them in from a REPL. Faster parsing, shorter code.\r\n\r\n## Potential disruption\r\n\r\n1) Golden value tests comparing string form data.\r\n2) Code inadvertently using the `repr` expecting to always get a decimal value. Bug in user code: Should use `str`.\r\n3) Stored reprs of data consumed at a distance by other code where it previously contained decimal values. Bug in user code: repr is not a data storage and transmission format.\r\n\r\nIf we didn't choose a low limit, but instead tied the switch over point to the largest binary value that fits within `sys.get_int_str_max_digits()` decimal digits we'd be inconsistent between environments or programs that choose to change their digits limit but would avoid emitting hexadecimal unless we had no other choice. This variant could be thought of as:\r\n\r\n```python\r\ndef digit_limit_tied_proposed_int_repr(value: int):\r\n try:\r\n return repr(value)\r\n except ValueError:\r\n return hex(value)\r\n```\r\n\r\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2022-09-05T19:58:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/96601/cpython/issues/96601"}
| 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:077b872f-d8e0-4e49-4771-a18d94e68b53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 85E2:17997A:9303CD:C56173:6969BF5D |
| html-safe-nonce | 282e734f4b27d36da9b855d44fd4c3d16a5fc635ef924754206e792bef3e2c82 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NUUyOjE3OTk3QTo5MzAzQ0Q6QzU2MTczOjY5NjlCRjVEIiwidmlzaXRvcl9pZCI6IjcxNTY2MDI0MjI1MjY4NTI5NTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9b8e9a8c3cb3a182ce811e4423279f07949e4d3b51f62e104e0e361a65f3360e |
| hovercard-subject-tag | issue:1362426119 |
| 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/python/cpython/96601/issue_layout |
| twitter:image | https://opengraph.githubassets.com/12d596ab572e927fac02f4203e50630820d2658b6a9ae52efc94279c35ff747d/python/cpython/issues/96601 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/12d596ab572e927fac02f4203e50630820d2658b6a9ae52efc94279c35ff747d/python/cpython/issues/96601 |
| og:image:alt | Problem Now that 95778 is in, the repr of an int can fail with a ValueError based on its size because repr and str are the same for int thus huge values cannot have a repr. We discussed this while ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gpshead |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width