@@ -295,6 +295,7 @@ _testcapi_unwatch_type_impl(PyObject *module, int watcher_id, PyObject *type)
295
295
// Test code object watching
296
296
297
297
#define NUM_CODE_WATCHERS 2
298
+ static int code_watcher_ids [NUM_CODE_WATCHERS ] = {-1 , -1 };
298
299
static int num_code_object_created_events [NUM_CODE_WATCHERS ] = {0 , 0 };
299
300
static int num_code_object_destroyed_events [NUM_CODE_WATCHERS ] = {0 , 0 };
300
301
@@ -345,11 +346,13 @@ add_code_watcher(PyObject *self, PyObject *which_watcher)
345
346
long which_l = PyLong_AsLong (which_watcher );
346
347
if (which_l == 0 ) {
347
348
watcher_id = PyCode_AddWatcher (first_code_object_callback );
349
+ code_watcher_ids [0 ] = watcher_id ;
348
350
num_code_object_created_events [0 ] = 0 ;
349
351
num_code_object_destroyed_events [0 ] = 0 ;
350
352
}
351
353
else if (which_l == 1 ) {
352
354
watcher_id = PyCode_AddWatcher (second_code_object_callback );
355
+ code_watcher_ids [1 ] = watcher_id ;
353
356
num_code_object_created_events [1 ] = 0 ;
354
357
num_code_object_destroyed_events [1 ] = 0 ;
355
358
}
@@ -375,9 +378,14 @@ clear_code_watcher(PyObject *self, PyObject *watcher_id)
375
378
return NULL ;
376
379
}
377
380
// 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
+ code_watcher_ids [i ] = -1 ;
385
+ num_code_object_created_events [i ] = 0 ;
386
+ num_code_object_destroyed_events [i ] = 0 ;
387
+ }
388
+ }
381
389
}
382
390
Py_RETURN_NONE ;
383
391
}
0 commit comments