Skip to content

Commit 5f8590c

Browse files
committed
gh-124872: Change PyContext_WatchCallback return type to void
The exception is ignored so change the return type from `int` to `void` to discourage callbacks from raising an exception in the first place.
1 parent acf0fdb commit 5f8590c

File tree

4 files changed

+14
-19
lines changed

4 files changed

+14
-19
lines changed

Doc/c-api/contextvars.rst

+2-4
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:: void (*PyContext_WatchCallback)(PyContextEvent event, PyContext* ctx)
131131
132132
Type of a context object watcher callback function.
133133
If *event* is ``Py_CONTEXT_EVENT_ENTER``, then the callback is invoked
@@ -138,9 +138,7 @@ Context object management functions:
138138
Any pending exception is cleared before the callback is called and restored
139139
after the callback returns.
140140
141-
If the callback returns with an exception set, it must return ``-1``; this
142-
exception will be printed as an unraisable exception using
143-
:c:func:`PyErr_FormatUnraisable`. Otherwise it should return ``0``.
141+
If the callback raises an exception it will be ignored.
144142
145143
.. versionadded:: 3.14
146144

Include/cpython/context.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ typedef enum {
4141
* Any pending exception is cleared before the callback is called and restored
4242
* after the callback returns.
4343
*
44-
* if the callback returns with an exception set, it must return -1. Otherwise
45-
* it should return 0
44+
* If the callback raises an exception it will be ignored.
4645
*/
47-
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
46+
typedef void (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
4847

4948
/*
5049
* Register a per-interpreter callback that will be invoked for context object

Modules/_testcapi/watchers.c

+8-11
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static int context_watcher_ids[NUM_CONTEXT_WATCHERS] = {-1, -1};
629629
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

632-
static int
632+
static void
633633
handle_context_watcher_event(int which_watcher, PyContextEvent event, PyContext *ctx) {
634634
if (event == Py_CONTEXT_EVENT_ENTER) {
635635
num_context_object_enter_events[which_watcher]++;
@@ -638,30 +638,27 @@ handle_context_watcher_event(int which_watcher, PyContextEvent event, PyContext
638638
num_context_object_exit_events[which_watcher]++;
639639
}
640640
else {
641-
return -1;
641+
Py_UNREACHABLE();
642642
}
643-
return 0;
644643
}
645644

646-
static int
645+
static void
647646
first_context_watcher_callback(PyContextEvent event, PyContext *ctx) {
648-
return handle_context_watcher_event(0, event, ctx);
647+
handle_context_watcher_event(0, event, ctx);
649648
}
650649

651-
static int
650+
static void
652651
second_context_watcher_callback(PyContextEvent event, PyContext *ctx) {
653-
return handle_context_watcher_event(1, event, ctx);
652+
handle_context_watcher_event(1, event, ctx);
654653
}
655654

656-
static int
655+
static void
657656
noop_context_event_handler(PyContextEvent event, PyContext *ctx) {
658-
return 0;
659657
}
660658

661-
static int
659+
static void
662660
error_context_event_handler(PyContextEvent event, PyContext *ctx) {
663661
PyErr_SetString(PyExc_RuntimeError, "boom!");
664-
return -1;
665662
}
666663

667664
static PyObject *

Python/context.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ static void notify_context_watchers(PyContextEvent event, PyContext *ctx, PyThre
125125
PyContext_WatchCallback cb = interp->context_watchers[i];
126126
assert(cb != NULL);
127127
PyObject *exc = _PyErr_GetRaisedException(ts);
128-
if (cb(event, ctx) < 0) {
128+
cb(event, ctx);
129+
if (_PyErr_Occurred(ts) != NULL) {
129130
PyErr_FormatUnraisable(
130131
"Exception ignored in %s watcher callback for %R",
131132
context_event_name(event), ctx);

0 commit comments

Comments
 (0)