gh-125512: Revert "gh-124872: Replace enter/exit events with "switched" (#124776)" (#125513)

This commit is contained in:
Kirill Podoprigora 2024-10-15 17:42:16 +03:00 committed by GitHub
parent 55c4f4c30b
commit d3c82b9cce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 118 additions and 118 deletions

View file

@ -102,8 +102,10 @@ PyContext_CopyCurrent(void)
static const char *
context_event_name(PyContextEvent event) {
switch (event) {
case Py_CONTEXT_SWITCHED:
return "Py_CONTEXT_SWITCHED";
case Py_CONTEXT_EVENT_ENTER:
return "Py_CONTEXT_EVENT_ENTER";
case Py_CONTEXT_EVENT_EXIT:
return "Py_CONTEXT_EVENT_EXIT";
default:
return "?";
}
@ -113,13 +115,6 @@ context_event_name(PyContextEvent event) {
static void
notify_context_watchers(PyThreadState *ts, PyContextEvent event, PyObject *ctx)
{
if (ctx == NULL) {
// This will happen after exiting the last context in the stack, which
// can occur if context_get was never called before entering a context
// (e.g., called `contextvars.Context().run()` on a fresh thread, as
// PyContext_Enter doesn't call context_get).
ctx = Py_None;
}
assert(Py_REFCNT(ctx) > 0);
PyInterpreterState *interp = ts->interp;
assert(interp->_initialized);
@ -180,16 +175,6 @@ PyContext_ClearWatcher(int watcher_id)
}
static inline void
context_switched(PyThreadState *ts)
{
ts->context_ver++;
// ts->context is used instead of context_get() because context_get() might
// throw if ts->context is NULL.
notify_context_watchers(ts, Py_CONTEXT_SWITCHED, ts->context);
}
static int
_PyContext_Enter(PyThreadState *ts, PyObject *octx)
{
@ -206,7 +191,9 @@ _PyContext_Enter(PyThreadState *ts, PyObject *octx)
ctx->ctx_entered = 1;
ts->context = Py_NewRef(ctx);
context_switched(ts);
ts->context_ver++;
notify_context_watchers(ts, Py_CONTEXT_EVENT_ENTER, octx);
return 0;
}
@ -240,11 +227,13 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
return -1;
}
notify_context_watchers(ts, Py_CONTEXT_EVENT_EXIT, octx);
Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
ts->context_ver++;
ctx->ctx_prev = NULL;
ctx->ctx_entered = 0;
context_switched(ts);
return 0;
}