Skip to content

Commit c1a51a8

Browse files
committed
pythongh-119333: Change PyContext_WatchCallback to take PyObject
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.
1 parent 6d0d26e commit c1a51a8

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

Doc/c-api/contextvars.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Context object management functions:
127127
128128
.. versionadded:: 3.14
129129
130-
.. c:type:: int (*PyContext_WatchCallback)(PyContextEvent event, PyContext* ctx)
130+
.. c:type:: int (*PyContext_WatchCallback)(PyContextEvent event, PyObject* ctx)
131131
132132
Type of a context object watcher callback function.
133133
If *event* is ``Py_CONTEXT_EVENT_ENTER``, then the callback is invoked

Include/cpython/context.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef enum {
4141
* if the callback returns with an exception set, it must return -1. Otherwise
4242
* it should return 0
4343
*/
44-
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
44+
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyObject *);
4545

4646
/*
4747
* Register a per-interpreter callback that will be invoked for context object

Modules/_testcapi/watchers.c

+5-5
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void notify_context_watchers(PyContextEvent event, PyContext *ctx, PyThre
124124
if (bits & 1) {
125125
PyContext_WatchCallback cb = interp->context_watchers[i];
126126
assert(cb != NULL);
127-
if (cb(event, ctx) < 0) {
127+
if (cb(event, (PyObject *)ctx) < 0) {
128128
PyErr_FormatUnraisable(
129129
"Exception ignored in %s watcher callback for %R",
130130
context_event_name(event), ctx);

0 commit comments

Comments
 (0)