summaryrefslogtreecommitdiff
path: root/numpy/testing
diff options
context:
space:
mode:
authorRalf Gommers <ralf.gommers@googlemail.com>2013-07-16 12:28:18 -0700
committerRalf Gommers <ralf.gommers@googlemail.com>2013-07-16 12:28:18 -0700
commitc067c156bf2ffb96d93083e468158ecbc35baba4 (patch)
treeb73812a3a1a95a044c07c6352b5e254ffd792793 /numpy/testing
parentb55f2752140de3bd6969dd66960f649da44974a5 (diff)
parent05a15c8b621f953607429f3b67e079dfe1b439d6 (diff)
downloadnumpy-c067c156bf2ffb96d93083e468158ecbc35baba4.tar.gz
Merge pull request #3520 from charris/replace-warningmanager
Replace use of WarningManager by warnings.catch_warnings and then deprecate it.
Diffstat (limited to 'numpy/testing')
-rw-r--r--numpy/testing/decorators.py13
-rw-r--r--numpy/testing/nosetester.py41
-rw-r--r--numpy/testing/utils.py25
3 files changed, 29 insertions, 50 deletions
diff --git a/numpy/testing/decorators.py b/numpy/testing/decorators.py
index b0ddb1880..8d1fb04bb 100644
--- a/numpy/testing/decorators.py
+++ b/numpy/testing/decorators.py
@@ -15,11 +15,8 @@ function name, setup and teardown functions and so on - see
"""
from __future__ import division, absolute_import, print_function
-import warnings
import sys
-
-from numpy.testing.utils import \
- WarningManager, WarningMessage
+import warnings
import collections
def slow(t):
@@ -253,10 +250,8 @@ def deprecated(conditional=True):
def _deprecated_imp(*args, **kwargs):
# Poor man's replacement for the with statement
- ctx = WarningManager(record=True)
- l = ctx.__enter__()
- warnings.simplefilter('always')
- try:
+ with warnings.catch_warnings(record=True) as l:
+ warnings.simplefilter('always')
f(*args, **kwargs)
if not len(l) > 0:
raise AssertionError("No warning raised when calling %s"
@@ -264,8 +259,6 @@ def deprecated(conditional=True):
if not l[0].category is DeprecationWarning:
raise AssertionError("First warning for %s is not a " \
"DeprecationWarning( is %s)" % (f.__name__, l[0]))
- finally:
- ctx.__exit__()
if isinstance(conditional, collections.Callable):
cond = conditional()
diff --git a/numpy/testing/nosetester.py b/numpy/testing/nosetester.py
index f1ebd2265..f11f04b20 100644
--- a/numpy/testing/nosetester.py
+++ b/numpy/testing/nosetester.py
@@ -363,32 +363,27 @@ class NoseTester(object):
if raise_warnings in _warn_opts.keys():
raise_warnings = _warn_opts[raise_warnings]
- # Preserve the state of the warning filters
- warn_ctx = numpy.testing.utils.WarningManager()
- warn_ctx.__enter__()
- # Reset the warning filters to the default state,
- # so that running the tests is more repeatable.
- warnings.resetwarnings()
- # If deprecation warnings are not set to 'error' below,
- # at least set them to 'warn'.
- warnings.filterwarnings('always', category=DeprecationWarning)
- # Force the requested warnings to raise
- for warningtype in raise_warnings:
- warnings.filterwarnings('error', category=warningtype)
- # Filter out annoying import messages.
- warnings.filterwarnings('ignore', message='Not importing directory')
- warnings.filterwarnings("ignore", message="numpy.dtype size changed")
- warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
- warnings.filterwarnings("ignore", category=ModuleDeprecationWarning)
-
- try:
+ with warnings.catch_warnings():
+ # Reset the warning filters to the default state,
+ # so that running the tests is more repeatable.
+ warnings.resetwarnings()
+ # If deprecation warnings are not set to 'error' below,
+ # at least set them to 'warn'.
+ warnings.filterwarnings('always', category=DeprecationWarning)
+ # Force the requested warnings to raise
+ for warningtype in raise_warnings:
+ warnings.filterwarnings('error', category=warningtype)
+ # Filter out annoying import messages.
+ warnings.filterwarnings('ignore', message='Not importing directory')
+ warnings.filterwarnings("ignore", message="numpy.dtype size changed")
+ warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
+ warnings.filterwarnings("ignore", category=ModuleDeprecationWarning)
+
from .noseclasses import NumpyTestProgram
- argv, plugins = self.prepare_test_args(label,
- verbose, extra_argv, doctests, coverage)
+ argv, plugins = self.prepare_test_args(
+ label, verbose, extra_argv, doctests, coverage)
t = NumpyTestProgram(argv=argv, exit=False, plugins=plugins)
- finally:
- warn_ctx.__exit__()
return t.result
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index ca564721a..bc0c59502 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -1365,6 +1365,8 @@ class WarningMessage(object):
"""
Holds the result of a single showwarning() call.
+ Deprecated in 1.8.0
+
Notes
-----
`WarningMessage` is copied from the Python 2.6 warnings module,
@@ -1405,6 +1407,8 @@ class WarningManager(object):
named 'warnings' and imported under that name. This argument is only useful
when testing the warnings module itself.
+ Deprecated in 1.8.0
+
Notes
-----
`WarningManager` is a copy of the ``catch_warnings`` context manager
@@ -1467,13 +1471,8 @@ def assert_warns(warning_class, func, *args, **kw):
The value returned by `func`.
"""
-
- # XXX: once we may depend on python >= 2.6, this can be replaced by the
- # warnings module context manager.
- ctx = WarningManager(record=True)
- l = ctx.__enter__()
- warnings.simplefilter('always')
- try:
+ with warnings.catch_warnings(record=True) as l:
+ warnings.simplefilter('always')
result = func(*args, **kw)
if not len(l) > 0:
raise AssertionError("No warning raised when calling %s"
@@ -1481,8 +1480,6 @@ def assert_warns(warning_class, func, *args, **kw):
if not l[0].category is warning_class:
raise AssertionError("First warning for %s is not a " \
"%s( is %s)" % (func.__name__, warning_class, l[0]))
- finally:
- ctx.__exit__()
return result
def assert_no_warnings(func, *args, **kw):
@@ -1503,18 +1500,12 @@ def assert_no_warnings(func, *args, **kw):
The value returned by `func`.
"""
- # XXX: once we may depend on python >= 2.6, this can be replaced by the
- # warnings module context manager.
- ctx = WarningManager(record=True)
- l = ctx.__enter__()
- warnings.simplefilter('always')
- try:
+ with warnings.catch_warnings(record=True) as l:
+ warnings.simplefilter('always')
result = func(*args, **kw)
if len(l) > 0:
raise AssertionError("Got warnings when calling %s: %s"
% (func.__name__, l))
- finally:
- ctx.__exit__()
return result