summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_monitoring.py
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2023-07-28 09:53:33 (GMT)
committerGitHub <noreply@github.com>2023-07-28 09:53:33 (GMT)
commit0902afbae29ef88bf9d212a7e11f9f17b6cbdeb5 (patch)
treeeeada04f808fc41bfeb35fb8ac87dd3297c9a037 /Lib/test/test_monitoring.py
parent3b1a4c18426c78a2fda0d59728bfe9eb92889722 (diff)
downloadcpython-0902afbae29ef88bf9d212a7e11f9f17b6cbdeb5.zip
cpython-0902afbae29ef88bf9d212a7e11f9f17b6cbdeb5.tar.gz
cpython-0902afbae29ef88bf9d212a7e11f9f17b6cbdeb5.tar.bz2
[3.12] GH-106895: Raise a `ValueError` when attempting to disable events that cannot be disabled. (GH-107337) (GH-107351)
Diffstat (limited to 'Lib/test/test_monitoring.py')
-rw-r--r--Lib/test/test_monitoring.py52
1 files changed, 51 insertions, 1 deletions
diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py
index 8bf95a7..9685a43 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: