diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-07-11 16:49:04 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-07-11 16:49:04 -0600 |
commit | 2f1174dee44e901b7d028beb86f4a8ea324bd74f (patch) | |
tree | 8f09dc2bd35e2631f5821fe2e998f6ea46e254b8 /numpy/core/numeric.py | |
parent | 49a587cd786242b05fcfd22d5cda961d733b68d4 (diff) | |
download | numpy-2f1174dee44e901b7d028beb86f4a8ea324bd74f.tar.gz |
MAINT: Use np.errstate context manager.
Now that Python < 2.6 is no longer supported we can use the errstate
context manager in places where constructs like
```
old = seterr(invalid='ignore')
try:
blah
finally:
seterr(**old)
```
were used.
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 13ee89744..9ae1af654 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2125,8 +2125,7 @@ def allclose(a, b, rtol=1.e-5, atol=1.e-8): y = y[~xinf] # ignore invalid fpe's - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with errstate(invalid='ignore'): r = all(less_equal(abs(x-y), atol + rtol * abs(y))) return r @@ -2191,11 +2190,8 @@ def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False): array([True, True]) """ def within_tol(x, y, atol, rtol): - err = seterr(invalid='ignore') - try: + with errstate(invalid='ignore'): result = less_equal(abs(x-y), atol + rtol * abs(y)) - finally: - seterr(**err) if isscalar(a) and isscalar(b): result = bool(result) return result @@ -2705,15 +2701,18 @@ class errstate(object): def __init__(self, **kwargs): self.call = kwargs.pop('call',_Unspecified) self.kwargs = kwargs + def __enter__(self): self.oldstate = seterr(**self.kwargs) if self.call is not _Unspecified: self.oldcall = seterrcall(self.call) + def __exit__(self, *exc_info): seterr(**self.oldstate) if self.call is not _Unspecified: seterrcall(self.oldcall) + def _setdef(): defval = [UFUNC_BUFSIZE_DEFAULT, ERR_DEFAULT2, None] umath.seterrobj(defval) |