Skip to content

Commit 6b49167

Browse files
committed
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 numpygh-8176
1 parent 0a02bb6 commit 6b49167

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

numpy/testing/utils.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,11 @@ def __init__(self, forwarding_rule="always"):
20272027
self._forwarding_rule = forwarding_rule
20282028

20292029
def _clear_registries(self):
2030+
if hasattr(warnings, "_filters_mutated"):
2031+
# clearing the registry should not be necessary on new pythons,
2032+
# instead the filters should be mutated.
2033+
warnings._filters_mutated()
2034+
return
20302035
# Simply clear the registry, this should normally be harmless,
20312036
# note that on new pythons it would be invalidated anyway.
20322037
for module in self._tmp_modules:
@@ -2116,6 +2121,8 @@ def __enter__(self):
21162121
raise RuntimeError("cannot enter suppress_warnings twice.")
21172122

21182123
self._orig_show = warnings.showwarning
2124+
if hasattr(warnings, "_showwarnmsg"):
2125+
self._orig_showmsg = warnings._showwarnmsg
21192126
self._filters = warnings.filters
21202127
warnings.filters = self._filters[:]
21212128

@@ -2139,20 +2146,29 @@ def __enter__(self):
21392146
module=module_regex)
21402147
self._tmp_modules.add(mod)
21412148
warnings.showwarning = self._showwarning
2149+
if hasattr(warnings, "_showwarnmsg"):
2150+
warnings._showwarnmsg = self._showwarnmsg
21422151
self._clear_registries()
21432152

21442153
return self
21452154

21462155
def __exit__(self, *exc_info):
21472156
warnings.showwarning = self._orig_show
2157+
if hasattr(warnings, "_showwarnmsg"):
2158+
warnings._showwarnmsg = self._orig_showmsg
21482159
warnings.filters = self._filters
21492160
self._clear_registries()
21502161
self._entered = False
21512162
del self._orig_show
21522163
del self._filters
21532164

2165+
def _showwarnmsg(self, msg):
2166+
self._showwarning(msg.message, msg.category, msg.filename, msg.lineno,
2167+
msg.file, msg.line, use_warnmsg=msg)
2168+
21542169
def _showwarning(self, message, category, filename, lineno,
21552170
*args, **kwargs):
2171+
use_warnmsg = kwargs.pop("use_warnmsg", None)
21562172
for cat, _, pattern, mod, rec in (
21572173
self._suppressions + self._tmp_suppressions)[::-1]:
21582174
if (issubclass(category, cat) and
@@ -2179,8 +2195,11 @@ def _showwarning(self, message, category, filename, lineno,
21792195
# There is no filter in place, so pass to the outside handler
21802196
# unless we should only pass it once
21812197
if self._forwarding_rule == "always":
2182-
self._orig_show(message, category, filename, lineno,
2183-
*args, **kwargs)
2198+
if use_warnmsg is None:
2199+
self._orig_show(message, category, filename, lineno,
2200+
*args, **kwargs)
2201+
else:
2202+
self._orig_showmsg(use_warnmsg)
21842203
return
21852204

21862205
if self._forwarding_rule == "once":
@@ -2193,7 +2212,11 @@ def _showwarning(self, message, category, filename, lineno,
21932212
if signature in self._forwarded:
21942213
return
21952214
self._forwarded.add(signature)
2196-
self._orig_show(message, category, filename, lineno, *args, **kwargs)
2215+
if use_warnmsg is None:
2216+
self._orig_show(message, category, filename, lineno, *args,
2217+
**kwargs)
2218+
else:
2219+
self._orig_showmsg(use_warnmsg)
21972220

21982221
def __call__(self, func):
21992222
"""

0 commit comments

Comments
 (0)