@@ -109,10 +109,21 @@ def test_error(self):
109109 self .watch (wid , d )
110110 with catch_unraisable_exception () as cm :
111111 d ["foo" ] = "bar"
112- self .assertIs (cm .unraisable .object , d )
112+ self .assertEqual (
113+ cm .unraisable .object ,
114+ f"watcher callback for <dict at { hex (id (d ))} >"
115+ )
113116 self .assertEqual (str (cm .unraisable .exc_value ), "boom!" )
114117 self .assert_events ([])
115118
119+ def test_dealloc_error (self ):
120+ d = {}
121+ with self .watcher (kind = self .ERROR ) as wid :
122+ self .watch (wid , d )
123+ with catch_unraisable_exception () as cm :
124+ del d
125+ self .assertEqual (str (cm .unraisable .exc_value ), "boom!" )
126+
116127 def test_two_watchers (self ):
117128 d1 = {}
118129 d2 = {}
@@ -389,6 +400,22 @@ def test_code_object_events_dispatched(self):
389400 del co4
390401 self .assert_event_counts (0 , 0 , 0 , 0 )
391402
403+ def test_error (self ):
404+ with self .code_watcher (2 ):
405+ with catch_unraisable_exception () as cm :
406+ co = _testcapi .code_newempty ("test_watchers" , "dummy0" , 0 )
407+
408+ self .assertEqual (cm .unraisable .object , f"watcher callback for { co !r} " )
409+ self .assertEqual (str (cm .unraisable .exc_value ), "boom!" )
410+
411+ def test_dealloc_error (self ):
412+ co = _testcapi .code_newempty ("test_watchers" , "dummy0" , 0 )
413+ with self .code_watcher (2 ):
414+ with catch_unraisable_exception () as cm :
415+ del co
416+
417+ self .assertEqual (str (cm .unraisable .exc_value ), "boom!" )
418+
392419 def test_clear_out_of_range_watcher_id (self ):
393420 with self .assertRaisesRegex (ValueError , r"Invalid code watcher ID -1" ):
394421 _testcapi .clear_code_watcher (- 1 )
@@ -479,7 +506,25 @@ def watcher(*args):
479506 def myfunc ():
480507 pass
481508
482- self .assertIs (cm .unraisable .object , myfunc )
509+ self .assertEqual (
510+ cm .unraisable .object ,
511+ f"watcher callback for { myfunc !r} "
512+ )
513+
514+ def test_dealloc_watcher_raises_error (self ):
515+ class MyError (Exception ):
516+ pass
517+
518+ def watcher (* args ):
519+ raise MyError ("testing 123" )
520+
521+ def myfunc ():
522+ pass
523+
524+ with self .add_watcher (watcher ):
525+ with catch_unraisable_exception () as cm :
526+ del myfunc
527+
483528 self .assertIsInstance (cm .unraisable .exc_value , MyError )
484529
485530 def test_clear_out_of_range_watcher_id (self ):
0 commit comments