class warnings.catch_warnings(*, record=False, module=None)
A context manager that copies and, upon exit, restores the warnings filter and the showwarning()
function. If the record argument is False
(the default) the context manager returns None
on entry. If record is True
, a list is returned that is progressively populated with objects as seen by a custom showwarning()
function (which also suppresses output to sys.stdout
). Each object in the list has attributes with the same names as the arguments to showwarning()
.
The module argument takes a module that will be used instead of the module returned when you import warnings
whose filter will be protected. This argument exists primarily for testing the warnings
module itself.
Note
The catch_warnings
manager works by replacing and then later restoring the module’s showwarning()
function and internal list of filter specifications. This means the context manager is modifying global state and therefore is not thread-safe.
Please login to continue.