FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-127124: Change context watcher callback to a callable object by rhansen · Pull Request #127247 · python/cpython · GitHub

Repository navigation

gh-127124: Change context watcher callback to a callable object - #127247

Open
rhansen wants to merge 2 commits into
python:mainfrom
rhansen:gh-127124-context-watcher-callable
Open

rhansen wants to merge 2 commits into
python:mainfrom
rhansen:gh-127124-context-watcher-callable

Conversation

rhansen commented Nov 25, 2024 •
edited
Loading

Copy link
Copy Markdown
Contributor

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.
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 thread Doc/c-api/contextvars.rst
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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.

I'm a tad concerned about void *data. See my comment here: #127124 (comment). That said, I'm not totally opposed to it, but we should definitely explore options before introducing this (and precedent for future void *) into the C API. Do you think this is worth bringing up on https://github.com/capi-workgroup/decisions?

rhansen commented Dec 2, 2024

Copy link
Copy Markdown
Contributor Author

@vstinner So your preference would be PR #127140 (possibly with a void * instead of PyObject *) over this PR?

vstinner commented Dec 3, 2024

Copy link
Copy Markdown
Member

@vstinner So your preference would be PR #127140 (possibly with a void * instead of PyObject *) over this PR?

I would prefer void* instead of PyObject*. But I never used this API and I didn't design it. So I'm not sure that I'm the right person to make a decision here.

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL