Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rhansen
Copy link
Contributor

@rhansen rhansen commented Nov 25, 2024

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/

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];
Copy link
Contributor Author

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?

Comment on lines +106 to +112
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.
Copy link
Contributor Author

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.

@vstinner
Copy link
Member

I think that I would prefer to add a void *data (or void *ctx) parameter to the callback API, rather than moving to PyObject *callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants