Skip to content

Commit 9f950a1

Browse files
committed
Fix test.test_capi.test_watchers assuming which watcher IDs are available
Some watcher tests hardcode watcher IDs 1 and 3 and assume them to be available. Instead, force an available watcher ID by creating and then immediately removing a watcher. This makes the tests a bit more resilient in the face of other modules registering watchers. This is relevant to CinderX, which installs watchers on import. The CinderX project runs the CPython test suite with the CinderX module loaded, which breaks the assumptions in these tests.
1 parent 22691a3 commit 9f950a1

1 file changed

Lines changed: 55 additions & 19 deletions

File tree

‎Lib/test/test_capi/test_watchers.py‎

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -171,8 +176,9 @@ def test_watch_out_of_range_watcher_id(self):
171176

172177
def test_watch_unassigned_watcher_id(self):
173178
d = {}
174-
with self.assertRaisesRegex(ValueError, r"No dict watcher set for ID 3"):
175-
self.watch(3, d)
179+
wid = self.unassigned_watcher_id()
180+
with self.assertRaisesRegex(ValueError, rf"No dict watcher set for ID {wid}"):
181+
self.watch(wid, d)
176182

177183
def test_unwatch_non_dict(self):
178184
with self.watcher() as wid:
@@ -189,8 +195,9 @@ def test_unwatch_out_of_range_watcher_id(self):
189195

190196
def test_unwatch_unassigned_watcher_id(self):
191197
d = {}
192-
with self.assertRaisesRegex(ValueError, r"No dict watcher set for ID 3"):
193-
self.unwatch(3, d)
198+
wid = self.unassigned_watcher_id()
199+
with self.assertRaisesRegex(ValueError, rf"No dict watcher set for ID {wid}"):
200+
self.unwatch(wid, d)
194201

195202
def test_clear_out_of_range_watcher_id(self):
196203
with self.assertRaisesRegex(ValueError, r"Invalid dict watcher ID -1"):
@@ -199,8 +206,9 @@ def test_clear_out_of_range_watcher_id(self):
199206
self.clear_watcher(8) # DICT_MAX_WATCHERS = 8
200207

201208
def test_clear_unassigned_watcher_id(self):
202-
with self.assertRaisesRegex(ValueError, r"No dict watcher set for ID 3"):
203-
self.clear_watcher(3)
209+
wid = self.unassigned_watcher_id()
210+
with self.assertRaisesRegex(ValueError, rf"No dict watcher set for ID {wid}"):
211+
self.clear_watcher(wid)
204212

205213

206214
class TestTypeWatchers(unittest.TestCase):
@@ -227,6 +235,11 @@ def watcher(self, kind=TYPES):
227235
finally:
228236
self.clear_watcher(wid)
229237

238+
def unassigned_watcher_id(self):
239+
wid = self.add_watcher()
240+
self.clear_watcher(wid)
241+
return wid
242+
230243
def assert_events(self, expected):
231244
actual = _testcapi.get_type_modified_events()
232245
self.assertEqual(actual, expected)
@@ -304,7 +317,7 @@ class C: pass
304317
C.foo = "bar"
305318
self.assertEqual(
306319
cm.unraisable.err_msg,
307-
f"Exception ignored in type watcher callback #1 for {C!r}",
320+
f"Exception ignored in type watcher callback #{wid} for {C!r}",
308321
)
309322
self.assertIs(cm.unraisable.object, None)
310323
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
@@ -348,8 +361,9 @@ class C: pass
348361

349362
def test_watch_unassigned_watcher_id(self):
350363
class C: pass
351-
with self.assertRaisesRegex(ValueError, r"No type watcher set for ID 1"):
352-
self.watch(1, C)
364+
wid = self.unassigned_watcher_id()
365+
with self.assertRaisesRegex(ValueError, rf"No type watcher set for ID {wid}"):
366+
self.watch(wid, C)
353367

354368
def test_unwatch_non_type(self):
355369
with self.watcher() as wid:
@@ -365,8 +379,9 @@ class C: pass
365379

366380
def test_unwatch_unassigned_watcher_id(self):
367381
class C: pass
368-
with self.assertRaisesRegex(ValueError, r"No type watcher set for ID 1"):
369-
self.unwatch(1, C)
382+
wid = self.unassigned_watcher_id()
383+
with self.assertRaisesRegex(ValueError, rf"No type watcher set for ID {wid}"):
384+
self.unwatch(wid, C)
370385

371386
def test_clear_out_of_range_watcher_id(self):
372387
with self.assertRaisesRegex(ValueError, r"Invalid type watcher ID -1"):
@@ -375,8 +390,9 @@ def test_clear_out_of_range_watcher_id(self):
375390
self.clear_watcher(self.TYPE_MAX_WATCHERS)
376391

