From eed97778b9cd738ee1e94dcc0e992fe0a5686f1b Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 30 Jun 2026 12:29:58 +0200 Subject: [PATCH] [3.14] Don't require the `_test{internal}capi` modules in `test_monitoring.py` (GH-152311) (#152661) (cherry picked from commit 189ab8388762c8102b2d72ad3a1a921f7f2aef10) Co-authored-by: Shahar Naveh <50263213+ShaharNaveh@users.noreply.github.com> Co-authored-by: Stan Ulbrych --- Lib/test/test_monitoring.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index de658c597a19..ffa1fd957eb4 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -14,9 +14,6 @@ import unittest import test.support from test.support import import_helper, requires_specialization_ft, script_helper -_testcapi = import_helper.import_module("_testcapi") -_testinternalcapi = import_helper.import_module("_testinternalcapi") - PAIR = (0,1) def f1(): @@ -882,6 +879,8 @@ class ExceptionMonitoringTest(CheckEvents): This test checks that both paths record an equivalent event. """ + _testinternalcapi = import_helper.import_module("_testinternalcapi") + def gen(): yield 1 return 2 @@ -1049,6 +1048,8 @@ class ExceptionMonitoringTest(CheckEvents): @requires_specialization_ft def test_no_unwind_for_shim_frame(self): + _testinternalcapi = import_helper.import_module("_testinternalcapi") + class ValueErrorRaiser: def __init__(self): raise ValueError() @@ -2141,6 +2142,7 @@ class TestRegressions(MonitoringTestBase, unittest.TestCase): sys.monitoring.set_events(TEST_TOOL, 0) def test_108390(self): + _testinternalcapi = import_helper.import_module("_testinternalcapi") class Foo: def __init__(self, set_event): @@ -2217,6 +2219,8 @@ class TestOptimizer(MonitoringTestBase, unittest.TestCase): 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 @@ -2253,18 +2257,20 @@ class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): class Scope: def __init__(self, *args): + self._testcapi = import_helper.import_module("_testcapi") 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() def setUp(self): super(TestCApiEventGeneration, self).setUp() - capi = _testcapi + self._testcapi = import_helper.import_module("_testcapi") + capi = self._testcapi self.codelike = capi.CodeLike(2) @@ -2333,7 +2339,7 @@ class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): 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) @@ -2344,7 +2350,7 @@ class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): 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)) @@ -2354,7 +2360,7 @@ class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): 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) @@ -2404,12 +2410,14 @@ class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase): 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() -- 2.47.3