-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-127124: Change context watcher callback to a callable object #127247
base: main
Are you sure you want to change the base?
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.
@@ -242,7 +242,7 @@ struct _is { | |||
PyObject *audit_hooks; | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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/