@@ -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 };
298299static int num_code_object_created_events [NUM_CODE_WATCHERS ] = {0 , 0 };
299300static 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,14 @@ 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+ code_watcher_ids [i ] = -1 ;
385+ num_code_object_created_events [i ] = 0 ;
386+ num_code_object_destroyed_events [i ] = 0 ;
387+ }
388+ }
381389 }
382390 Py_RETURN_NONE ;
383391}
0 commit comments