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-119333, gh-99633: Replace enter/exit events with "switched" #124776

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

rhansen
Copy link

@rhansen rhansen commented Sep 30, 2024

Users want to know when the current context switches to a different context object. Right now this happens when and only when a context is entered or exited, so the enter and exit events are synonymous with "switched". However, if the changes proposed for gh-99633 are implemented, the current context will also switch for reasons other than context enter or exit. Since users actually care about context switches and not enter or exit, replace the enter and exit events with a single switched event.

The former exit event was emitted just before exiting the context. The new switched event is emitted after the context is exited to match the semantics users expect of an event with a past-tense name. If users need the ability to clean up before the switch takes effect, another event type can be added in the future. It is not added here because YAGNI.

This commit amends a feature that is new to v3.14 so I did not include a NEWS blurb (the existing blurb from #119335 should suffice).

This PR is marked as draft because it is based on commits from pull requests that have not yet been merged:

Once those are merged I'll rebase this and mark it as ready for review. Until then, only the tip commit is relevant to this PR.

cc @fried


📚 Documentation preview 📚: https://cpython-previews--124776.org.readthedocs.build/

The PyContext struct is not intended to be public, and users of the
API don't need anything more specific than PyObject.  Also see
pythongh-78943.
…back

I believe that the value of a simpler API (and defense against poorly
written callbacks) outweighs the cost of backing up and restoring the
thread's exception state.
The exception is ignored so change the return type from `int` to
`void` to discourage callbacks from raising an exception in the first
place.
  * Add definitions for "context", "current context", and "context
    management protocol".
  * Update related definitions to be consistent with the new
    definitions.
  * Restructure the documentation for the `contextvars.Context` class
    to prepare for adding context manager support, and for consistency
    with the definitions.
  * Use `testcode` and `testoutput` to test the `Context.run` example.
  * Expand the documentation for the `Py_CONTEXT_EVENT_ENTER` and
    `Py_CONTEXT_EVENT_EXIT` events to clarify and to prepare for
    planned changes.
…tchCallback

I changed my mind about where to back up and restore the exception.
Instead of backing up and restoring once outside the loop over the
registered callbacks, the exception is backed up and restored each
time a callback is called.

This avoids concerns about what happens if a callback raises an
exception but returns non-negative.

This is also expected to be more efficient, not less, because the
common case is no watchers at all, in which case there is no
backup/restore overhead.  The second most common case is one watcher,
which is no less efficient than before.  The only time this is more
expensive is when there are two or more watchers, in which case the
overhead of the watcher callbacks probably dwarfs the overhead of the
extra backups anyways.
…ched"

Users want to know when the current context switches to a different
context object.  Right now this happens when and only when a context
is entered or exited, so the enter and exit events are synonymous with
"switched".  However, if the changes proposed for pythongh-99633 are
implemented, the current context will also switch for reasons other
than context enter or exit.  Since users actually care about context
switches and not enter or exit, replace the enter and exit events with
a single switched event.

The former exit event was emitted just before exiting the context.
The new switched event is emitted after the context is exited to match
the semantics users expect of an event with a past-tense name.  If
users need the ability to clean up before the switch takes effect,
another event type can be added in the future.  It is not added here
because YAGNI.

I skipped 0 in the enum as a matter of practice.  Skipping 0 makes it
easier to troubleshoot when code forgets to set zeroed memory, and it
aligns with best practices for other tools (e.g.,
https://protobuf.dev/programming-guides/dos-donts/#unspecified-enum).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant