Title: [C API] Add `PyTupleWriter` API · Issue #161694 · python/cpython · GitHub
Open Graph Title: [C API] Add `PyTupleWriter` API · Issue #161694 · python/cpython
X Title: [C API] Add `PyTupleWriter` API · Issue #161694 · python/cpython
Description: Currently, to build a tuple, PyTuple_New() should be used with PyTuple_SetItem() or PyTuple_SET_ITEM(). This API has multiple issues: it modifies an immutable tuple and the tuple is directly tracked by the GC. There is also _PyTuple_Resi...
Open Graph Description: Currently, to build a tuple, PyTuple_New() should be used with PyTuple_SetItem() or PyTuple_SET_ITEM(). This API has multiple issues: it modifies an immutable tuple and the tuple is directly tracke...
X Description: Currently, to build a tuple, PyTuple_New() should be used with PyTuple_SetItem() or PyTuple_SET_ITEM(). This API has multiple issues: it modifies an immutable tuple and the tuple is directly tracke...
Opengraph URL: https://github.com/python/cpython/issues/161694
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[C API] Add `PyTupleWriter` API","articleBody":"Currently, to build a tuple, `PyTuple_New()` should be used with `PyTuple_SetItem()` or `PyTuple_SET_ITEM()`. This API has multiple issues: it modifies an **immutable** tuple and the tuple is directly **tracked by the GC**. There is also `_PyTuple_Resize()` function to resize an **immutable** tuple.\n\nI propose adding a new `PyTupleWriter` C API to clarify the \"build\" phase which ends with a Finish() call (or Discard() on error or if the builder is no longer needed).\n\nThe problem of designing such API is that it should be *as efficient* or even more efficient than the existing `PyTuple_New()` + `PyTuple_SET_ITEM()` pattern. I made a [first attempt](https://github.com/python/cpython/pull/107139) in 2023. Last year, I made a [second attempt](https://github.com/python/cpython/issues/139888) which didn't convince other developers. The API was based on `PyTupleWriter_Create()` + `PyTupleWriter_Add()` + `PyTupleWriter_Finish()`, and `PyTupleWriter_Create()` allocated a structure on the heap memory.\n\nI'm back with a different design: `PyTupleWriter_Create()` no longer allocates a structure on the heap memory, it's just a `PyTupleObject*` pointer hidden as an opaque `PyTupleWriter*` type. So `PyTupleWriter_Create()` and `PyTupleWriter_Finish()` are fast. The API basically allocates an array of `PyObject*` and the caller has to fill the array. The array is not initialized for best performance. The caller is responsible to fill all items, otherwise the tuple contains uninitialized memory.\n\nSee https://github.com/python/cpython/issues/139888 for the rationale on adding `PyTupleWriter` API.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161639\n* gh-161908\n* gh-161911\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-11-09T17:46:17Z","url":"https://github.com/python/cpython/issues/161694"}
| 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:ba35a883-0301-6223-7863-c37904cf08f0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 925C:161320:1E26E99:27D7C51:6B0231C0 |
| html-safe-nonce | 1a7970c31de3adea21470d5b1856243df2188e317bf481d3460c7bd3d2850f42 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjVDOjE2MTMyMDoxRTI2RTk5OjI3RDdDNTE6NkIwMjMxQzAiLCJ2aXNpdG9yX2lkIjoiMzI4MjYzODQ1MDk1MTU5ODUyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3e84d3cf673249b9ad9d71cf7b03c00173bef05cbf95cf69fab0caee74bafa36 |
| hovercard-subject-tag | issue:6291484360 |
| 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/161694/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a93c4c78738ddec3ac4b1a0f2d33e8b52012df9c798406f612075e27dac5a8a2/python/cpython/issues/161694 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a93c4c78738ddec3ac4b1a0f2d33e8b52012df9c798406f612075e27dac5a8a2/python/cpython/issues/161694 |
| og:image:alt | Currently, to build a tuple, PyTuple_New() should be used with PyTuple_SetItem() or PyTuple_SET_ITEM(). This API has multiple issues: it modifies an immutable tuple and the tuple is directly tracke... |
| 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 | 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