Title: [C API] Add `PyList_AsTupleAndClear()` function · Issue #161635 · python/cpython · GitHub
Open Graph Title: [C API] Add `PyList_AsTupleAndClear()` function · Issue #161635 · python/cpython
X Title: [C API] Add `PyList_AsTupleAndClear()` function · Issue #161635 · python/cpython
Description: Building a tuple when the size if known is advance is easy: allocate a new tuple with PyTuple_New(size) and then fill items using PyTuple_SetItem() or PyTuple_SET_ITEM(). Building a tuple when the final size is not known in advance is mo...
Open Graph Description: Building a tuple when the size if known is advance is easy: allocate a new tuple with PyTuple_New(size) and then fill items using PyTuple_SetItem() or PyTuple_SET_ITEM(). Building a tuple when the ...
X Description: Building a tuple when the size if known is advance is easy: allocate a new tuple with PyTuple_New(size) and then fill items using PyTuple_SetItem() or PyTuple_SET_ITEM(). Building a tuple when the ...
Opengraph URL: https://github.com/python/cpython/issues/161635
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[C API] Add `PyList_AsTupleAndClear()` function","articleBody":"Building a tuple when the size if known is advance is easy: allocate a new tuple with `PyTuple_New(size)` and then fill items using `PyTuple_SetItem()` or `PyTuple_SET_ITEM()`.\n\nBuilding a tuple when the final size is not known in advance is more complicated. It's possible to call `_PyTuple_Resize()` multiple time to resize the tuple. But it's more efficient to build a list, since the list is overallocated and so mutliple \"resize\" operations are more efficient.\n\nThe problem is that `PyList_AsTuple()` has to call `Py_NewRef()` on each time to create the tuple, and then call `Py_DECREF()` on each item when destroying the temporary list. I propose adding a new `PyList_AsTupleAndClear()` function to transfer items from the list to the tuple and so avoid the useless INCREF/DECREF dance.\n\n`PyList_AsTupleAndClear()` should only be called when a temporary list is created in the same function and the list cannot be exposed to Python.\n\nIn fact, Python already has an **internal** `_PyList_AsTupleAndClear()` function, since it's the most efficient way to build a tuple from a list. I just propose to make the function **public**.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161637\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-11-08T18:41:40Z","url":"https://github.com/python/cpython/issues/161635"}
| 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:a8018f7e-3614-9e5f-5751-bc94d79a5158 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ADE2:291B58:3953FDF:4898771:6B022F58 |
| html-safe-nonce | dd5ec1617534845f7c6e98a744f7b4c2c20e22d7efad9d7e3d80384862cd4d70 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBREUyOjI5MUI1ODozOTUzRkRGOjQ4OTg3NzE6NkIwMjJGNTgiLCJ2aXNpdG9yX2lkIjoiMjE3ODA3MzAwODE3MjgzODc0NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6d1fc1d16b8e82bfbfa311a0ea432e74cf2371c48d82a27a0cf959266458212c |
| hovercard-subject-tag | issue:6274284697 |
| 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/161635/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b5020d8c30061e1a9352cb5f064c9c57b423a1dd66fcec718c6a7b2994f78997/python/cpython/issues/161635 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b5020d8c30061e1a9352cb5f064c9c57b423a1dd66fcec718c6a7b2994f78997/python/cpython/issues/161635 |
| og:image:alt | Building a tuple when the size if known is advance is easy: allocate a new tuple with PyTuple_New(size) and then fill items using PyTuple_SetItem() or PyTuple_SET_ITEM(). Building a tuple when the ... |
| 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