gh-127124: Change context watcher callback to a callable object#127247
gh-127124: Change context watcher callback to a callable object#127247rhansen wants to merge 2 commits intopython:mainfrom
Conversation
This enables developers to associate state with the callback without relying on globals. Also, refactor the tests for improved readability and extensibility, and to cover the new state object.
| PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS]; | ||
| PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS]; | ||
| PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS]; | ||
| PyObject *context_watchers[CONTEXT_MAX_WATCHERS]; |
There was a problem hiding this comment.
Does this need a Py_VISIT somewhere? I didn't see any for the other fields in this struct, but maybe the GC does something special with this struct?
| Registers the callable object *callback* as a context object watcher for the | ||
| current interpreter. When a context event occurs, *callback* is called with | ||
| two arguments: | ||
|
|
||
| #. An event type ID from :c:type:`PyContextEvent`. | ||
| #. An object containing event-specific supplemental data; see | ||
| :c:type:`PyContextEvent` for details. |
There was a problem hiding this comment.
I think my preference would be to create a new contextvars.ContextSwitchedEvent class that holds the Context object and pass that instead, but this is a smaller change.
|
I think that I would prefer to add a |
I'm a tad concerned about |
This enables developers to associate state with the callback without relying on globals.
Also, refactor the tests for improved readability and extensibility, and to cover the new state object.
This PR is an alternative to #127140.
This PR also incorporates #124741 because I don't think it makes sense to call a callable object without backing up the exception state first.
📚 Documentation preview 📚: https://cpython-previews--127247.org.readthedocs.build/