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
| 2223 | |
| 2224 | |
| 2225 | class clear_and_catch_warnings(warnings.catch_warnings): |
| 2226 | """ Context manager that resets warning registry for catching warnings |
| 2227 | |
| 2228 | Warnings can be slippery, because, whenever a warning is triggered, Python |
| 2229 | adds a ``__warningregistry__`` member to the *calling* module. This makes |
| 2230 | it impossible to retrigger the warning in this module, whatever you put in |
| 2231 | the warnings filters. This context manager accepts a sequence of `modules` |
| 2232 | as a keyword argument to its constructor and: |
| 2233 | |
| 2234 | * stores and removes any ``__warningregistry__`` entries in given `modules` |
| 2235 | on entry; |
| 2236 | * resets ``__warningregistry__`` to its previous state on exit. |
| 2237 | |
| 2238 | This makes it possible to trigger any warning afresh inside the context |
| 2239 | manager without disturbing the state of warnings outside. |
| 2240 | |
| 2241 | For compatibility with Python, please consider all arguments to be |
| 2242 | keyword-only. |
| 2243 | |
| 2244 | Parameters |
| 2245 | ---------- |
| 2246 | record : bool, optional |
| 2247 | Specifies whether warnings should be captured by a custom |
| 2248 | implementation of ``warnings.showwarning()`` and be appended to a list |
| 2249 | returned by the context manager. Otherwise None is returned by the |
| 2250 | context manager. The objects appended to the list are arguments whose |
| 2251 | attributes mirror the arguments to ``showwarning()``. |
| 2252 | modules : sequence, optional |
| 2253 | Sequence of modules for which to reset warnings registry on entry and |
| 2254 | restore on exit. To work correctly, all 'ignore' filters should |
| 2255 | filter by one of these modules. |
| 2256 | |
| 2257 | Examples |
| 2258 | -------- |
| 2259 | >>> import warnings |
| 2260 | >>> with np.testing.clear_and_catch_warnings( |
| 2261 | ... modules=[np._core.fromnumeric]): |
| 2262 | ... warnings.simplefilter('always') |
| 2263 | ... warnings.filterwarnings('ignore', module='np._core.fromnumeric') |
| 2264 | ... # do something that raises a warning but ignore those in |
| 2265 | ... # np._core.fromnumeric |
| 2266 | """ |
| 2267 | class_modules = () |
| 2268 | |
| 2269 | def __init__(self, record=False, modules=()): |
| 2270 | self.modules = set(modules).union(self.class_modules) |
| 2271 | self._warnreg_copies = {} |
| 2272 | super().__init__(record=record) |
| 2273 | |
| 2274 | def __enter__(self): |
| 2275 | for mod in self.modules: |
| 2276 | if hasattr(mod, '__warningregistry__'): |
| 2277 | mod_reg = mod.__warningregistry__ |
| 2278 | self._warnreg_copies[mod] = mod_reg.copy() |
| 2279 | mod_reg.clear() |
| 2280 | return super().__enter__() |
| 2281 | |
| 2282 | def __exit__(self, *exc_info): |
no outgoing calls
searching dependent graphs…