Title: Replace context enter and exit events with single "context switched" event · Issue #124872 · python/cpython · GitHub
Open Graph Title: Replace context enter and exit events with single "context switched" event · Issue #124872 · python/cpython
X Title: Replace context enter and exit events with single "context switched" event · Issue #124872 · python/cpython
Description: The Python programming language. Contribute to python/cpython development by creating an account on GitHub.
Open Graph Description: The Python programming language. Contribute to python/cpython development by creating an account on GitHub.
X Description: The Python programming language. Contribute to python/cpython development by creating an account on GitHub.
Opengraph URL: https://github.com/python/cpython
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Replace context enter and exit events with single \"context switched\" event","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\n(split off of https://github.com/python/cpython/issues/99633#issuecomment-2380302948 to keep related pull requests and discussion better organized)\r\n\r\nStarting in v3.14, C code can subscribe to \"context enter\" and \"context exit\" events:\r\n\r\nhttps://github.com/python/cpython/blob/120729d862f0ef9979508899f7f63a9f3d9623cb/Include/cpython/context.h#L30-L60\r\n\r\nSee gh-119333 for some background.\r\n\r\nI would like to replace the just-added \"context enter\" and \"context exit\" events with a single \"context switched\" event:\r\n\r\n```diff\r\ndiff --git a/Include/cpython/context.h b/Include/cpython/context.h\r\nindex ec72966e82c..f9638b0abe5 100644\r\n--- a/Include/cpython/context.h\r\n+++ b/Include/cpython/context.h\r\n@@ -28,8 +28,12 @@ PyAPI_FUNC(int) PyContext_Enter(PyObject *);\r\n PyAPI_FUNC(int) PyContext_Exit(PyObject *);\r\n \r\n typedef enum {\r\n- Py_CONTEXT_EVENT_ENTER,\r\n- Py_CONTEXT_EVENT_EXIT,\r\n+ /*\r\n+ * The current context has switched to a different context. The object\r\n+ * passed to the watch callback is the now-current contextvars.Context\r\n+ * object.\r\n+ */\r\n+ Py_CONTEXT_SWITCHED = 1,\r\n } PyContextEvent;\r\n \r\n /*\r\ndiff --git a/Python/context.c b/Python/context.c\r\nindex ddb03555f9e..6c9f68583c2 100644\r\n--- a/Python/context.c\r\n+++ b/Python/context.c\r\n@@ -192,7 +192,7 @@ _PyContext_Enter(PyThreadState *ts, PyObject *octx)\r\n ts->context = Py_NewRef(ctx);\r\n ts->context_ver++;\r\n \r\n- notify_context_watchers(Py_CONTEXT_EVENT_ENTER, ctx, ts);\r\n+ notify_context_watchers(Py_CONTEXT_SWITCHED, ctx, ts);\r\n return 0;\r\n }\r\n \r\n@@ -226,13 +226,13 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)\r\n return -1;\r\n }\r\n \r\n- notify_context_watchers(Py_CONTEXT_EVENT_EXIT, ctx, ts);\r\n Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);\r\n ts->context_ver++;\r\n \r\n ctx->ctx_prev = NULL;\r\n ctx->ctx_entered = 0;\r\n \r\n+ notify_context_watchers(Py_CONTEXT_SWITCHED, (PyContext *)ts->context, ts);\r\n return 0;\r\n }\r\n \r\n```\r\n\r\nRationale: Users want to know when the current context switches to a different context object. Right now this happens when and only when a context is entered or exited, so the enter and exit events are synonymous with \"switched\". However, if the [changes proposed for gh-99633](https://github.com/python/cpython/issues/99633#issuecomment-2377915344) are implemented, the current context will also switch for reasons other than context enter or exit. Since users actually care about context switches and not enter or exit, replacing the enter and exit events with a single switched event would make the new feature compatible with gh-99633.\r\n\r\nThe current \"context exit\" event is emitted just before exiting the context. The new \"context switched\" event would be emitted *after* the context is exited to match the semantics users expect of an event with a past-tense name. If users need the ability to clean up before the switch takes effect, another \"context is about to switch\" event can be added in the future. I am not proposing to add such an event for this issue because no users have requested it yet ([YAGNI](https://en.wikipedia.org/wiki/You_aren't_gonna_need_it)).\r\n\r\nI would love to get this resolved before v3.14 is released so that we don't have to worry about breaking backwards compatibility.\r\n\r\n### Has this already been discussed elsewhere?\r\n\r\nI have already discussed this feature proposal on another bug report.\r\n\r\n### Links to previous discussion of this feature:\r\n\r\nhttps://github.com/python/cpython/issues/99633#issuecomment-2380302948\r\n\r\n\u003c!-- gh-linked-prs -->\r\n### Linked PRs\r\n* gh-124774\r\n* gh-124773\r\n* gh-124741\r\n* gh-124737\r\n* gh-124776\r\n* gh-125233\n* gh-125285\n* gh-125532\n* gh-125638\n\u003c!-- /gh-linked-prs -->\r\n","author":{"url":"https://github.com/rhansen","@type":"Person","name":"rhansen"},"datePublished":"2024-10-01T23:05:03Z","url":"https://github.com/python/cpython/issues/124872"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:389cc24f-858f-f668-9279-1fbcc7db6efe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D7D0:20AA01:4B03951:5EF929B:6B023F33 |
| html-safe-nonce | 3760744713ed82e5268d1732636c734b67e3ba688ba2a7b60369d43cccfbdb0a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEN0QwOjIwQUEwMTo0QjAzOTUxOjVFRjkyOUI6NkIwMjNGMzMiLCJ2aXNpdG9yX2lkIjoiMjQzOTgwOTI1NTc4ODkyMDYyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c5414ba207a9156452121f0e56a1bde46d155cf06d1b844a764b5b2a8a842787 |
| hovercard-subject-tag | repository:81598961 |
| 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/python/cpython/issues/124872 |
| twitter:image | https://opengraph.githubassets.com/d06ca9fc1730d9116a0c91c844c99bc143903692f02ecd5256e3fd389c305c19/python/cpython |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d06ca9fc1730d9116a0c91c844c99bc143903692f02ecd5256e3fd389c305c19/python/cpython |
| og:image:alt | The Python programming language. Contribute to python/cpython development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-cache |
| 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