diff options
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r-- | numpy/testing/utils.py | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index f58997d58..bc0c59502 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -71,14 +71,11 @@ def gisfinite(x): exception is always raised. This should be removed once this problem is solved at the Ufunc level.""" - from numpy.core import isfinite, seterr - err = seterr(invalid='ignore') - try: + from numpy.core import isfinite, errstate + with errstate(invalid='ignore'): st = isfinite(x) if isinstance(st, type(NotImplemented)): raise TypeError("isfinite not supported for this type") - finally: - seterr(**err) return st def gisinf(x): @@ -92,14 +89,11 @@ def gisinf(x): exception is always raised. This should be removed once this problem is solved at the Ufunc level.""" - from numpy.core import isinf, seterr - err = seterr(invalid='ignore') - try: + from numpy.core import isinf, errstate + with errstate(invalid='ignore'): st = isinf(x) if isinstance(st, type(NotImplemented)): raise TypeError("isinf not supported for this type") - finally: - seterr(**err) return st def rand(*args): @@ -539,13 +533,9 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): return # Normalized the numbers to be in range (-10.0,10.0) # scale = float(pow(10,math.floor(math.log10(0.5*(abs(desired)+abs(actual)))))) - err = np.seterr(invalid='ignore') - try: + with np.errstate(invalid='ignore'): scale = 0.5*(np.abs(desired) + np.abs(actual)) scale = np.power(10,np.floor(np.log10(scale))) - finally: - np.seterr(**err) - try: sc_desired = desired/scale except ZeroDivisionError: @@ -1375,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, @@ -1415,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 @@ -1477,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" @@ -1491,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): @@ -1513,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 |