Title: Pass opaque state to PyContext_AddWatcher callback · Issue #127124 · python/cpython · GitHub
Open Graph Title: Pass opaque state to PyContext_AddWatcher callback · Issue #127124 · python/cpython
X Title: Pass opaque state to PyContext_AddWatcher callback · Issue #127124 · python/cpython
Description: Feature or enhancement Proposal: I would like to change the Context watcher C API (new to v3.14, see #119333) to pass an opaque pointer to the callback. Specifically, change: cpython/Include/cpython/context.h Lines 39 to 55 in e8bb053 /*...
Open Graph Description: Feature or enhancement Proposal: I would like to change the Context watcher C API (new to v3.14, see #119333) to pass an opaque pointer to the callback. Specifically, change: cpython/Include/cpytho...
X Description: Feature or enhancement Proposal: I would like to change the Context watcher C API (new to v3.14, see #119333) to pass an opaque pointer to the callback. Specifically, change: cpython/Include/cpytho...
Opengraph URL: https://github.com/python/cpython/issues/127124
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pass opaque state to PyContext_AddWatcher callback","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\nI would like to change the [Context watcher C API](https://github.com/python/cpython/blob/e8bb05394164e7735f7a9de80a046953606a38eb/Include/cpython/context.h#L30-L62) (new to v3.14, see #119333) to pass an opaque pointer to the callback. Specifically, change:\r\n\r\nhttps://github.com/python/cpython/blob/e8bb05394164e7735f7a9de80a046953606a38eb/Include/cpython/context.h#L39-L55\r\n\r\nto:\r\n\r\n```c\r\n/*\r\n * Context object watcher callback function. arg is the same pointer passed to\r\n * PyContext_AddWatcher when the callback was registered. The object passed to\r\n * the callback is event-specific; see PyContextEvent for details.\r\n *\r\n * if the callback returns with an exception set, it must return -1. Otherwise\r\n * it should return 0\r\n */\r\ntypedef int PyContext_WatchCallback(\r\n void *arg, PyContextEvent event, PyObject *obj);\r\n\r\n/*\r\n * Register a per-interpreter callback that will be invoked for context events.\r\n * arg is an optional opaque pointer that is passed back to the callback; it\r\n * can be used to manage state if desired.\r\n *\r\n * Returns a handle that may be passed to PyContext_ClearWatcher on success,\r\n * or -1 and sets and error if no more handles are available.\r\n */\r\nPyAPI_FUNC(int) PyContext_AddWatcher(\r\n PyContext_WatchCallback *callback, void *arg);\r\n```\r\n\r\nThe [original idea](https://discuss.python.org/t/v3-14a1-design-limitations-of-pycontext-addwatcher/68177/2) was for the callback to get any required state from the current context, but:\r\n\r\n * The config/state is not guaranteed to be available in the context. For example, the Python code can do `ctx = contextvars.Context()` instead of `ctx = contextvars.copy_context()`. Or it can do `copy_context()`, but early during initialization and squirrel the context away for later use. The latter seems plausible for 3rd party libraries (where the user might not have much control).\r\n * The same watcher callback cannot be registered multiple times simultaneously, each with its own config/state. This came up while I was refactoring [`TestContextObjectWatchers`](https://github.com/python/cpython/blob/e8bb05394164e7735f7a9de80a046953606a38eb/Lib/test/test_capi/test_watchers.py#L590) to add some more tests for a change I’m working on.\r\n\r\nI’m not sure how likely either is to come up in normal use, but I think it is worth changing the design before the 3.14 release cements it.\r\n\r\n### Has this already been discussed elsewhere?\r\n\r\nI have already discussed this feature proposal on Discourse\r\n\r\n### Links to previous discussion of this feature:\r\n\r\nhttps://discuss.python.org/t/v3-14a1-design-limitations-of-pycontext-addwatcher/68177/4\r\n\r\ncc @fried\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-127140\n* gh-127247\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/rhansen","@type":"Person","name":"rhansen"},"datePublished":"2024-11-22T00:51:25Z","url":"https://github.com/python/cpython/issues/127124"}
| 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:f289d602-1168-9800-8d08-65cd5ed62536 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CF2E:3CBB3E:368EA35:458E3A9:6B0231AE |
| html-safe-nonce | 4ab06de7fe8506be881edbd9ca8cb2215aa24f162045ac026a05d2ccdcda8d82 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjJFOjNDQkIzRTozNjhFQTM1OjQ1OEUzQTk6NkIwMjMxQUUiLCJ2aXNpdG9yX2lkIjoiMjU0NzI5NjIzMjYyMDA0NDcxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c3f0b29e8792394b5f89b08da6f2b3d89a577d936f53d80dc5a3e5252d3a48d0 |
| hovercard-subject-tag | issue:2681439294 |
| 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/127124/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f565697bfb5280bdb55605c44859fa9a4e9adaadbbd41736e59b7a3ac54b5252/python/cpython/issues/127124 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f565697bfb5280bdb55605c44859fa9a4e9adaadbbd41736e59b7a3ac54b5252/python/cpython/issues/127124 |
| og:image:alt | Feature or enhancement Proposal: I would like to change the Context watcher C API (new to v3.14, see #119333) to pass an opaque pointer to the callback. Specifically, change: cpython/Include/cpytho... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhansen |
| 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