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