From 2a17967d44fbf7cb590242739330a688f76b4959 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:16:29 +0300 Subject: [PATCH 1/4] Dont' require `_testcapi` for `test_monitoring.py` --- Lib/test/test_monitoring.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index b8861d09e1564b..1da8146b588742 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -14,7 +14,6 @@ import test.support from test.support import import_helper, requires_specialization, script_helper -_testcapi = import_helper.import_module("_testcapi") _testinternalcapi = import_helper.import_module("_testinternalcapi") PAIR = (0,1) @@ -2579,21 +2578,24 @@ def test_monitoring_live_at_shutdown(self): class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): + _testcapi = import_helper.import_module("_testcapi") class Scope: def __init__(self, *args): self.args = args def __enter__(self): - _testcapi.monitoring_enter_scope(*self.args) + self._testcapi.monitoring_enter_scope(*self.args) def __exit__(self, *args): - _testcapi.monitoring_exit_scope() + self._testcapi.monitoring_exit_scope() + + Scope._testcapi = _testcapi def setUp(self): super(TestCApiEventGeneration, self).setUp() - capi = _testcapi + capi = self._testcapi self.codelike = capi.CodeLike(2) @@ -2662,7 +2664,7 @@ def __call__(self, *args): def test_fire_event(self): for expected, event, function, *args in self.cases: offset = 0 - self.codelike = _testcapi.CodeLike(1) + self.codelike = self._testcapi.CodeLike(1) with self.subTest(function.__name__): args_ = (self.codelike, offset) + tuple(args) self.check_event_count(event, function, args_, expected) @@ -2673,7 +2675,7 @@ def test_missing_exception(self): continue assert args and isinstance(args[-1], BaseException) offset = 0 - self.codelike = _testcapi.CodeLike(1) + self.codelike = self._testcapi.CodeLike(1) with self.subTest(function.__name__): args_ = (self.codelike, offset) + tuple(args[:-1]) + (None,) evt = int(math.log2(event)) @@ -2683,7 +2685,7 @@ def test_missing_exception(self): def test_fire_event_failing_callback(self): for expected, event, function, *args in self.cases: offset = 0 - self.codelike = _testcapi.CodeLike(1) + self.codelike = self._testcapi.CodeLike(1) with self.subTest(function.__name__): args_ = (self.codelike, offset) + tuple(args) exc = OSError(42) @@ -2733,12 +2735,14 @@ def check_disable(self, event, func, args, expected): def test_disable_event(self): for expected, event, function, *args in self.cases: offset = 0 - self.codelike = _testcapi.CodeLike(2) + self.codelike = self._testcapi.CodeLike(2) with self.subTest(function.__name__): args_ = (self.codelike, 0) + tuple(args) self.check_disable(event, function, args_, expected) def test_enter_scope_two_events(self): + _testcapi = self._testcapi + try: yield_counter = CounterWithDisable() unwind_counter = CounterWithDisable() From d043654655c342b77bb4d16b9f5671efad02fc08 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:19:59 +0300 Subject: [PATCH 2/4] same for `_testinternalcapi` --- Lib/test/test_monitoring.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 1da8146b588742..2931e1378f2d55 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -14,8 +14,6 @@ import test.support from test.support import import_helper, requires_specialization, script_helper -_testinternalcapi = import_helper.import_module("_testinternalcapi") - PAIR = (0,1) def f1(): @@ -866,6 +864,8 @@ def test_implicit_stop_iteration(self): This test checks that both paths record an equivalent event. """ + _testinternalcapi = import_helper.import_module("_testinternalcapi") + def gen(): yield 1 return 2 @@ -1033,6 +1033,8 @@ def func(): @requires_specialization def test_no_unwind_for_shim_frame(self): + _testinternalcapi = import_helper.import_module("_testinternalcapi") + class ValueErrorRaiser: def __init__(self): raise ValueError() @@ -2454,6 +2456,7 @@ def run(): sys.monitoring.set_events(TEST_TOOL, 0) def test_108390(self): + _testinternalcapi = import_helper.import_module("_testinternalcapi") class Foo: def __init__(self, set_event): @@ -2545,6 +2548,8 @@ def test_func(x): class TestTier2Optimizer(CheckEvents): def test_monitoring_already_opimized_loop(self): + _testinternalcapi = import_helper.import_module("_testinternalcapi") + def test_func(recorder): set_events = sys.monitoring.set_events line = E.LINE From 377e8246a517051944c33f8e658dc145576a972a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 27 Jun 2026 02:44:03 +0300 Subject: [PATCH 3/4] fix CR --- Lib/test/test_monitoring.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 2931e1378f2d55..484e93dbc7739b 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -2583,10 +2583,9 @@ def test_monitoring_live_at_shutdown(self): class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): - _testcapi = import_helper.import_module("_testcapi") - class Scope: def __init__(self, *args): + self._testcapi = import_helper.import_module("_testcapi") self.args = args def __enter__(self): @@ -2595,12 +2594,10 @@ def __enter__(self): def __exit__(self, *args): self._testcapi.monitoring_exit_scope() - Scope._testcapi = _testcapi - def setUp(self): super(TestCApiEventGeneration, self).setUp() - capi = self._testcapi + capi = import_helper.import_module("_testcapi") self.codelike = capi.CodeLike(2) From a87c719ece1e320470dd7724e02e21f233c6960c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 27 Jun 2026 02:47:58 +0300 Subject: [PATCH 4/4] fix setup --- Lib/test/test_monitoring.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 484e93dbc7739b..5c2d69934b02ea 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -2583,6 +2583,7 @@ def test_monitoring_live_at_shutdown(self): class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): + class Scope: def __init__(self, *args): self._testcapi = import_helper.import_module("_testcapi") @@ -2597,7 +2598,8 @@ def __exit__(self, *args): def setUp(self): super(TestCApiEventGeneration, self).setUp() - capi = import_helper.import_module("_testcapi") + self._testcapi = import_helper.import_module("_testcapi") + capi = self._testcapi self.codelike = capi.CodeLike(2)