Skip to content

Commit 330c527

Browse files
authored
gh-124872: Change PyContext_WatchCallback to take PyObject (#124737)
The PyContext struct is not intended to be public, and users of the API don't need anything more specific than PyObject. Also see gh-78943.
1 parent fa52b82 commit 330c527

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Doc/c-api/contextvars.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Context object management functions:
136136
137137
.. versionadded:: 3.14
138138
139-
.. c:type:: int (*PyContext_WatchCallback)(PyContextEvent event, PyContext* ctx)
139+
.. c:type:: int (*PyContext_WatchCallback)(PyContextEvent event, PyObject *obj)
140140
141141
Context object watcher callback function. The object passed to the callback
142142
is event-specific; see :c:type:`PyContextEvent` for details.

Include/cpython/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef enum {
5252
* if the callback returns with an exception set, it must return -1. Otherwise
5353
* it should return 0
5454
*/
55-
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
55+
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyObject *);
5656

5757
/*
5858
* Register a per-interpreter callback that will be invoked for context object

Modules/_testcapi/watchers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ static int num_context_object_enter_events[NUM_CONTEXT_WATCHERS] = {0, 0};
630630
static int num_context_object_exit_events[NUM_CONTEXT_WATCHERS] = {0, 0};
631631

632632
static int
633-
handle_context_watcher_event(int which_watcher, PyContextEvent event, PyContext *ctx) {
633+
handle_context_watcher_event(int which_watcher, PyContextEvent event, PyObject *ctx) {
634634
if (event == Py_CONTEXT_EVENT_ENTER) {
635635
num_context_object_enter_events[which_watcher]++;
636636
}
@@ -644,22 +644,22 @@ handle_context_watcher_event(int which_watcher, PyContextEvent event, PyContext
644644
}
645645

646646
static int
647-
first_context_watcher_callback(PyContextEvent event, PyContext *ctx) {
647+
first_context_watcher_callback(PyContextEvent event, PyObject *ctx) {
648648
return handle_context_watcher_event(0, event, ctx);
649649
}
650650

651651
static int
652-
second_context_watcher_callback(PyContextEvent event, PyContext *ctx) {
652+
second_context_watcher_callback(PyContextEvent event, PyObject *ctx) {
653653
return handle_context_watcher_event(1, event, ctx);
654654
}
655655

656656
static int
657-
noop_context_event_handler(PyContextEvent event, PyContext *ctx) {
657+
noop_context_event_handler(PyContextEvent event, PyObject *ctx) {
658658
return 0;
659659
}
660660

661661
static int
662-
error_context_event_handler(PyContextEvent event, PyContext *ctx) {
662+
error_context_event_handler(PyContextEvent event, PyObject *ctx) {
663663
PyErr_SetString(PyExc_RuntimeError, "boom!");
664664
return -1;
665665
}

Python/context.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ context_event_name(PyContextEvent event) {
113113
}
114114

115115
static void
116-
notify_context_watchers(PyThreadState *ts, PyContextEvent event, PyContext *ctx)
116+
notify_context_watchers(PyThreadState *ts, PyContextEvent event, PyObject *ctx)
117117
{
118118
assert(Py_REFCNT(ctx) > 0);
119119
PyInterpreterState *interp = ts->interp;
@@ -193,7 +193,7 @@ _PyContext_Enter(PyThreadState *ts, PyObject *octx)
193193
ts->context = Py_NewRef(ctx);
194194
ts->context_ver++;
195195

196-
notify_context_watchers(ts, Py_CONTEXT_EVENT_ENTER, ctx);
196+
notify_context_watchers(ts, Py_CONTEXT_EVENT_ENTER, octx);
197197
return 0;
198198
}
199199

@@ -227,7 +227,7 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
227227
return -1;
228228
}
229229

230-
notify_context_watchers(ts, Py_CONTEXT_EVENT_EXIT, ctx);
230+
notify_context_watchers(ts, Py_CONTEXT_EVENT_EXIT, octx);
231231
Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
232232
ts->context_ver++;
233233

0 commit comments

Comments
 (0)