Title: Add a public C-API function to iterate over GC’able objects · Issue #102013 · python/cpython · GitHub
Open Graph Title: Add a public C-API function to iterate over GC’able objects · Issue #102013 · python/cpython
X Title: Add a public C-API function to iterate over GC’able objects · Issue #102013 · python/cpython
Description: Feature or enhancement An API similar to: /* Visit all live GC-capable objects, similar to gc.get_objects(None). * * Users should avoid allocating or deallocating objects on the Python heap in * the callback. */ typedef void (*gcvisitobj...
Open Graph Description: Feature or enhancement An API similar to: /* Visit all live GC-capable objects, similar to gc.get_objects(None). * * Users should avoid allocating or deallocating objects on the Python heap in * th...
X Description: Feature or enhancement An API similar to: /* Visit all live GC-capable objects, similar to gc.get_objects(None). * * Users should avoid allocating or deallocating objects on the Python heap in * th...
Opengraph URL: https://github.com/python/cpython/issues/102013
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add a public C-API function to iterate over GC’able objects","articleBody":"# Feature or enhancement\r\nAn API similar to:\r\n```\r\n/* Visit all live GC-capable objects, similar to gc.get_objects(None).\r\n*\r\n* Users should avoid allocating or deallocating objects on the Python heap in\r\n* the callback. */\r\ntypedef void (*gcvisitobjects_t)(PyObject *, void *);\r\nPyAPI_FUNC(void) PyGC_VisitObjects(gcvisitobjects_t callback, void *arg);\r\n```\r\n\r\nWhich could be used as:\r\n```\r\nvoid count_functions(PyObject *op, void *arg) {\r\n if (PyFunction_Check(op)) {\r\n (*(int*)arg)++;\r\n }\r\n}\r\n\r\nint get_num_functions() {\r\n int count;\r\n PyGC_VisitObjects(count_functions, \u0026count);\r\n return count;\r\n}\r\n```\r\n\r\n# Pitch\r\nWe have a version of this in [Cinder](https://github.com/facebookincubator/cinder) already and right now and use it to [identify all generator objects so they can be de-opted when our JIT is shutdown](https://github.com/facebookincubator/cinder/blob/cinder/3.10/Jit/pyjit.cpp#L1871). In future we plan to use it for things like discovering existing PyFunction objects, and then using gh-91049 to mark them as JIT’able. This could facilitate loading the JIT feature at a later time (e.g. as part of a module).\r\n\r\n[Edited] In general, there already exists a Python API for iterating over GC’able objects via [gc.get_objects()](https://docs.python.org/3/library/gc.html#gc.get_objects), however there is no good way of doing this from native extensions. While it is technically possible to import the `gc` module and extract the `gc_objects` function in C this is cumbersome, and more importantly might lead to unexpected behavior if Python code has replaced the `gc_objects` function.\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-102014\r\n* gh-115560\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/jbower-fb","@type":"Person","name":"jbower-fb"},"datePublished":"2023-02-18T00:45:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/102013/cpython/issues/102013"}
| 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:4e88d63e-eb84-0a0b-84a9-53dc9f2cd136 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8BB4:34359D:8665FF:BCF446:696A20F6 |
| html-safe-nonce | c6870bc632e2956f020235b8627fe882c85a93f4363f6f3cedb8f11fc3de9412 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QkI0OjM0MzU5RDo4NjY1RkY6QkNGNDQ2OjY5NkEyMEY2IiwidmlzaXRvcl9pZCI6Ijg1OTA1OTY2NjcyNzIyMDg2MzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d9e086d6e16d5bfc49721cc291b89b117218a805e0178a9fabe31a4905ce2872 |
| hovercard-subject-tag | issue:1590136073 |
| 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/102013/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b52482e00077ab79ab63d19a1ca34595983d8d1f579c9824d1ca474be6143065/python/cpython/issues/102013 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b52482e00077ab79ab63d19a1ca34595983d8d1f579c9824d1ca474be6143065/python/cpython/issues/102013 |
| og:image:alt | Feature or enhancement An API similar to: /* Visit all live GC-capable objects, similar to gc.get_objects(None). * * Users should avoid allocating or deallocating objects on the Python heap in * th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jbower-fb |
| hostname | github.com |
| expected-hostname | github.com |
| None | 014f3d193f36b7d393f88ca22d06fbacd370800b40a547c1ea67291e02dc8ea3 |
| 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 | d515f6f09fa57a93bf90355cb894eb84ca4f458f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width