summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2016-10-19 15:57:35 +0200
committerSebastian Berg <sebastian@sipsolutions.net>2016-10-19 16:49:17 +0200
commit6b49167c3134e695efb1a122f73c3524a9049db5 (patch)
treec0fea9872674f477d17b727e8b8b8c0444a7dcd3 /numpy/testing/utils.py
parent0a02bb6f62a5c09103cf748bafe7a622f3dfe98b (diff)
downloadnumpy-6b49167c3134e695efb1a122f73c3524a9049db5.tar.gz
MAINT: Add python 3.6 support to suppress_warnings
Python 3.6 has two methods for showing warnings, so we have to support both ways (strictly speaking, on python 3.6 itself only the new would probably be needed). Also adds the _filters_mutated() call to replace manual registry clearing. This is basically the fix that newer python versions have to avoid the issue making the manual clearing unnecessary. Closes gh-8176
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):
"""