|
| 1 | +import unittest |
| 2 | + |
| 3 | +from contextlib import contextmanager |
| 4 | +from test.support import catch_unraisable_exception, import_helper |
| 5 | + |
| 6 | +_testcapi = import_helper.import_module("_testcapi") |
| 7 | + |
| 8 | +from _testcapi import ( |
| 9 | + PYFUNC_EVENT_CREATED, |
| 10 | + PYFUNC_EVENT_DESTROY, |
| 11 | + PYFUNC_EVENT_MODIFY_CODE, |
| 12 | + PYFUNC_EVENT_MODIFY_DEFAULTS, |
| 13 | + PYFUNC_EVENT_MODIFY_KWDEFAULTS, |
| 14 | + _add_func_watcher, |
| 15 | + _clear_func_watcher, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class FuncEventsTest(unittest.TestCase): |
| 20 | + @contextmanager |
| 21 | + def add_watcher(self, func): |
| 22 | + wid = _add_func_watcher(func) |
| 23 | + try: |
| 24 | + yield |
| 25 | + finally: |
| 26 | + _clear_func_watcher(wid) |
| 27 | + |
| 28 | + def test_func_events_dispatched(self): |
| 29 | + events = [] |
| 30 | + def watcher(*args): |
| 31 | + events.append(args) |
| 32 | + |
| 33 | + with self.add_watcher(watcher): |
| 34 | + def myfunc(): |
| 35 | + pass |
| 36 | + self.assertIn((PYFUNC_EVENT_CREATED, myfunc, None), events) |
| 37 | + myfunc_id = id(myfunc) |
| 38 | + |
| 39 | + new_code = self.test_func_events_dispatched.__code__ |
| 40 | + myfunc.__code__ = new_code |
| 41 | + self.assertIn((PYFUNC_EVENT_MODIFY_CODE, myfunc, new_code), events) |
| 42 | + |
| 43 | + new_defaults = (123,) |
| 44 | + myfunc.__defaults__ = new_defaults |
| 45 | + self.assertIn((PYFUNC_EVENT_MODIFY_DEFAULTS, myfunc, new_defaults), events) |
| 46 | + |
| 47 | + new_kwdefaults = {"self": 123} |
| 48 | + myfunc.__kwdefaults__ = new_kwdefaults |
| 49 | + self.assertIn((PYFUNC_EVENT_MODIFY_KWDEFAULTS, myfunc, new_kwdefaults), events) |
| 50 | + |
| 51 | + # Clear events reference to func |
| 52 | + events = [] |
| 53 | + del myfunc |
| 54 | + self.assertIn((PYFUNC_EVENT_DESTROY, myfunc_id, None), events) |
| 55 | + |
| 56 | + def test_multiple_watchers(self): |
| 57 | + events0 = [] |
| 58 | + def first_watcher(*args): |
| 59 | + events0.append(args) |
| 60 | + |
| 61 | + events1 = [] |
| 62 | + def second_watcher(*args): |
| 63 | + events1.append(args) |
| 64 | + |
| 65 | + with self.add_watcher(first_watcher): |
| 66 | + with self.add_watcher(second_watcher): |
| 67 | + def myfunc(): |
| 68 | + pass |
| 69 | + |
| 70 | + event = (PYFUNC_EVENT_CREATED, myfunc, None) |
| 71 | + self.assertIn(event, events0) |
| 72 | + self.assertIn(event, events1) |
| 73 | + |
| 74 | + def test_watcher_raises_error(self): |
| 75 | + class MyError(Exception): |
| 76 | + pass |
| 77 | + |
| 78 | + def watcher(*args): |
| 79 | + raise MyError("testing 123") |
| 80 | + |
| 81 | + with self.add_watcher(watcher): |
| 82 | + with catch_unraisable_exception() as cm: |
| 83 | + def myfunc(): |
| 84 | + pass |
| 85 | + |
| 86 | + self.assertIs(cm.unraisable.object, myfunc) |
| 87 | + self.assertIsInstance(cm.unraisable.exc_value, MyError) |
| 88 | + |
| 89 | + def test_clear_out_of_range_watcher_id(self): |
| 90 | + with self.assertRaisesRegex(ValueError, r"Invalid func watcher ID -1"): |
| 91 | + _clear_func_watcher(-1) |
| 92 | + with self.assertRaisesRegex(ValueError, r"Invalid func watcher ID 8"): |
| 93 | + _clear_func_watcher(8) # FUNC_MAX_WATCHERS = 8 |
| 94 | + |
| 95 | + def test_clear_unassigned_watcher_id(self): |
| 96 | + with self.assertRaisesRegex(ValueError, r"No func watcher set for ID 1"): |
| 97 | + _clear_func_watcher(1) |
0 commit comments