-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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-124872: Replace enter/exit events with "switched" #124776
Draft
rhansen
wants to merge
7
commits into
python:main
Choose a base branch
from
rhansen:gh-119333-context-switched
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+152
−144
Conversation
This file contains 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
This was referenced Sep 30, 2024
rhansen
force-pushed
the
gh-119333-context-switched
branch
3 times, most recently
from
October 1, 2024 08:13
0b18cf1
to
8242dce
Compare
…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.
rhansen
force-pushed
the
gh-119333-context-switched
branch
from
October 1, 2024 23:23
8242dce
to
7606b67
Compare
rhansen
changed the title
gh-119333, gh-99633: Replace enter/exit events with "switched"
gh-124872: Replace enter/exit events with "switched"
Oct 1, 2024
rhansen
force-pushed
the
gh-119333-context-switched
branch
3 times, most recently
from
October 2, 2024 22:04
e68cb7f
to
d04d9c0
Compare
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.
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).
rhansen
force-pushed
the
gh-119333-context-switched
branch
from
October 10, 2024 08:45
d04d9c0
to
f3f17b3
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
contextvars.Context
#99633📚 Documentation preview 📚: https://cpython-previews--124776.org.readthedocs.build/