Context manager that resets warning registry for catching warnings Warnings can be slippery, because, whenever a warning is triggered, Python adds a ``__warningregistry__`` member to the *calling* module. This makes it impossible to retrigger the warning in this module, whatever you p
| 1936 | |
| 1937 | |
| 1938 | class clear_and_catch_warnings(warnings.catch_warnings): |
| 1939 | """ Context manager that resets warning registry for catching warnings |
| 1940 | |
| 1941 | Warnings can be slippery, because, whenever a warning is triggered, Python |
| 1942 | adds a ``__warningregistry__`` member to the *calling* module. This makes |
| 1943 | it impossible to retrigger the warning in this module, whatever you put in |
| 1944 | the warnings filters. This context manager accepts a sequence of `modules` |
| 1945 | as a keyword argument to its constructor and: |
| 1946 | |
| 1947 | * stores and removes any ``__warningregistry__`` entries in given `modules` |
| 1948 | on entry; |
| 1949 | * resets ``__warningregistry__`` to its previous state on exit. |
| 1950 | |
| 1951 | This makes it possible to trigger any warning afresh inside the context |
| 1952 | manager without disturbing the state of warnings outside. |
| 1953 | |
| 1954 | For compatibility with Python 3.0, please consider all arguments to be |
| 1955 | keyword-only. |
| 1956 | |
| 1957 | Parameters |
| 1958 | ---------- |
| 1959 | record : bool, optional |
| 1960 | Specifies whether warnings should be captured by a custom |
| 1961 | implementation of ``warnings.showwarning()`` and be appended to a list |
| 1962 | returned by the context manager. Otherwise None is returned by the |
| 1963 | context manager. The objects appended to the list are arguments whose |
| 1964 | attributes mirror the arguments to ``showwarning()``. |
| 1965 | modules : sequence, optional |
| 1966 | Sequence of modules for which to reset warnings registry on entry and |
| 1967 | restore on exit. To work correctly, all 'ignore' filters should |
| 1968 | filter by one of these modules. |
| 1969 | |
| 1970 | Examples |
| 1971 | -------- |
| 1972 | >>> import warnings |
| 1973 | >>> with np.testing.clear_and_catch_warnings( |
| 1974 | ... modules=[np.core.fromnumeric]): |
| 1975 | ... warnings.simplefilter('always') |
| 1976 | ... warnings.filterwarnings('ignore', module='np.core.fromnumeric') |
| 1977 | ... # do something that raises a warning but ignore those in |
| 1978 | ... # np.core.fromnumeric |
| 1979 | """ |
| 1980 | class_modules = () |
| 1981 | |
| 1982 | def __init__(self, record=False, modules=()): |
| 1983 | self.modules = set(modules).union(self.class_modules) |
| 1984 | self._warnreg_copies = {} |
| 1985 | super().__init__(record=record) |
| 1986 | |
| 1987 | def __enter__(self): |
| 1988 | for mod in self.modules: |
| 1989 | if hasattr(mod, '__warningregistry__'): |
| 1990 | mod_reg = mod.__warningregistry__ |
| 1991 | self._warnreg_copies[mod] = mod_reg.copy() |
| 1992 | mod_reg.clear() |
| 1993 | return super().__enter__() |
| 1994 | |
| 1995 | def __exit__(self, *exc_info): |
no outgoing calls