diff options
author | Richard Hansen <rhansen@rhansen.org> | 2024-10-09 23:44:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-09 23:44:03 (GMT) |
commit | 99400930ac1d4e5e10a5ae30f8202d8bc2661e39 (patch) | |
tree | c10248b89e10f17d1ea801755e62ce469b41d5a5 /Doc/c-api | |
parent | 942916378aa6a0946b1385c2c7ca6935620d710a (diff) | |
download | cpython-99400930ac1d4e5e10a5ae30f8202d8bc2661e39.zip cpython-99400930ac1d4e5e10a5ae30f8202d8bc2661e39.tar.gz cpython-99400930ac1d4e5e10a5ae30f8202d8bc2661e39.tar.bz2 |
gh-124872: Refine contextvars documentation (#124773)
* Add definitions for "context", "current context", and "context
management protocol".
* Update related definitions to be consistent with the new
definitions.
* Restructure the documentation for the `contextvars.Context` class
to prepare for adding context manager support, and for consistency
with the definitions.
* Use `testcode` and `testoutput` to test the `Context.run` example.
* Expand the documentation for the `Py_CONTEXT_EVENT_ENTER` and
`Py_CONTEXT_EVENT_EXIT` events to clarify and to prepare for
planned changes.
Diffstat (limited to 'Doc/c-api')
-rw-r--r-- | Doc/c-api/contextvars.rst | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Doc/c-api/contextvars.rst b/Doc/c-api/contextvars.rst index 0de135b..59e74ba 100644 --- a/Doc/c-api/contextvars.rst +++ b/Doc/c-api/contextvars.rst @@ -122,18 +122,24 @@ Context object management functions: .. c:type:: PyContextEvent Enumeration of possible context object watcher events: - - ``Py_CONTEXT_EVENT_ENTER`` - - ``Py_CONTEXT_EVENT_EXIT`` + + - ``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. .. versionadded:: 3.14 .. c:type:: int (*PyContext_WatchCallback)(PyContextEvent event, PyContext* ctx) - Type of a context object watcher callback function. - If *event* is ``Py_CONTEXT_EVENT_ENTER``, then the callback is invoked - after *ctx* has been set as the current context for the current thread. - Otherwise, the callback is invoked before the deactivation of *ctx* as the current context - and the restoration of the previous contex object for the current thread. + Context object watcher callback function. The object passed to the callback + is event-specific; see :c:type:`PyContextEvent` for details. If the callback returns with an exception set, it must return ``-1``; this exception will be printed as an unraisable exception using |