diff options
author | Kirill Podoprigora <kirill.bast9@mail.ru> | 2024-10-16 11:53:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-16 11:53:21 (GMT) |
commit | bee112a94d688c8048ddeddaa7bbd5150aecad11 (patch) | |
tree | 17d6becb2d86da4935017e77fc8b8b3271673050 /Doc/c-api | |
parent | 37e533a39716bf7da026eda2b35073ef2eb3d1fb (diff) | |
download | cpython-bee112a94d688c8048ddeddaa7bbd5150aecad11.zip cpython-bee112a94d688c8048ddeddaa7bbd5150aecad11.tar.gz cpython-bee112a94d688c8048ddeddaa7bbd5150aecad11.tar.bz2 |
gh-124872: Replace enter/exit events with "switched" (#125532)
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 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, replace the enter and exit events with
a single switched event.
The former exit event was emitted just before exiting the context.
The new switched event is 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 event type can be added in the future. It is not added here
because YAGNI.
I skipped 0 in the enum as a matter of practice. Skipping 0 makes it
easier to troubleshoot when code forgets to set zeroed memory, and it
aligns with best practices for other tools (e.g.,
https://protobuf.dev/programming-guides/dos-donts/#unspecified-enum).
Co-authored-by: Richard Hansen <rhansen@rhansen.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Doc/c-api')
-rw-r--r-- | Doc/c-api/contextvars.rst | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/Doc/c-api/contextvars.rst b/Doc/c-api/contextvars.rst index 8eba54a..b7c6550 100644 --- a/Doc/c-api/contextvars.rst +++ b/Doc/c-api/contextvars.rst @@ -123,16 +123,10 @@ Context object management functions: Enumeration of possible context object watcher events: - - ``Py_CONTEXT_EVENT_ENTER``: A context has been entered, causing the - :term:`current context` to switch to it. The object passed to the watch - callback is the now-current :class:`contextvars.Context` object. Each - enter event will eventually have a corresponding exit event for the same - context object after any subsequently entered contexts have themselves been - exited. - - ``Py_CONTEXT_EVENT_EXIT``: A context is about to be exited, which will - cause the :term:`current context` to switch back to what it was before the - context was entered. The object passed to the watch callback is the - still-current :class:`contextvars.Context` object. + - ``Py_CONTEXT_SWITCHED``: The :term:`current context` has switched to a + different context. The object passed to the watch callback is the + now-current :class:`contextvars.Context` object, or None if no context is + current. .. versionadded:: 3.14 |