Skip to content

Commit b6c5da7

Browse files
committed
pythongh-91053: make func watcher tests resilient to other func watchers
1 parent 02ce3d5 commit b6c5da7

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Modules/_testcapi/watchers.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,9 @@ allocate_too_many_code_watchers(PyObject *self, PyObject *args)
432432

433433
// Test function watchers
434434

435-
#define NUM_FUNC_WATCHERS 2
436-
static PyObject *pyfunc_watchers[NUM_FUNC_WATCHERS];
437-
static int func_watcher_ids[NUM_FUNC_WATCHERS] = {-1, -1};
435+
#define NUM_TEST_FUNC_WATCHERS 2
436+
static PyObject *pyfunc_watchers[NUM_TEST_FUNC_WATCHERS];
437+
static int func_watcher_ids[NUM_TEST_FUNC_WATCHERS] = {-1, -1};
438438

439439
static PyObject *
440440
get_id(PyObject *obj)
@@ -508,7 +508,7 @@ second_func_watcher_callback(PyFunction_WatchEvent event,
508508
return call_pyfunc_watcher(pyfunc_watchers[1], event, func, new_value);
509509
}
510510

511-
static PyFunction_WatchCallback func_watcher_callbacks[NUM_FUNC_WATCHERS] = {
511+
static PyFunction_WatchCallback func_watcher_callbacks[NUM_TEST_FUNC_WATCHERS] = {
512512
first_func_watcher_callback,
513513
second_func_watcher_callback
514514
};
@@ -533,26 +533,25 @@ add_func_watcher(PyObject *self, PyObject *func)
533533
return NULL;
534534
}
535535
int idx = -1;
536-
for (int i = 0; i < NUM_FUNC_WATCHERS; i++) {
536+
for (int i = 0; i < NUM_TEST_FUNC_WATCHERS; i++) {
537537
if (func_watcher_ids[i] == -1) {
538538
idx = i;
539539
break;
540540
}
541541
}
542542
if (idx == -1) {
543-
PyErr_SetString(PyExc_RuntimeError, "no free watchers");
544-
return NULL;
545-
}
546-
PyObject *result = PyLong_FromLong(idx);
547-
if (result == NULL) {
543+
PyErr_SetString(PyExc_RuntimeError, "no free test watchers");
548544
return NULL;
549545
}
550546
func_watcher_ids[idx] = PyFunction_AddWatcher(func_watcher_callbacks[idx]);
551547
if (func_watcher_ids[idx] < 0) {
552-
Py_DECREF(result);
553548
return NULL;
554549
}
555550
pyfunc_watchers[idx] = Py_NewRef(func);
551+
PyObject *result = PyLong_FromLong(func_watcher_ids[idx]);
552+
if (result == NULL) {
553+
return NULL;
554+
}
556555
return result;
557556
}
558557

@@ -569,7 +568,7 @@ clear_func_watcher(PyObject *self, PyObject *watcher_id_obj)
569568
return NULL;
570569
}
571570
int idx = -1;
572-
for (int i = 0; i < NUM_FUNC_WATCHERS; i++) {
571+
for (int i = 0; i < NUM_TEST_FUNC_WATCHERS; i++) {
573572
if (func_watcher_ids[i] == wid) {
574573
idx = i;
575574
break;

0 commit comments

Comments
 (0)