gh-119333: Add C api to have contextvar enter/exit callbacks (#119335)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
This commit is contained in:
Jason Fried 2024-09-23 20:40:17 -07:00 committed by GitHub
parent ad7c778546
commit d87482bc4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 402 additions and 0 deletions

View file

@ -27,6 +27,38 @@ PyAPI_FUNC(PyObject *) PyContext_CopyCurrent(void);
PyAPI_FUNC(int) PyContext_Enter(PyObject *);
PyAPI_FUNC(int) PyContext_Exit(PyObject *);
typedef enum {
Py_CONTEXT_EVENT_ENTER,
Py_CONTEXT_EVENT_EXIT,
} PyContextEvent;
/*
* A Callback to clue in non-python contexts impls about a
* change in the active python context.
*
* The callback is invoked with the event and a reference to =
* the context after its entered and before its exited.
*
* if the callback returns with an exception set, it must return -1. Otherwise
* it should return 0
*/
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
/*
* Register a per-interpreter callback that will be invoked for context object
* enter/exit events.
*
* Returns a handle that may be passed to PyContext_ClearWatcher on success,
* or -1 and sets and error if no more handles are available.
*/
PyAPI_FUNC(int) PyContext_AddWatcher(PyContext_WatchCallback callback);
/*
* Clear the watcher associated with the watcher_id handle.
*
* Returns 0 on success or -1 if no watcher exists for the provided id.
*/
PyAPI_FUNC(int) PyContext_ClearWatcher(int watcher_id);
/* Create a new context variable.