10000 gh-91054: make code watcher tests resilient to other watchers · python/cpython@f32672c · GitHub
[go: up one dir, main page]

Skip to content

Commit f32672c

Browse files
committed
gh-91054: make code watcher tests resilient to other watchers
1 parent 326f0ba commit f32672c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Modules/_testcapi/watchers.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ _testcapi_unwatch_type_impl(PyObject *module, int watcher_id, PyObject *type)
295295
// Test code object watching
296296

297297
#define NUM_CODE_WATCHERS 2
298+
static int code_watcher_ids[NUM_CODE_WATCHERS] = {-1, -1};
298299
static int num_code_object_created_events[NUM_CODE_WATCHERS] = {0, 0};
299300
static int num_code_object_destroyed_events[NUM_CODE_WATCHERS] = {0, 0};
300301

@@ -345,11 +346,13 @@ add_code_watcher(PyObject *self, PyObject *which_watcher)
345346
long which_l = PyLong_AsLong(which_watcher);
346347
if (which_l == 0) {
347348
watcher_id = PyCode_AddWatcher(first_code_object_callback);
349+
code_watcher_ids[0] = watcher_id;
348350
num_code_object_created_events[0] = 0;
349351
num_code_object_destroyed_events[0] = 0;
350352
}
351353
else if (which_l == 1) {
352354
watcher_id = PyCode_AddWatcher(second_code_object_callback);
355+
code_watcher_ids[1] = watcher_id;
353356
num_code_object_created_events[1] = 0;
354357
num_code_object_destroyed_events[1] = 0;
355358
}
@@ -375,9 +378,13 @@ clear_code_watcher(PyObject *self, PyObject *watcher_id)
375378
return NULL;
376379
}
377380
// reset static events counters
378-
if (watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS) {
379-
num_code_object_created_events[watcher_id_l] = 0;
380-
num_code_object_destroyed_events[watcher_id_l] = 0;
381+
if (watcher_id_l >= 0) {
382+
for (int i = 0; i < NUM_CODE_WATCHERS; i++) {
383+
if (watcher_id_l == code_watcher_ids[i]) {
384+
num_code_object_created_events[i] = 0;
385+
num_code_object_destroyed_events[i] = 0;
386+
}
387+
}
381388
}
382389
Py_RETURN_NONE;
383390
}

0 commit comments

Comments
 (0)
0