Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Add python 3.6 support to suppress_warnings #8177

Merged
merged 1 commit into from
Oct 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions numpy/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,11 @@ def __init__(self, forwarding_rule="always"):
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:
Expand Down Expand Up @@ -2116,6 +2121,8 @@ def __enter__(self):
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[:]

Expand All @@ -2139,20 +2146,29 @@ def __enter__(self):
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
Expand All @@ -2179,8 +2195,11 @@ def _showwarning(self, message, category, filename, lineno,
# 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":
Expand All @@ -2193,7 +2212,11 @@ def _showwarning(self, message, category, filename, lineno,
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):
"""
Expand Down