warnings.catch_warnings
Up: warnings | Next: warnings.deprecated
Rel: warnings.deprecated, warnings.filterwarnings, warnings.formatwarning, warnings.resetwarnings
Ref: docs.python.org — warnings.catch_warnings
Tags: #public #python3 #stdlib #warnings
Import
import warnings
from warnings import catch_warnings
Signature & Overview
warnings.catch_warnings(*, record=False, module=None, action=None, category=<class 'Warning'>, lineno=0, append=False)
A context manager that copies and restores the warnings filter upon exiting the context.
Usage
import warnings
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter('always')
warnings.warn('heads up', UserWarning)
assert len(caught) == 1 and str(caught[0].message) == 'heads up'
Gotchas
- Not thread-safe: Modifies global warning filters inside the
withblock.
REPL Inspect
from warnings import catch_warnings
help(catch_warnings)
Linked from
- tuts/python3/stdlib_module/warnings
- tuts/python3/stdlib_module/warnings/deprecated
- tuts/python3/stdlib_module/warnings/filterwarnings
- tuts/python3/stdlib_module/warnings/formatwarning
- tuts/python3/stdlib_module/warnings/resetwarnings
- tuts/python3/stdlib_module/warnings/showwarning
- tuts/python3/stdlib_module/warnings/simplefilter
- tuts/python3/stdlib_module/warnings/warn
- tuts/python3/stdlib_module/warnings/warn_explicit
Related notes
- tuts/python3/stdlib_module/warnings/deprecated Linked here; links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/filterwarnings Linked here; links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/formatwarning Linked here; links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/resetwarnings Linked here; links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings/showwarning links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/simplefilter links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/warn links to this page; shared tags: #stdlib, #warnings
- tuts/python3/stdlib_module/warnings/warn_explicit links to this page; shared tags: #stdlib, #warnings
- tuts/c/stdlib shared tags: #stdlib