Title: The C-API for Python to C integer conversion is, to be frank, a mess. · Issue #102471 · python/cpython · GitHub
Open Graph Title: The C-API for Python to C integer conversion is, to be frank, a mess. · Issue #102471 · python/cpython
X Title: The C-API for Python to C integer conversion is, to be frank, a mess. · Issue #102471 · python/cpython
Description: The C-API has built up over 30 years, in a haphazard way. So, it is no surprise that it is a bit of a mess. What makes it worse is that it is based around the C long type, which is varies in size between architectures and operating syste...
Open Graph Description: The C-API has built up over 30 years, in a haphazard way. So, it is no surprise that it is a bit of a mess. What makes it worse is that it is based around the C long type, which is varies in size b...
X Description: The C-API has built up over 30 years, in a haphazard way. So, it is no surprise that it is a bit of a mess. What makes it worse is that it is based around the C long type, which is varies in size b...
Opengraph URL: https://github.com/python/cpython/issues/102471
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"The C-API for Python to C integer conversion is, to be frank, a mess.","articleBody":"The C-API has built up over 30 years, in a haphazard way. So, it is no surprise that it is a bit of a mess.\r\nWhat makes it worse is that it is based around the C `long` type, which is varies in size between architectures and operating systems in odd ways.\r\nC `long`s are 32 bit on (almost?) all 32 bit machines, 64 bit on most 64 bit machines, except Windows when C `long`s are 32 bits on 64 bit machines. In other words, it is not a useful fixed size, like `int32_t`, nor does match the machine word size, like `intptr_t`.\r\n\r\nWe need a more consistent API for converting from Python integers to C integers and back again.\r\nWe should support both 32 bit and word size C integers. 32 bit, because we often want to store 32 bit values to save space on 64 bit machines, or for portability. We also want to support word size integers for performance and ease of coding.\r\n\r\nThis means we want 4 functions (2 sizes, 2 directions) to convert between C and Python integers.\r\n\r\nCurrently we have:\r\n\r\nWidth | Py -\u003e C | C -\u003e Py \r\n --- | --- | ----\r\n32 bit | Missing* | Missing\r\nMachine word | Missing* | `PyLong_FromSsize_t` | \r\n\r\nThe C API has a function to convert Python ints to `intptr_t`, but it is missing efficient overflow handling.\r\nIt also has a function with efficient overflow handling, `PyLong_AsLongAndOverflow`, but that returns a `long`.\r\n\r\nHere's what we want:\r\n\r\nWidth | Py -\u003e C | C -\u003e Py \r\n --- | --- | ----\r\n32 bit | `PyInt_AsInt32` | `PyInt_FromInt32`\r\nMachine word | `PyInt_AsSsize_t` | `PyInt_FromSsize_t` | \r\n\r\nI'm using `PyInt` prefix, now that Python 2 is history. It makes it clearer what is the new API.\r\n\r\nNote that I'm not handling unsigned values. I think the extra bit of precision is not worth the complexity of a larger API.\r\nAnd if we decide that they are, we can always add them later.\r\n\r\n\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-121339\n* gh-127925\n* gh-128267\n* gh-129338\n* gh-129339\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/markshannon","@type":"Person","name":"markshannon"},"datePublished":"2023-03-06T15:00:09.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/102471/cpython/issues/102471"}
| 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:b164ef53-3217-c1d0-a89a-75da50708804 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D520:3367CB:A0E37:E3276:696A5F47 |
| html-safe-nonce | b661868b0532281dae5d403ce039e0aeeec9a1617f43e19bcb85f6891ae02508 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTIwOjMzNjdDQjpBMEUzNzpFMzI3Njo2OTZBNUY0NyIsInZpc2l0b3JfaWQiOiI4NjUwMzQ5NTE5MzA5NTI4OTAzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e9d9761bbe93cb95c82e3afbb5b78ac5e40227442a51694757090d48588f719e |
| hovercard-subject-tag | issue:1611612427 |
| 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/102471/issue_layout |
| twitter:image | https://opengraph.githubassets.com/237e48ad464580337c5a404c438e5d990feb4c93080f261a125bb7ecb0c87bc7/python/cpython/issues/102471 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/237e48ad464580337c5a404c438e5d990feb4c93080f261a125bb7ecb0c87bc7/python/cpython/issues/102471 |
| og:image:alt | The C-API has built up over 30 years, in a haphazard way. So, it is no surprise that it is a bit of a mess. What makes it worse is that it is based around the C long type, which is varies in size b... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markshannon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9b7735a184970dd9333b2cbe036c8f3c0a9108c64aaa93827c5a64fc70993392 |
| 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 | 87cbd411c2982752221b5751d583a515b23bf5fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width