mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
This commit is contained in:
parent
55c4f4c30b
commit
d3c82b9cce
6 changed files with 118 additions and 118 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue