From d7cd11c643e480a9c69cb6f517ce95089e993e37 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 12 Aug 2026 06:52:51 -0400 Subject: [PATCH] Fix flakey test_warnings free threading tests Ensure we cleanup after ourselves on teardown --- Lib/test/test_warnings/__init__.py | 36 +++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index bf1bcf8e6ed5d9..3ae3d6a060e317 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -410,13 +410,16 @@ def test_message_matching(self): self.assertEqual(w, []) def test_mutate_filter_list(self): - class X: - def match(self, a, start=0): - L[:] = [] - - L = [("default",X(),UserWarning,X(),0) for i in range(2)] with self.module.catch_warnings(record=True) as w: - self.module.filters = L + # In context-aware mode the active filter list is the current + # context's own list, not warnings.filters, so mutate that list + # directly. (Assigning warnings.filters would leave the ambient + # filters -- e.g. an "error" filter from -W error -- in effect.) + L = self.module._get_filters() + class X: + def match(self, a, start=0): + L[:] = [] + L[:] = [("default",X(),UserWarning,X(),0) for i in range(2)] self.module.warn_explicit(UserWarning("b"), None, "f.py", 42) self.assertEqual(str(w[-1].message), "b") @@ -1720,8 +1723,22 @@ class AsyncTests(BaseTest): def setUp(self): super().setUp() + # Reset the filters for this test, but restore the module's filter list + # in tearDown. These tests exercise the C 'warnings' module, whose + # filters list is the interpreter-global one that regrtest checks for + # modification; leaving it cleared triggers a spurious "env changed". + # Save and restore the list contents directly rather than using + # catch_warnings(): that manipulates the warnings context variable and, + # combined with the threads/tasks these tests spawn, can leave a stale + # context active for later tests. + self._saved_filters = self.module.filters[:] self.module.resetwarnings() + def tearDown(self): + self.module.filters[:] = self._saved_filters + self.module._filters_mutated() + super().tearDown() + @unittest.skipIf(not sys.flags.context_aware_warnings, "requires context aware warnings") def test_async_context(self): @@ -1816,8 +1833,15 @@ class ThreadTests(BaseTest): def setUp(self): super().setUp() + self._saved_filters = self.module.filters[:] self.module.resetwarnings() + def tearDown(self): + # Restore module filters after test run to ensure a clean global state + self.module.filters[:] = self._saved_filters + self.module._filters_mutated() + super().tearDown() + @unittest.skipIf(not ENABLE_THREAD_TESTS, "requires thread-safe warnings flags") def test_threaded_context(self):