- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtest_watchers.py
678 lines (560 loc) · 23.8 KB
/
test_watchers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
importunittest
importcontextvars
fromcontextlibimportcontextmanager, ExitStack
fromtest.supportimport (
catch_unraisable_exception, import_helper,
gc_collect)
# Skip this test if the _testcapi module isn't available.
_testcapi=import_helper.import_module('_testcapi')
classTestDictWatchers(unittest.TestCase):
# types of watchers testcapimodule can add:
EVENTS=0# appends dict events as strings to global event list
ERROR=1# unconditionally sets and signals a RuntimeException
SECOND=2# always appends "second" to global event list
defadd_watcher(self, kind=EVENTS):
return_testcapi.add_dict_watcher(kind)
defclear_watcher(self, watcher_id):
_testcapi.clear_dict_watcher(watcher_id)
@contextmanager
defwatcher(self, kind=EVENTS):
wid=self.add_watcher(kind)
try:
yieldwid
finally:
self.clear_watcher(wid)
defassert_events(self, expected):
actual=_testcapi.get_dict_watcher_events()
self.assertEqual(actual, expected)
defwatch(self, wid, d):
_testcapi.watch_dict(wid, d)
defunwatch(self, wid, d):
_testcapi.unwatch_dict(wid, d)
deftest_set_new_item(self):
d= {}
withself.watcher() aswid:
self.watch(wid, d)
d["foo"] ="bar"
self.assert_events(["new:foo:bar"])
deftest_set_existing_item(self):
d= {"foo": "bar"}
withself.watcher() aswid:
self.watch(wid, d)
d["foo"] ="baz"
self.assert_events(["mod:foo:baz"])
deftest_clone(self):
d= {}
d2= {"foo": "bar"}
withself.watcher() aswid:
self.watch(wid, d)
d.update(d2)
self.assert_events(["clone"])
deftest_no_event_if_not_watched(self):
d= {}
withself.watcher() aswid:
d["foo"] ="bar"
self.assert_events([])
deftest_del(self):
d= {"foo": "bar"}
withself.watcher() aswid:
self.watch(wid, d)
deld["foo"]
self.assert_events(["del:foo"])
deftest_pop(self):
d= {"foo": "bar"}
withself.watcher() aswid:
self.watch(wid, d)
d.pop("foo")
self.assert_events(["del:foo"])
deftest_clear(self):
d= {"foo": "bar"}
withself.watcher() aswid:
self.watch(wid, d)
d.clear()
self.assert_events(["clear"])
deftest_dealloc(self):
d= {"foo": "bar"}
withself.watcher() aswid:
self.watch(wid, d)
deld
self.assert_events(["dealloc"])
deftest_object_dict(self):
classMyObj: pass
o=MyObj()
withself.watcher() aswid:
self.watch(wid, o.__dict__)
o.foo="bar"
o.foo="baz"
delo.foo
self.assert_events(["new:foo:bar", "mod:foo:baz", "del:foo"])
withself.watcher() aswid:
self.watch(wid, o.__dict__)
for_inrange(100):
o.foo="bar"
self.assert_events(["new:foo:bar"] + ["mod:foo:bar"] *99)
deftest_unwatch(self):
d= {}
withself.watcher() aswid:
self.watch(wid, d)
d["foo"] ="bar"
self.unwatch(wid, d)
d["hmm"] ="baz"
self.assert_events(["new:foo:bar"])
deftest_error(self):
d= {}
withself.watcher(kind=self.ERROR) aswid:
self.watch(wid, d)
withcatch_unraisable_exception() ascm:
d["foo"] ="bar"
self.assertIn(
"Exception ignored in "
"PyDict_EVENT_ADDED watcher callback for <dict at ",
cm.unraisable.err_msg
)
self.assertIsNone(cm.unraisable.object)
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
self.assert_events([])
deftest_dealloc_error(self):
d= {}
withself.watcher(kind=self.ERROR) aswid:
self.watch(wid, d)
withcatch_unraisable_exception() ascm:
deld
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
deftest_two_watchers(self):
d1= {}
d2= {}
withself.watcher() aswid1:
withself.watcher(kind=self.SECOND) aswid2:
self.watch(wid1, d1)
self.watch(wid2, d2)
d1["foo"] ="bar"
d2["hmm"] ="baz"
self.assert_events(["new:foo:bar", "second"])
deftest_watch_non_dict(self):
withself.watcher() aswid:
withself.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"):
self.watch(wid, 1)
deftest_watch_out_of_range_watcher_id(self):
d= {}
withself.assertRaisesRegex(ValueError, r"Invalid dict watcher ID -1"):
self.watch(-1, d)
withself.assertRaisesRegex(ValueError, r"Invalid dict watcher ID 8"):
self.watch(8, d) # DICT_MAX_WATCHERS = 8
deftest_watch_unassigned_watcher_id(self):
d= {}
withself.assertRaisesRegex(ValueError, r"No dict watcher set for ID 3"):
self.watch(3, d)
deftest_unwatch_non_dict(self):
withself.watcher() aswid:
withself.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"):
self.unwatch(wid, 1)
deftest_unwatch_out_of_range_watcher_id(self):
d= {}
withself.assertRaisesRegex(ValueError, r"Invalid dict watcher ID -1"):
self.unwatch(-1, d)
withself.assertRaisesRegex(ValueError, r"Invalid dict watcher ID 8"):
self.unwatch(8, d) # DICT_MAX_WATCHERS = 8
deftest_unwatch_unassigned_watcher_id(self):
d= {}
withself.assertRaisesRegex(ValueError, r"No dict watcher set for ID 3"):
self.unwatch(3, d)
deftest_clear_out_of_range_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"Invalid dict watcher ID -1"):
self.clear_watcher(-1)
withself.assertRaisesRegex(ValueError, r"Invalid dict watcher ID 8"):
self.clear_watcher(8) # DICT_MAX_WATCHERS = 8
deftest_clear_unassigned_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"No dict watcher set for ID 3"):
self.clear_watcher(3)
classTestTypeWatchers(unittest.TestCase):
# types of watchers testcapimodule can add:
TYPES=0# appends modified types to global event list
ERROR=1# unconditionally sets and signals a RuntimeException
WRAP=2# appends modified type wrapped in list to global event list
# duplicating the C constant
TYPE_MAX_WATCHERS=8
defadd_watcher(self, kind=TYPES):
return_testcapi.add_type_watcher(kind)
defclear_watcher(self, watcher_id):
_testcapi.clear_type_watcher(watcher_id)
@contextmanager
defwatcher(self, kind=TYPES):
wid=self.add_watcher(kind)
try:
yieldwid
finally:
self.clear_watcher(wid)
defassert_events(self, expected):
actual=_testcapi.get_type_modified_events()
self.assertEqual(actual, expected)
defwatch(self, wid, t):
_testcapi.watch_type(wid, t)
defunwatch(self, wid, t):
_testcapi.unwatch_type(wid, t)
deftest_watch_type(self):
classC: pass
withself.watcher() aswid:
self.watch(wid, C)
C.foo="bar"
self.assert_events([C])
deftest_event_aggregation(self):
classC: pass
withself.watcher() aswid:
self.watch(wid, C)
C.foo="bar"
C.bar="baz"
# only one event registered for both modifications
self.assert_events([C])
deftest_lookup_resets_aggregation(self):
classC: pass
withself.watcher() aswid:
self.watch(wid, C)
C.foo="bar"
# lookup resets type version tag
self.assertEqual(C.foo, "bar")
C.bar="baz"
# both events registered
self.assert_events([C, C])
deftest_unwatch_type(self):
classC: pass
withself.watcher() aswid:
self.watch(wid, C)
C.foo="bar"
self.assertEqual(C.foo, "bar")
self.assert_events([C])
self.unwatch(wid, C)
C.bar="baz"
self.assert_events([C])
deftest_clear_watcher(self):
classC: pass
# outer watcher is unused, it's just to keep events list alive
withself.watcher() as_:
withself.watcher() aswid:
self.watch(wid, C)
C.foo="bar"
self.assertEqual(C.foo, "bar")
self.assert_events([C])
C.bar="baz"
# Watcher on C has been cleared, no new event
self.assert_events([C])
deftest_watch_type_subclass(self):
classC: pass
classD(C): pass
withself.watcher() aswid:
self.watch(wid, D)
C.foo="bar"
self.assert_events([D])
deftest_error(self):
classC: pass
withself.watcher(kind=self.ERROR) aswid:
self.watch(wid, C)
withcatch_unraisable_exception() ascm:
C.foo="bar"
self.assertEqual(
cm.unraisable.err_msg,
f"Exception ignored in type watcher callback #1 for {C!r}",
)
self.assertIs(cm.unraisable.object, None)
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
self.assert_events([])
deftest_two_watchers(self):
classC1: pass
classC2: pass
withself.watcher() aswid1:
withself.watcher(kind=self.WRAP) aswid2:
self.assertNotEqual(wid1, wid2)
self.watch(wid1, C1)
self.watch(wid2, C2)
C1.foo="bar"
C2.hmm="baz"
self.assert_events([C1, [C2]])
deftest_all_watchers(self):
classC: pass
withExitStack() asstack:
last_wid=-1
# don't make assumptions about how many watchers are already
# registered, just go until we reach the max ID
whilelast_wid<self.TYPE_MAX_WATCHERS-1:
last_wid=stack.enter_context(self.watcher())
self.watch(last_wid, C)
C.foo="bar"
self.assert_events([C])
deftest_watch_non_type(self):
withself.watcher() aswid:
withself.assertRaisesRegex(ValueError, r"Cannot watch non-type"):
self.watch(wid, 1)
deftest_watch_out_of_range_watcher_id(self):
classC: pass
withself.assertRaisesRegex(ValueError, r"Invalid type watcher ID -1"):
self.watch(-1, C)
withself.assertRaisesRegex(ValueError, r"Invalid type watcher ID 8"):
self.watch(self.TYPE_MAX_WATCHERS, C)
deftest_watch_unassigned_watcher_id(self):
classC: pass
withself.assertRaisesRegex(ValueError, r"No type watcher set for ID 1"):
self.watch(1, C)
deftest_unwatch_non_type(self):
withself.watcher() aswid:
withself.assertRaisesRegex(ValueError, r"Cannot watch non-type"):
self.unwatch(wid, 1)
deftest_unwatch_out_of_range_watcher_id(self):
classC: pass
withself.assertRaisesRegex(ValueError, r"Invalid type watcher ID -1"):
self.unwatch(-1, C)
withself.assertRaisesRegex(ValueError, r"Invalid type watcher ID 8"):
self.unwatch(self.TYPE_MAX_WATCHERS, C)
deftest_unwatch_unassigned_watcher_id(self):
classC: pass
withself.assertRaisesRegex(ValueError, r"No type watcher set for ID 1"):
self.unwatch(1, C)
deftest_clear_out_of_range_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"Invalid type watcher ID -1"):
self.clear_watcher(-1)
withself.assertRaisesRegex(ValueError, r"Invalid type watcher ID 8"):
self.clear_watcher(self.TYPE_MAX_WATCHERS)
deftest_clear_unassigned_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"No type watcher set for ID 1"):
self.clear_watcher(1)
deftest_no_more_ids_available(self):
withself.assertRaisesRegex(RuntimeError, r"no more type watcher IDs"):
withExitStack() asstack:
for_inrange(self.TYPE_MAX_WATCHERS+1):
stack.enter_context(self.watcher())
classTestCodeObjectWatchers(unittest.TestCase):
@contextmanager
defcode_watcher(self, which_watcher):
wid=_testcapi.add_code_watcher(which_watcher)
try:
yieldwid
finally:
_testcapi.clear_code_watcher(wid)
defassert_event_counts(self, exp_created_0, exp_destroyed_0,
exp_created_1, exp_destroyed_1):
gc_collect() # code objects are collected by GC in free-threaded build
self.assertEqual(
exp_created_0, _testcapi.get_code_watcher_num_created_events(0))
self.assertEqual(
exp_destroyed_0, _testcapi.get_code_watcher_num_destroyed_events(0))
self.assertEqual(
exp_created_1, _testcapi.get_code_watcher_num_created_events(1))
self.assertEqual(
exp_destroyed_1, _testcapi.get_code_watcher_num_destroyed_events(1))
deftest_code_object_events_dispatched(self):
# verify that all counts are zero before any watchers are registered
self.assert_event_counts(0, 0, 0, 0)
# verify that all counts remain zero when a code object is
# created and destroyed with no watchers registered
co1=_testcapi.code_newempty("test_watchers", "dummy1", 0)
self.assert_event_counts(0, 0, 0, 0)
delco1
self.assert_event_counts(0, 0, 0, 0)
# verify counts are as expected when first watcher is registered
withself.code_watcher(0):
self.assert_event_counts(0, 0, 0, 0)
co2=_testcapi.code_newempty("test_watchers", "dummy2", 0)
self.assert_event_counts(1, 0, 0, 0)
delco2
self.assert_event_counts(1, 1, 0, 0)
# again with second watcher registered
withself.code_watcher(1):
self.assert_event_counts(1, 1, 0, 0)
co3=_testcapi.code_newempty("test_watchers", "dummy3", 0)
self.assert_event_counts(2, 1, 1, 0)
delco3
self.assert_event_counts(2, 2, 1, 1)
# verify counts are reset and don't change after both watchers are cleared
co4=_testcapi.code_newempty("test_watchers", "dummy4", 0)
self.assert_event_counts(0, 0, 0, 0)
delco4
self.assert_event_counts(0, 0, 0, 0)
deftest_error(self):
withself.code_watcher(2):
withcatch_unraisable_exception() ascm:
co=_testcapi.code_newempty("test_watchers", "dummy0", 0)
self.assertEqual(
cm.unraisable.err_msg,
f"Exception ignored in "
f"PY_CODE_EVENT_CREATE watcher callback for {co!r}"
)
self.assertIsNone(cm.unraisable.object)
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
deftest_dealloc_error(self):
co=_testcapi.code_newempty("test_watchers", "dummy0", 0)
withself.code_watcher(2):
withcatch_unraisable_exception() ascm:
delco
gc_collect()
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
deftest_clear_out_of_range_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"Invalid code watcher ID -1"):
_testcapi.clear_code_watcher(-1)
withself.assertRaisesRegex(ValueError, r"Invalid code watcher ID 8"):
_testcapi.clear_code_watcher(8) # CODE_MAX_WATCHERS = 8
deftest_clear_unassigned_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"No code watcher set for ID 1"):
_testcapi.clear_code_watcher(1)
deftest_allocate_too_many_watchers(self):
withself.assertRaisesRegex(RuntimeError, r"no more code watcher IDs available"):
_testcapi.allocate_too_many_code_watchers()
classTestFuncWatchers(unittest.TestCase):
@contextmanager
defadd_watcher(self, func):
wid=_testcapi.add_func_watcher(func)
try:
yield
finally:
_testcapi.clear_func_watcher(wid)
deftest_func_events_dispatched(self):
events= []
defwatcher(*args):
events.append(args)
withself.add_watcher(watcher):
defmyfunc():
pass
self.assertIn((_testcapi.PYFUNC_EVENT_CREATE, myfunc, None), events)
myfunc_id=id(myfunc)
new_code=self.test_func_events_dispatched.__code__
myfunc.__code__=new_code
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_CODE, myfunc, new_code), events)
new_defaults= (123,)
myfunc.__defaults__=new_defaults
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_DEFAULTS, myfunc, new_defaults), events)
new_defaults= (456,)
_testcapi.set_func_defaults_via_capi(myfunc, new_defaults)
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_DEFAULTS, myfunc, new_defaults), events)
new_kwdefaults= {"self": 123}
myfunc.__kwdefaults__=new_kwdefaults
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_KWDEFAULTS, myfunc, new_kwdefaults), events)
new_kwdefaults= {"self": 456}
_testcapi.set_func_kwdefaults_via_capi(myfunc, new_kwdefaults)
self.assertIn((_testcapi.PYFUNC_EVENT_MODIFY_KWDEFAULTS, myfunc, new_kwdefaults), events)
# Clear events reference to func
events= []
delmyfunc
self.assertIn((_testcapi.PYFUNC_EVENT_DESTROY, myfunc_id, None), events)
deftest_multiple_watchers(self):
events0= []
deffirst_watcher(*args):
events0.append(args)
events1= []
defsecond_watcher(*args):
events1.append(args)
withself.add_watcher(first_watcher):
withself.add_watcher(second_watcher):
defmyfunc():
pass
event= (_testcapi.PYFUNC_EVENT_CREATE, myfunc, None)
self.assertIn(event, events0)
self.assertIn(event, events1)
deftest_watcher_raises_error(self):
classMyError(Exception):
pass
defwatcher(*args):
raiseMyError("testing 123")
withself.add_watcher(watcher):
withcatch_unraisable_exception() ascm:
defmyfunc():
pass
self.assertEqual(
cm.unraisable.err_msg,
f"Exception ignored in "
f"PyFunction_EVENT_CREATE watcher callback for {repr(myfunc)[1:-1]}"
)
self.assertIsNone(cm.unraisable.object)
deftest_dealloc_watcher_raises_error(self):
classMyError(Exception):
pass
defwatcher(*args):
raiseMyError("testing 123")
defmyfunc():
pass
withself.add_watcher(watcher):
withcatch_unraisable_exception() ascm:
delmyfunc
self.assertIsInstance(cm.unraisable.exc_value, MyError)
deftest_clear_out_of_range_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"invalid func watcher ID -1"):
_testcapi.clear_func_watcher(-1)
withself.assertRaisesRegex(ValueError, r"invalid func watcher ID 8"):
_testcapi.clear_func_watcher(8) # FUNC_MAX_WATCHERS = 8
deftest_clear_unassigned_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"no func watcher set for ID 1"):
_testcapi.clear_func_watcher(1)
deftest_allocate_too_many_watchers(self):
withself.assertRaisesRegex(RuntimeError, r"no more func watcher IDs"):
_testcapi.allocate_too_many_func_watchers()
classTestContextObjectWatchers(unittest.TestCase):
@contextmanager
defcontext_watcher(self, which_watcher):
wid=_testcapi.add_context_watcher(which_watcher)
try:
switches=_testcapi.get_context_switches(which_watcher)
exceptValueError:
switches=None
try:
yieldswitches
finally:
_testcapi.clear_context_watcher(wid)
defassert_event_counts(self, want_0, want_1):
self.assertEqual(len(_testcapi.get_context_switches(0)), want_0)
self.assertEqual(len(_testcapi.get_context_switches(1)), want_1)
deftest_context_object_events_dispatched(self):
# verify that all counts are zero before any watchers are registered
self.assert_event_counts(0, 0)
# verify that all counts remain zero when a context object is
# entered and exited with no watchers registered
ctx=contextvars.copy_context()
ctx.run(self.assert_event_counts, 0, 0)
self.assert_event_counts(0, 0)
# verify counts are as expected when first watcher is registered
withself.context_watcher(0):
self.assert_event_counts(0, 0)
ctx.run(self.assert_event_counts, 1, 0)
self.assert_event_counts(2, 0)
# again with second watcher registered
withself.context_watcher(1):
self.assert_event_counts(2, 0)
ctx.run(self.assert_event_counts, 3, 1)
self.assert_event_counts(4, 2)
# verify counts are reset and don't change after both watchers are cleared
ctx.run(self.assert_event_counts, 0, 0)
self.assert_event_counts(0, 0)
deftest_callback_error(self):
ctx_outer=contextvars.copy_context()
ctx_inner=contextvars.copy_context()
unraisables= []
def_in_outer():
withself.context_watcher(2):
withcatch_unraisable_exception() ascm:
ctx_inner.run(lambda: unraisables.append(cm.unraisable))
unraisables.append(cm.unraisable)
try:
ctx_outer.run(_in_outer)
self.assertEqual([x.err_msgforxinunraisables],
["Exception ignored in Py_CONTEXT_SWITCHED "
f"watcher callback for {ctx!r}"
forctxin [ctx_inner, ctx_outer]])
self.assertEqual([str(x.exc_value) forxinunraisables],
["boom!", "boom!"])
finally:
# Break reference cycle
unraisables=None
deftest_clear_out_of_range_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"Invalid context watcher ID -1"):
_testcapi.clear_context_watcher(-1)
withself.assertRaisesRegex(ValueError, r"Invalid context watcher ID 8"):
_testcapi.clear_context_watcher(8) # CONTEXT_MAX_WATCHERS = 8
deftest_clear_unassigned_watcher_id(self):
withself.assertRaisesRegex(ValueError, r"No context watcher set for ID 1"):
_testcapi.clear_context_watcher(1)
deftest_allocate_too_many_watchers(self):
withself.assertRaisesRegex(RuntimeError, r"no more context watcher IDs available"):
_testcapi.allocate_too_many_context_watchers()
deftest_exit_base_context(self):
ctx=contextvars.Context()
_testcapi.clear_context_stack()
withself.context_watcher(0) asswitches:
ctx.run(lambda: None)
self.assertEqual(switches, [ctx, None])
if__name__=="__main__":
unittest.main()