Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
Loading