diff options
author | Mark Shannon <mark@hotpy.org> | 2023-07-27 14:27:11 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-27 14:27:11 (GMT) |
commit | c6539b36c163efff3d6ed02b938a6151325f4db7 (patch) | |
tree | 628e61ae6907427baf9cf76e9d1a3d78525e6366 /Lib | |
parent | d77d973335835bd744be8106010061cb338b0ae1 (diff) | |
download | cpython-c6539b36c163efff3d6ed02b938a6151325f4db7.zip cpython-c6539b36c163efff3d6ed02b938a6151325f4db7.tar.gz cpython-c6539b36c163efff3d6ed02b938a6151325f4db7.tar.bz2 |
GH-106895: Raise a `ValueError` when attempting to disable events that cannot be disabled. (GH-107337)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_monitoring.py | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 7538786..7098f48 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -136,20 +136,27 @@ class MonitoringCountTest(MonitoringTestBase, unittest.TestCase): E = sys.monitoring.events -SIMPLE_EVENTS = [ +INSTRUMENTED_EVENTS = [ (E.PY_START, "start"), (E.PY_RESUME, "resume"), (E.PY_RETURN, "return"), (E.PY_YIELD, "yield"), (E.JUMP, "jump"), (E.BRANCH, "branch"), +] + +EXCEPT_EVENTS = [ (E.RAISE, "raise"), (E.PY_UNWIND, "unwind"), (E.EXCEPTION_HANDLED, "exception_handled"), +] + +SIMPLE_EVENTS = INSTRUMENTED_EVENTS + EXCEPT_EVENTS + [ (E.C_RAISE, "c_raise"), (E.C_RETURN, "c_return"), ] + SIMPLE_EVENT_SET = functools.reduce(operator.or_, [ev for (ev, _) in SIMPLE_EVENTS], 0) | E.CALL @@ -618,6 +625,49 @@ class LineMonitoringTest(MonitoringTestBase, unittest.TestCase): self.check_lines(func2, [1,2,3,4,5,6]) +class TestDisable(MonitoringTestBase, unittest.TestCase): + + def gen(self, cond): + for i in range(10): + if cond: + yield 1 + else: + yield 2 + + def raise_handle_reraise(self): + try: + 1/0 + except: + raise + + def test_disable_legal_events(self): + for event, name in INSTRUMENTED_EVENTS: + try: + counter = CounterWithDisable() + counter.disable = True + sys.monitoring.register_callback(TEST_TOOL, event, counter) + sys.monitoring.set_events(TEST_TOOL, event) + for _ in self.gen(1): + pass + self.assertLess(counter.count, 4) + finally: + sys.monitoring.set_events(TEST_TOOL, 0) + sys.monitoring.register_callback(TEST_TOOL, event, None) + + + def test_disable_illegal_events(self): + for event, name in EXCEPT_EVENTS: + try: + counter = CounterWithDisable() + counter.disable = True + sys.monitoring.register_callback(TEST_TOOL, event, counter) + sys.monitoring.set_events(TEST_TOOL, event) + with self.assertRaises(ValueError): + self.raise_handle_reraise() + finally: + sys.monitoring.set_events(TEST_TOOL, 0) + sys.monitoring.register_callback(TEST_TOOL, event, None) + class ExceptionRecorder: |