Title: C API: Consider adding public PyLong_AsByteArray() and PyLong_FromByteArray() functions · Issue #111140 · python/cpython · GitHub
Open Graph Title: C API: Consider adding public PyLong_AsByteArray() and PyLong_FromByteArray() functions · Issue #111140 · python/cpython
X Title: C API: Consider adding public PyLong_AsByteArray() and PyLong_FromByteArray() functions · Issue #111140 · python/cpython
Description: Feature or enhancement The private _PyLong_AsByteArray() and _PyLong_FromByteArray() functions were removed in Python 3.13: see PR #108429. @scoder asked what is the intended replacement for _PyLong_FromByteArray(). The replacement for _...
Open Graph Description: Feature or enhancement The private _PyLong_AsByteArray() and _PyLong_FromByteArray() functions were removed in Python 3.13: see PR #108429. @scoder asked what is the intended replacement for _PyLon...
X Description: Feature or enhancement The private _PyLong_AsByteArray() and _PyLong_FromByteArray() functions were removed in Python 3.13: see PR #108429. @scoder asked what is the intended replacement for _PyLon...
Opengraph URL: https://github.com/python/cpython/issues/111140
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":" C API: Consider adding public PyLong_AsByteArray() and PyLong_FromByteArray() functions","articleBody":"# Feature or enhancement\r\n\r\nThe private `_PyLong_AsByteArray()` and `_PyLong_FromByteArray()` functions were removed in Python 3.13: see PR #108429.\r\n\r\n@scoder [asked](https://github.com/python/cpython/pull/108429#issuecomment-1772777608) what is the intended replacement for `_PyLong_FromByteArray()`.\r\n\r\nThe replacement for `_PyLong_FromByteArray()` is ``PyObject_CallMethod((PyObject*)\u0026PyList_Type, \"from_bytes\", \"s#s\", str, len, \"big\")`` but I'm not sure what is the easy way to set the *signed* parameter to True (default: `signed=False`).\r\n\r\nThe replacement for `_PyLong_AsByteArray()` is `PyObject_CallMethod(my_int, \"to_bytes\", \"ns\", length, \"big\")`. Same, I'm not sure how to easy set the *signed* parameter to True (default: `signed=False`).\r\n\r\nI propose to add public PyLong_AsByteArray() and PyLong_FromByteArray() functions to the C API.\r\n\r\nPython 3.12 modified PyLongObject: it's no longer a simple array of digits, but it's now a more less straightforward `_PyLongValue` structure which requires using unstable functions to access small \"compact\" values:\r\n\r\n* PyUnstable_Long_IsCompact()\r\n* PyUnstable_Long_CompactValue()\r\n\r\nSo having a reliable and simple way to import/export a Python int object as bytes became even more important.\r\n\r\n---\r\n\r\nA code search for ``_PyLong_AsByteArray `` in PyPI top 5,000 projects found 12 projects using it:\r\n\r\n* Cython (0.29.36)\r\n* blspy (2.0.2)\r\n* catboost (1.2)\r\n* fastobo (0.12.2)\r\n* gevent (22.10.2)\r\n* guppy3 (3.1.3)\r\n* line_profiler (4.0.3)\r\n* msgspec (0.16.0)\r\n* orjson (3.9.1)\r\n* pickle5 (0.0.12)\r\n* pyodbc (4.0.39)\r\n* rlp (3.0.0)\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-114886\n* gh-115375\n* gh-115380\n* gh-116053\n* gh-118612\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2023-10-20T21:39:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":61},"url":"https://github.com/111140/cpython/issues/111140"}
| 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:ec89a742-4ae8-a52f-391b-18aa855d02e0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB76:215A88:5A4222:7ECA60:696A24BF |
| html-safe-nonce | d46b2713438d46b3ac0c4d0a0f189a0dafa5f8d535dbdd41949549c127f0b345 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjc2OjIxNUE4ODo1QTQyMjI6N0VDQTYwOjY5NkEyNEJGIiwidmlzaXRvcl9pZCI6IjI1MDgyMTI4NTc4NjU1OTY3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | afdb4b9751f87ece06b98b83fc07479bf1afd8570bf5851c70cbb7a7cc6f5e1d |
| hovercard-subject-tag | issue:1955043490 |
| 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/111140/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c16fdd83c7eb269f4e6d85a4d05f6088d857d30c9bcf8ba8327f0c6aa544030a/python/cpython/issues/111140 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c16fdd83c7eb269f4e6d85a4d05f6088d857d30c9bcf8ba8327f0c6aa544030a/python/cpython/issues/111140 |
| og:image:alt | Feature or enhancement The private _PyLong_AsByteArray() and _PyLong_FromByteArray() functions were removed in Python 3.13: see PR #108429. @scoder asked what is the intended replacement for _PyLon... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | a1022f03e4f0d91ea173e4e5dac892c982e0588c62f1ce56121d755a320a3569 |
| 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 | f472b8e6c7b3fdd5d0354972a3f4c516289bf0be |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width