Title: ctypes: Switch field accessors to fixed-width integers · Issue #127295 · python/cpython · GitHub
Open Graph Title: ctypes: Switch field accessors to fixed-width integers · Issue #127295 · python/cpython
X Title: ctypes: Switch field accessors to fixed-width integers · Issue #127295 · python/cpython
Description: Feature or enhancement I believe the next step toward untangling ctypes should be switching cfield.c to be based on fixed-width integer types. This should be a pure refactoring, without user-visible behaviour changes. Currently, we use t...
Open Graph Description: Feature or enhancement I believe the next step toward untangling ctypes should be switching cfield.c to be based on fixed-width integer types. This should be a pure refactoring, without user-visibl...
X Description: Feature or enhancement I believe the next step toward untangling ctypes should be switching cfield.c to be based on fixed-width integer types. This should be a pure refactoring, without user-visibl...
Opengraph URL: https://github.com/python/cpython/issues/127295
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ctypes: Switch field accessors to fixed-width integers","articleBody":"# Feature or enhancement\n\nI believe the next step toward untangling `ctypes` should be switching `cfield.c` to be based on fixed-width integer types.\nThis should be a pure refactoring, without user-visible behaviour changes.\n\nCurrently, we use traditional native C types, usually identified by [`struct` format characters][struct-chars] when a short (and identifier-friendly) name is needed:\n- `signed char` (`b`) / `unsigned char` (`B`)\n- `short` (`h`) / `unsigned short` (`h`)\n- `int` (`i`) / `unsigned int` (`i`)\n- `long` (`l`) / `unsigned long` (`l`)\n- `long long` (`q`) / `unsigned long long` (`q`)\n\nThese map to C99 fixed-width types, which i propose switching to:\n- `int8_t`/`uint8_t`\n- `int16_t`/`uint16_t`\n- `int32_t`/`uint32_t`\n- `int64_t`/`uint64_t`\n\nThe C standard doesn't guatrantee that the “traditional” types must map to the fixints.\nBut, [`ctypes` currently requires it][swapdefs], so the assuption won't break anything.\n\nBy “map” I mean that the *size* of the types matches. The *alignment* requirements might not.\nThis needs to be kept in mind but is not an issue in `ctypes` accessors, which [explicitly handle unaligned memory][memcpy] for the integer types.\n\nNote that there are 5 “traditional” C type sizes, but 4 fixed-width ones. Two of the former are functionally identical to one another; which ones they are is platform-specific (e.g. `int`==`long`==`int32_t`.)\nThis means that one of the [current][current-impls-1] [implementations][current-impls-2] is redundant on any given platform.\n\n\nThe fixint types are parametrized by the number of bytes/bits, and one bit for signedness. This makes it easier to autogenerate code for them or to write generic macros (though generic API like [`PyLong_AsNativeBytes`][PyLong_AsNativeBytes] is problematic for performance reasons -- especially compared to a `memcpy` with compile-time-constant size).\n\nWhen one has a *different* integer type, determining the corresponding fixint means a `sizeof` and signedness check. This is easier and more robust than the current implementations (see [`wchar_t`][sizeof-wchar_t] or [`_Bool`][sizeof-bool]).\n\nThe refactoring can pave the way for:\n- Separating bitfield accessors, so bitfield logic doesn't slow down normal access. (This can be done today, but would mean another set of nearly-identical hand-written functions, which is hard to maintain, let alone experiment with. We need more metaprogramming.)\n- Integer types with arbitrary size & alignment (useful for getting the `__int128` type, or matching other platforms). This would be a new future feature.\n\n\n[swapdefs]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L420-L444\n[struct-chars]: https://docs.python.org/3/library/struct.html#format-characters\n[current-impls-1]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L470-L653\n[current-impls-2]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L703-L944\n[memcpy]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L613\n[PyLong_AsNativeBytes]: https://docs.python.org/3/c-api/long.html#c.PyLong_AsNativeBytes\n[sizeof-wchar_t]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L1547-L1555\n[sizeof-bool]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L1562-L1572\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-127297\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/encukou","@type":"Person","name":"encukou"},"datePublished":"2024-11-26T14:38:26Z","url":"https://github.com/python/cpython/issues/127295"}
| 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:34af5ff4-b852-4761-6b0c-8cf32dc2d210 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 816A:1DC4CC:1A56BE6:22FCD61:6B022041 |
| html-safe-nonce | dff403f9498dc5dc43cf9411ca6890c142b474f2bff70c6f73f160ab495aa04d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTZBOjFEQzRDQzoxQTU2QkU2OjIyRkNENjE6NkIwMjIwNDEiLCJ2aXNpdG9yX2lkIjoiMTA5MTEwODU5NDc3NjE1NDE3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 599b372f9a2d9e1507d3e3319c5914686f3b270b3f256271cb09a0c2ae06c54b |
| hovercard-subject-tag | issue:2694885143 |
| 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/127295/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b1f0aa43ba11243be5b4b3e30d47fae31eea96391cbf7acc914e3e0e7c1c1a39/python/cpython/issues/127295 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b1f0aa43ba11243be5b4b3e30d47fae31eea96391cbf7acc914e3e0e7c1c1a39/python/cpython/issues/127295 |
| og:image:alt | Feature or enhancement I believe the next step toward untangling ctypes should be switching cfield.c to be based on fixed-width integer types. This should be a pure refactoring, without user-visibl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | encukou |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width