Skip to content

Commit 67f69a2

Browse files
committed
Catch warnings with simplefilter
1 parent 7fbc7f6 commit 67f69a2

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,25 @@ def test_append_duplicate(self):
373373
"appended duplicate changed order of filters"
374374
)
375375

376+
def test_catchwarnings_with_simplefilter_ignore(self):
377+
with original_warnings.catch_warnings(module=self.module):
378+
self.module.resetwarnings()
379+
self.module.simplefilter("error")
380+
with self.module.catch_warnings(
381+
module=self.module, action="ignore"
382+
):
383+
self.module.warn("This will be ignored")
384+
385+
def test_catchwarnings_with_simplefilter_error(self):
386+
with original_warnings.catch_warnings(module=self.module):
387+
self.module.resetwarnings()
388+
with self.module.catch_warnings(
389+
module=self.module, action="error", category=FutureWarning
390+
):
391+
self.module.warn("Other types of warnings are not errors")
392+
self.assertRaises(FutureWarning,
393+
self.module.warn, FutureWarning("msg"))
394+
376395
class CFilterTests(FilterTests, unittest.TestCase):
377396
module = c_warnings
378397

Lib/warnings.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,16 @@ class catch_warnings(object):
432432
named 'warnings' and imported under that name. This argument is only useful
433433
when testing the warnings module itself.
434434
435+
If the 'action' argument is not None, the remaining arguments are passed
436+
to warnings.simplefilter() as if it that call was the first line of the
437+
with-statement.
438+
439+
.. versionchanged:: 3.11
440+
Added the action, category, lineno, and append arguments.
435441
"""
436442

437-
def __init__(self, *, record=False, module=None):
443+
def __init__(self, *, record=False, module=None,
444+
action=None, category=Warning, lineno=0, append=False):
438445
"""Specify whether to record warnings and if an alternative module
439446
should be used other than sys.modules['warnings'].
440447
@@ -445,6 +452,10 @@ def __init__(self, *, record=False, module=None):
445452
self._record = record
446453
self._module = sys.modules['warnings'] if module is None else module
447454
self._entered = False
455+
if action is None:
456+
self._filter = None
457+
else:
458+
self._filter = (action, category, lineno, append)
448459

449460
def __repr__(self):
450461
args = []
@@ -464,6 +475,8 @@ def __enter__(self):
464475
self._module._filters_mutated()
465476
self._showwarning = self._module.showwarning
466477
self._showwarnmsg_impl = self._module._showwarnmsg_impl
478+
if self._filter is not None:
479+
simplefilter(*self._filter)
467480
if self._record:
468481
log = []
469482
self._module._showwarnmsg_impl = log.append
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`warnings.catch_warnings` now accepts arguments for
2+
:func:`warnings.simplefilter`, providing a more concise way to
3+
locally ignore warnings or convert them to errors.

0 commit comments

Comments
 (0)