summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r--numpy/testing/utils.py29
1 files changed, 26 insertions, 3 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index b01de173d..d17e7e404 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -2027,6 +2027,11 @@ class suppress_warnings(object):
self._forwarding_rule = forwarding_rule
def _clear_registries(self):
+ if hasattr(warnings, "_filters_mutated"):
+ # clearing the registry should not be necessary on new pythons,
+ # instead the filters should be mutated.
+ warnings._filters_mutated()
+ return
# Simply clear the registry, this should normally be harmless,
# note that on new pythons it would be invalidated anyway.
for module in self._tmp_modules:
@@ -2116,6 +2121,8 @@ class suppress_warnings(object):
raise RuntimeError("cannot enter suppress_warnings twice.")
self._orig_show = warnings.showwarning
+ if hasattr(warnings, "_showwarnmsg"):
+ self._orig_showmsg = warnings._showwarnmsg
self._filters = warnings.filters
warnings.filters = self._filters[:]
@@ -2139,20 +2146,29 @@ class suppress_warnings(object):
module=module_regex)
self._tmp_modules.add(mod)
warnings.showwarning = self._showwarning
+ if hasattr(warnings, "_showwarnmsg"):
+ warnings._showwarnmsg = self._showwarnmsg
self._clear_registries()
return self
def __exit__(self, *exc_info):
warnings.showwarning = self._orig_show
+ if hasattr(warnings, "_showwarnmsg"):
+ warnings._showwarnmsg = self._orig_showmsg
warnings.filters = self._filters
self._clear_registries()
self._entered = False
del self._orig_show
del self._filters
+ def _showwarnmsg(self, msg):
+ self._showwarning(msg.message, msg.category, msg.filename, msg.lineno,
+ msg.file, msg.line, use_warnmsg=msg)
+
def _showwarning(self, message, category, filename, lineno,
*args, **kwargs):
+ use_warnmsg = kwargs.pop("use_warnmsg", None)
for cat, _, pattern, mod, rec in (
self._suppressions + self._tmp_suppressions)[::-1]:
if (issubclass(category, cat) and
@@ -2179,8 +2195,11 @@ class suppress_warnings(object):
# There is no filter in place, so pass to the outside handler
# unless we should only pass it once
if self._forwarding_rule == "always":
- self._orig_show(message, category, filename, lineno,
- *args, **kwargs)
+ if use_warnmsg is None:
+ self._orig_show(message, category, filename, lineno,
+ *args, **kwargs)
+ else:
+ self._orig_showmsg(use_warnmsg)
return
if self._forwarding_rule == "once":
@@ -2193,7 +2212,11 @@ class suppress_warnings(object):
if signature in self._forwarded:
return
self._forwarded.add(signature)
- self._orig_show(message, category, filename, lineno, *args, **kwargs)
+ if use_warnmsg is None:
+ self._orig_show(message, category, filename, lineno, *args,
+ **kwargs)
+ else:
+ self._orig_showmsg(use_warnmsg)
def __call__(self, func):
"""