377392
def test_clear_unassigned_watcher_id(self):
378-
with self.assertRaisesRegex(ValueError, r"No type watcher set for ID 1"):
379-
self.clear_watcher(1)
393+
wid = self.unassigned_watcher_id()
394+
with self.assertRaisesRegex(ValueError, rf"No type watcher set for ID {wid}"):
395+
self.clear_watcher(wid)
380396

381397
def test_watch_type_dealloc(self):
382398
# Use the NAME watcher (kind=3) which records the type's name as a
@@ -415,6 +431,11 @@ def code_watcher(self, which_watcher):
415431
finally:
416432
_testcapi.clear_code_watcher(wid)
417433

434+
def unassigned_watcher_id(self, which_watcher=0):
435+
wid = _testcapi.add_code_watcher(which_watcher)
436+
_testcapi.clear_code_watcher(wid)
437+
return wid
438+
418439
def assert_event_counts(self, exp_created_0, exp_destroyed_0,
419440
exp_created_1, exp_destroyed_1):
420441
gc_collect() # code objects are collected by GC in free-threaded build
@@ -489,8 +510,9 @@ def test_clear_out_of_range_watcher_id(self):
489510
_testcapi.clear_code_watcher(8) # CODE_MAX_WATCHERS = 8
490511

491512
def test_clear_unassigned_watcher_id(self):
492-
with self.assertRaisesRegex(ValueError, r"No code watcher set for ID 1"):
493-
_testcapi.clear_code_watcher(1)
513+
wid = self.unassigned_watcher_id()
514+
with self.assertRaisesRegex(ValueError, rf"No code watcher set for ID {wid}"):
515+
_testcapi.clear_code_watcher(wid)
494516

495517
def test_allocate_too_many_watchers(self):
496518
with self.assertRaisesRegex(RuntimeError, r"no more code watcher IDs available"):
@@ -506,6 +528,13 @@ def add_watcher(self, func):
506528
finally:
507529
_testcapi.clear_func_watcher(wid)
508530

531+
def unassigned_watcher_id(self, which_watcher=0):
532+
def func(*args):
533+
pass
534+
wid = _testcapi.add_func_watcher(func)
535+
_testcapi.clear_func_watcher(wid)
536+
return wid
537+
509538
def test_func_events_dispatched(self):
510539
events = []
511540
def watcher(*args):
@@ -606,8 +635,9 @@ def test_clear_out_of_range_watcher_id(self):
606635
_testcapi.clear_func_watcher(8) # FUNC_MAX_WATCHERS = 8
607636

608637
def test_clear_unassigned_watcher_id(self):
609-
with self.assertRaisesRegex(ValueError, r"no func watcher set for ID 1"):
610-
_testcapi.clear_func_watcher(1)
638+
wid = self.unassigned_watcher_id()
639+
with self.assertRaisesRegex(ValueError, rf"no func watcher set for ID {wid}"):
640+
_testcapi.clear_func_watcher(wid)
611641

612642
def test_allocate_too_many_watchers(self):
613643
with self.assertRaisesRegex(RuntimeError, r"no more func watcher IDs"):
@@ -627,6 +657,11 @@ def context_watcher(self, which_watcher):
627657
finally:
628658
_testcapi.clear_context_watcher(wid)
629659

660+
def unassigned_watcher_id(self, which_watcher=0):
661+
wid = _testcapi.add_context_watcher(which_watcher)
662+
_testcapi.clear_context_watcher(wid)
663+
return wid
664+
630665
def assert_event_counts(self, want_0, want_1):
631666
self.assertEqual(len(_testcapi.get_context_switches(0)), want_0)
632667
self.assertEqual(len(_testcapi.get_context_switches(1)), want_1)
@@ -687,8 +722,9 @@ def test_clear_out_of_range_watcher_id(self):
687722
_testcapi.clear_context_watcher(8) # CONTEXT_MAX_WATCHERS = 8
688723

689724
def test_clear_unassigned_watcher_id(self):
690-
with self.assertRaisesRegex(ValueError, r"No context watcher set for ID 1"):
691-
_testcapi.clear_context_watcher(1)
725+
wid = self.unassigned_watcher_id()
726+
with self.assertRaisesRegex(ValueError, rf"No context watcher set for ID {wid}"):
727+
_testcapi.clear_context_watcher(wid)
692728

693729
def test_allocate_too_many_watchers(self):
694730
with self.assertRaisesRegex(RuntimeError, r"no more context watcher IDs available"):

0 commit comments

Comments
 (0)