@@ -31,6 +31,11 @@ def watcher(self, kind=EVENTS):
3131 finally :
3232 self .clear_watcher (wid )
3333
34+ def unassigned_watcher_id (self ):
35+ wid = self .add_watcher ()
36+ self .clear_watcher (wid )
37+ return wid
38+
3439 def assert_events (self , expected ):
3540 actual = _testcapi .get_dict_watcher_events ()
3641 self .assertEqual (actual , expected )
@@ -186,8 +191,9 @@ def test_watch_out_of_range_watcher_id(self):
186191
187192 def test_watch_unassigned_watcher_id (self ):
188193 d = {}
189- with self .assertRaisesRegex (ValueError , r"No dict watcher set for ID 3" ):
190- self .watch (3 , d )
194+ wid = self .unassigned_watcher_id ()
195+ with self .assertRaisesRegex (ValueError , rf"No dict watcher set for ID { wid } " ):
196+ self .watch (wid , d )
191197
192198 def test_unwatch_non_dict (self ):
193199 with self .watcher () as wid :
@@ -204,8 +210,9 @@ def test_unwatch_out_of_range_watcher_id(self):
204210
205211 def test_unwatch_unassigned_watcher_id (self ):
206212 d = {}
207- with self .assertRaisesRegex (ValueError , r"No dict watcher set for ID 3" ):
208- self .unwatch (3 , d )
213+ wid = self .unassigned_watcher_id ()
214+ with self .assertRaisesRegex (ValueError , rf"No dict watcher set for ID { wid } " ):
215+ self .unwatch (wid , d )
209216
210217 def test_clear_out_of_range_watcher_id (self ):
211218 with self .assertRaisesRegex (ValueError , r"Invalid dict watcher ID -1" ):
@@ -214,8 +221,9 @@ def test_clear_out_of_range_watcher_id(self):
214221 self .clear_watcher (8 ) # DICT_MAX_WATCHERS = 8
215222
216223 def test_clear_unassigned_watcher_id (self ):
217- with self .assertRaisesRegex (ValueError , r"No dict watcher set for ID 3" ):
218- self .clear_watcher (3 )
224+ wid = self .unassigned_watcher_id ()
225+ with self .assertRaisesRegex (ValueError , rf"No dict watcher set for ID { wid } " ):
226+ self .clear_watcher (wid )
219227
220228
221229class TestTypeWatchers (unittest .TestCase ):
@@ -242,6 +250,11 @@ def watcher(self, kind=TYPES):
242250 finally :
243251 self .clear_watcher (wid )
244252
253+ def unassigned_watcher_id (self ):
254+ wid = self .add_watcher ()
255+ self .clear_watcher (wid )
256+ return wid
257+
245258 def assert_events (self , expected ):
246259 actual = _testcapi .get_type_modified_events ()
247260 self .assertEqual (actual , expected )
@@ -319,7 +332,7 @@ class C: pass
319332 C .foo = "bar"
320333 self .assertEqual (
321334 cm .unraisable .err_msg ,
322- f"Exception ignored in type watcher callback #1 for { C !r} " ,
335+ f"Exception ignored in type watcher callback #{ wid } for { C !r} " ,
323336 )
324337 self .assertIs (cm .unraisable .object , None )
325338 self .assertEqual (str (cm .unraisable .exc_value ), "boom!" )
@@ -363,8 +376,9 @@ class C: pass
363376
364377 def test_watch_unassigned_watcher_id (self ):
365378 class C : pass
366- with self .assertRaisesRegex (ValueError , r"No type watcher set for ID 1" ):
367- self .watch (1 , C )
379+ wid = self .unassigned_watcher_id ()
380+ with self .assertRaisesRegex (ValueError , rf"No type watcher set for ID { wid } " ):
381+ self .watch (wid , C )
368382
369383 def test_unwatch_non_type (self ):
370384 with self .watcher () as wid :
@@ -380,8 +394,9 @@ class C: pass
380394
381395 def test_unwatch_unassigned_watcher_id (self ):
382396 class C : pass
383- with self .assertRaisesRegex (ValueError , r"No type watcher set for ID 1" ):
384- self .unwatch (1 , C )
397+ wid = self .unassigned_watcher_id ()
398+ with self .assertRaisesRegex (ValueError , rf"No type watcher set for ID { wid } " ):
399+ self .unwatch (wid , C )
385400
386401 def test_clear_out_of_range_watcher_id (self ):
387402 with self .assertRaisesRegex (ValueError , r"Invalid type watcher ID -1" ):
@@ -390,8 +405,9 @@ def test_clear_out_of_range_watcher_id(self):
390405 self .clear_watcher (self .TYPE_MAX_WATCHERS )
391406
392407 def test_clear_unassigned_watcher_id (self ):
393- with self .assertRaisesRegex (ValueError , r"No type watcher set for ID 1" ):
394- self .clear_watcher (1 )
408+ wid = self .unassigned_watcher_id ()
409+ with self .assertRaisesRegex (ValueError , rf"No type watcher set for ID { wid } " ):
410+ self .clear_watcher (wid )
395411
396412 def test_watch_type_dealloc (self ):
397413 # Use the NAME watcher (kind=3) which records the type's name as a
@@ -430,6 +446,11 @@ def code_watcher(self, which_watcher):
430446 finally :
431447 _testcapi .clear_code_watcher (wid )
432448
449+ def unassigned_watcher_id (self , which_watcher = 0 ):
450+ wid = _testcapi .add_code_watcher (which_watcher )
451+ _testcapi .clear_code_watcher (wid )
452+ return wid
453+
433454 def assert_event_counts (self , exp_created_0 , exp_destroyed_0 ,
434455 exp_created_1 , exp_destroyed_1 ):
435456 gc_collect () # code objects are collected by GC in free-threaded build
@@ -504,8 +525,9 @@ def test_clear_out_of_range_watcher_id(self):
504525 _testcapi .clear_code_watcher (8 ) # CODE_MAX_WATCHERS = 8
505526
506527 def test_clear_unassigned_watcher_id (self ):
507- with self .assertRaisesRegex (ValueError , r"No code watcher set for ID 1" ):
508- _testcapi .clear_code_watcher (1 )
528+ wid = self .unassigned_watcher_id ()
529+ with self .assertRaisesRegex (ValueError , rf"No code watcher set for ID { wid } " ):
530+ _testcapi .clear_code_watcher (wid )
509531
510532 def test_allocate_too_many_watchers (self ):
511533 with self .assertRaisesRegex (RuntimeError , r"no more code watcher IDs available" ):
@@ -521,6 +543,13 @@ def add_watcher(self, func):
521543 finally :
522544 _testcapi .clear_func_watcher (wid )
523545
546+ def unassigned_watcher_id (self , which_watcher = 0 ):
547+ def func (* args ):
548+ pass
549+ wid = _testcapi .add_func_watcher (func )
550+ _testcapi .clear_func_watcher (wid )
551+ return wid
552+
524553 def test_func_events_dispatched (self ):
525554 events = []
526555 def watcher (* args ):
@@ -621,8 +650,9 @@ def test_clear_out_of_range_watcher_id(self):
621650 _testcapi .clear_func_watcher (8 ) # FUNC_MAX_WATCHERS = 8
622651
623652 def test_clear_unassigned_watcher_id (self ):
624- with self .assertRaisesRegex (ValueError , r"no func watcher set for ID 1" ):
625- _testcapi .clear_func_watcher (1 )
653+ wid = self .unassigned_watcher_id ()
654+ with self .assertRaisesRegex (ValueError , rf"no func watcher set for ID { wid } " ):
655+ _testcapi .clear_func_watcher (wid )
626656
627657 def test_allocate_too_many_watchers (self ):
628658 with self .assertRaisesRegex (RuntimeError , r"no more func watcher IDs" ):
@@ -642,6 +672,11 @@ def context_watcher(self, which_watcher):
642672 finally :
643673 _testcapi .clear_context_watcher (wid )
644674
675+ def unassigned_watcher_id (self , which_watcher = 0 ):
676+ wid = _testcapi .add_context_watcher (which_watcher )
677+ _testcapi .clear_context_watcher (wid )
678+ return wid
679+
645680 def assert_event_counts (self , want_0 , want_1 ):
646681 self .assertEqual (len (_testcapi .get_context_switches (0 )), want_0 )
647682 self .assertEqual (len (_testcapi .get_context_switches (1 )), want_1 )
@@ -702,8 +737,9 @@ def test_clear_out_of_range_watcher_id(self):
702737 _testcapi .clear_context_watcher (8 ) # CONTEXT_MAX_WATCHERS = 8
703738
704739 def test_clear_unassigned_watcher_id (self ):
705- with self .assertRaisesRegex (ValueError , r"No context watcher set for ID 1" ):
706- _testcapi .clear_context_watcher (1 )
740+ wid = self .unassigned_watcher_id ()
741+ with self .assertRaisesRegex (ValueError , rf"No context watcher set for ID { wid } " ):
742+ _testcapi .clear_context_watcher (wid )
707743
708744 def test_allocate_too_many_watchers (self ):
709745 with self .assertRaisesRegex (RuntimeError , r"no more context watcher IDs available" ):
0 commit comments