Skip to content

Commit 1012dc1

Browse files
authored
GH-91054: Reset static events counts in code watchers tests (#99978)
1 parent 76f43fc commit 1012dc1

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Lib/test/test_capi/test_watchers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,11 @@ def test_code_object_events_dispatched(self):
383383
del co3
384384
self.assert_event_counts(2, 2, 1, 1)
385385

386-
# verify counts remain as they were after both watchers are cleared
386+
# verify counts are reset and don't change after both watchers are cleared
387387
co4 = _testcapi.code_newempty("test_watchers", "dummy4", 0)
388-
self.assert_event_counts(2, 2, 1, 1)
388+
self.assert_event_counts(0, 0, 0, 0)
389389
del co4
390-
self.assert_event_counts(2, 2, 1, 1)
390+
self.assert_event_counts(0, 0, 0, 0)
391391

392392
def test_clear_out_of_range_watcher_id(self):
393393
with self.assertRaisesRegex(ValueError, r"Invalid code watcher ID -1"):

Modules/_testcapi/watchers.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,13 @@ add_code_watcher(PyObject *self, PyObject *which_watcher)
325325
long which_l = PyLong_AsLong(which_watcher);
326326
if (which_l == 0) {
327327
watcher_id = PyCode_AddWatcher(first_code_object_callback);
328+
num_code_object_created_events[0] = 0;
329+
num_code_object_destroyed_events[0] = 0;
328330
}
329331
else if (which_l == 1) {
330332
watcher_id = PyCode_AddWatcher(second_code_object_callback);
333+
num_code_object_created_events[1] = 0;
334+
num_code_object_destroyed_events[1] = 0;
331335
}
332336
else {
333337
return NULL;
@@ -346,6 +350,11 @@ clear_code_watcher(PyObject *self, PyObject *watcher_id)
346350
if (PyCode_ClearWatcher(watcher_id_l) < 0) {
347351
return NULL;
348352
}
353+
// reset static events counters
354+
if (watcher_id_l >= 0 && watcher_id_l < NUM_CODE_WATCHERS) {
355+
num_code_object_created_events[watcher_id_l] = 0;
356+
num_code_object_destroyed_events[watcher_id_l] = 0;
357+
}
349358
Py_RETURN_NONE;
350359
}
351360

0 commit comments

Comments
 (0)