Title: Add _DuringGC functions for tp_traverse · Issue #145921 · python/cpython · GitHub
Open Graph Title: Add _DuringGC functions for tp_traverse · Issue #145921 · python/cpython
X Title: Add _DuringGC functions for tp_traverse · Issue #145921 · python/cpython
Description: Feature or enhancement As seen in https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331/13 and capi-workgroup/decisions#95: There are newly documented restrictions on tp_traverse: The traversal function must not have an...
Open Graph Description: Feature or enhancement As seen in https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331/13 and capi-workgroup/decisions#95: There are newly documented restrictions on tp_traverse:...
X Description: Feature or enhancement As seen in https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331/13 and capi-workgroup/decisions#95: There are newly documented restrictions on tp_traverse:...
Opengraph URL: https://github.com/python/cpython/issues/145921
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add _DuringGC functions for tp_traverse","articleBody":"# Feature or enhancement\n\nAs seen in https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331/13 and https://github.com/capi-workgroup/decisions/issues/95:\n\n\nThere are [newly documented restrictions on tp_traverse](https://github.com/python/cpython/pull/142272):\n\n> The traversal function must not have any side effects. It must not modify the reference counts of any Python objects nor create or destroy any Python objects.\n\nBut, whether a C-API function modifies some reference count is an *implementation detail* that we can change at any time. Core devs can add logging, an audit event, or a warning, to virtually *any* function.\nThis implies that you can't safely use any C API function in `tp_traverse`, unless it *guarantees* that it has no side effects.\n\nI propose to add such side-effect-free versions of some functions useful in `tp_traverse`, with a `_DuringGC` suffix -- namely:\n\n- `PyObject_GetTypeData_DuringGC`\n- `PyObject_GetItemData_DuringGC`\n- `PyType_GetModuleState_DuringGC`\n- `PyModule_GetState_DuringGC`\n- `PyModule_GetToken_DuringGC`\n- `PyType_GetBaseByToken_DuringGC`\n- `PyType_GetModule_DuringGC`\n- `PyType_GetModuleByToken_DuringGC`\n\n\nCompared to their non-suffixed variants, the main difference is that we *promise* that these will remain safe to use in `tp_traverse`. The other differences are that these functions cannot fail, and (where applicable) they return borrowed references rather than strong ones.\n\nAlso, `Py_VISIT`, `Py_SIZE`, `PyObject_VisitManagedDict` as well as the `visitproc` that Python passes to `tp_traverse` will be documented to work like this.\n[edit] I'll also include a few ones useful in these: `PyObject_TypeCheck`, `PyType_IsSubtype`, `PyType_HasFeature`, `Py{\u003ctype>}_Check`, `Py{\u003ctype>}_CheckExact`.\n\nUsers that call any `_DuringGC` function (including some type's `tp_traverse`) must manually must ensure that the type, MRO, and layout of any argument doesn't concurrently change. That means only calling them from `tp_traverse`.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-145925\n* gh-160455\n* gh-160826\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/encukou","@type":"Person","name":"encukou"},"datePublished":"2026-03-13T15:17:19Z","url":"https://github.com/python/cpython/issues/145921"}
| 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:238a91eb-5bdb-2bc1-0e76-d3216ff5326f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9A4:2D14E:CF374B:11B2196:6B065048 |
| html-safe-nonce | 2324884af33364e68441222155e2e9c0a006942df6fd60f622272235ec2b54f2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUE0OjJEMTRFOkNGMzc0QjoxMUIyMTk2OjZCMDY1MDQ4IiwidmlzaXRvcl9pZCI6IjQ5MzM5ODkxNjk3NzgwNzQzMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1b6a8ca12b8637c0d2b19aba13f74ad54699ee9b764bad079176b2b617db2494 |
| hovercard-subject-tag | issue:4071745106 |
| 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/145921/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dbff1dc92ed1e4c2be6a118f231af054c801a5f998c5c4b24e5ef1ca30dc8218/python/cpython/issues/145921 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dbff1dc92ed1e4c2be6a118f231af054c801a5f998c5c4b24e5ef1ca30dc8218/python/cpython/issues/145921 |
| og:image:alt | Feature or enhancement As seen in https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331/13 and capi-workgroup/decisions#95: There are newly documented restrictions on tp_traverse:... |
| 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 | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| 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 | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width