From ac1bb132f6e94059f3601bdb6224042851574e81 Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Tue, 24 Sep 2024 09:42:42 -0700 Subject: [PATCH 1/3] PyContext watchers notify get interp from tstate --- Include/cpython/context.h | 6 +++--- Python/context.c | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Include/cpython/context.h b/Include/cpython/context.h index a509f4eaba3d77..a80b604726d24f 100644 --- a/Include/cpython/context.h +++ b/Include/cpython/context.h @@ -33,10 +33,10 @@ typedef enum { } PyContextEvent; /* - * A Callback to clue in non-python contexts impls about a - * change in the active python context. + * A Callback to signal non-python contexts impls when a context + * object undergoes a PyContext_Enter or PyContext_Exit * - * The callback is invoked with the event and a reference to = + * The callback is invoked with the event and a reference to * the context after its entered and before its exited. * * if the callback returns with an exception set, it must return -1. Otherwise diff --git a/Python/context.c b/Python/context.c index e52efbb6516d5c..1734eee67a3dd0 100644 --- a/Python/context.c +++ b/Python/context.c @@ -115,7 +115,9 @@ context_event_name(PyContextEvent event) { static void notify_context_watchers(PyContextEvent event, PyContext *ctx) { assert(Py_REFCNT(ctx) > 0); - PyInterpreterState *interp = _PyInterpreterState_GET(); + PyThreadState *ts = _PyThreadState_GET(); + assert(ts != NULL); + PyInterpreterState *interp = ts->interp; assert(interp->_initialized); uint8_t bits = interp->active_context_watchers; int i = 0; From b08940ee324ae785a635154f9274636c07eda33d Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Wed, 25 Sep 2024 10:24:42 -0700 Subject: [PATCH 2/3] pass in ts from the Exit and Enter functions --- Python/context.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Python/context.c b/Python/context.c index 1734eee67a3dd0..ddb03555f9e402 100644 --- a/Python/context.c +++ b/Python/context.c @@ -112,11 +112,9 @@ context_event_name(PyContextEvent event) { Py_UNREACHABLE(); } -static void notify_context_watchers(PyContextEvent event, PyContext *ctx) +static void notify_context_watchers(PyContextEvent event, PyContext *ctx, PyThreadState *ts) { assert(Py_REFCNT(ctx) > 0); - PyThreadState *ts = _PyThreadState_GET(); - assert(ts != NULL); PyInterpreterState *interp = ts->interp; assert(interp->_initialized); uint8_t bits = interp->active_context_watchers; @@ -194,7 +192,7 @@ _PyContext_Enter(PyThreadState *ts, PyObject *octx) ts->context = Py_NewRef(ctx); ts->context_ver++; - notify_context_watchers(Py_CONTEXT_EVENT_ENTER, ctx); + notify_context_watchers(Py_CONTEXT_EVENT_ENTER, ctx, ts); return 0; } @@ -228,7 +226,7 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx) return -1; } - notify_context_watchers(Py_CONTEXT_EVENT_EXIT, ctx); + notify_context_watchers(Py_CONTEXT_EVENT_EXIT, ctx, ts); Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev); ts->context_ver++; From 1de2942899749e67c58f6f60b5111277d2706301 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Thu, 26 Sep 2024 10:30:48 +0530 Subject: [PATCH 3/3] fix comment --- Include/cpython/context.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Include/cpython/context.h b/Include/cpython/context.h index a80b604726d24f..ec72966e82c6f9 100644 --- a/Include/cpython/context.h +++ b/Include/cpython/context.h @@ -33,8 +33,7 @@ typedef enum { } PyContextEvent; /* - * A Callback to signal non-python contexts impls when a context - * object undergoes a PyContext_Enter or PyContext_Exit + * Callback to be invoked when a context object is entered or exited. * * The callback is invoked with the event and a reference to * the context after its entered and before its exited.