diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-07-11 20:29:08 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-07-12 07:27:07 -0600 |
commit | 05a15c8b621f953607429f3b67e079dfe1b439d6 (patch) | |
tree | c3c851bffa1d85399b4547f2a1620ad87c2c07bc /numpy/core | |
parent | a053a4372aba0af0bd63ffd5e207baf469cfc7bf (diff) | |
download | numpy-05a15c8b621f953607429f3b67e079dfe1b439d6.tar.gz |
MAINT: Remove uses of the WarningManager class.
WarningManager was a workaround for the lack of the with statement
in Python versions < 2.6. As those versions are no longer supported
it can be removed.
Deprecation notes are added to WarningManager and WarningMessage, but
to avoid a cascade of messages in third party apps, no warnings are
raised at this time, that can be done later.
Closes #3519.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/tests/test_api.py | 3 | ||||
-rw-r--r-- | numpy/core/tests/test_einsum.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 41 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 8 |
4 files changed, 22 insertions, 39 deletions
diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py index b94f4772e..41d12a30c 100644 --- a/numpy/core/tests/test_api.py +++ b/numpy/core/tests/test_api.py @@ -1,11 +1,10 @@ from __future__ import division, absolute_import, print_function import sys +import warnings import numpy as np from numpy.testing import * -from numpy.testing.utils import WarningManager -import warnings from numpy.compat import sixu # Switch between new behaviour when NPY_RELAXED_STRIDES_CHECKING is set. diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index dd5e9b85d..e7480a269 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -1,12 +1,11 @@ from __future__ import division, absolute_import, print_function import sys +import warnings from decimal import Decimal import numpy as np from numpy.testing import * -from numpy.testing.utils import WarningManager -import warnings class TestEinSum(TestCase): def test_einsum_errors(self): @@ -272,9 +271,7 @@ class TestEinSum(TestCase): assert_equal(np.einsum(a, [0], b, [1]), np.outer(a, b)) # Suppress the complex warnings for the 'as f8' tests - ctx = WarningManager() - ctx.__enter__() - try: + with warnings.catch_warnings(): warnings.simplefilter('ignore', np.ComplexWarning) # matvec(a,b) / a.dot(b) where a is matrix, b is vector @@ -379,8 +376,6 @@ class TestEinSum(TestCase): dtype='f8', casting='unsafe') assert_equal(c, np.tensordot(a.astype('f8'), b.astype('f8'), axes=([1,0],[0,1])).astype(dtype)) - finally: - ctx.__exit__() # logical_and(logical_and(a!=0, b!=0), c!=0) a = np.array([1, 3, -2, 0, 12, 13, 0, 1], dtype=dtype) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 232734b84..6cd24a76f 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -8,7 +8,6 @@ import warnings import numpy as np from nose import SkipTest from numpy.core import * -from numpy.testing.utils import WarningManager from numpy.compat import asbytes, getexception, strchar, sixu from test_print import in_foreign_locale from numpy.core.multiarray_tests import ( @@ -958,17 +957,13 @@ class TestMethods(TestCase): assert_equal(b.diagonal(0, 2, 1), [[0, 3], [4, 7]]) def test_diagonal_deprecation(self): - import warnings - from numpy.testing.utils import WarningManager + def collect_warning_types(f, *args, **kwargs): - ctx = WarningManager(record=True) - warning_log = ctx.__enter__() - warnings.simplefilter("always") - try: + with warnings.catch_warnings(record=True) as log: + warnings.simplefilter("always") f(*args, **kwargs) - finally: - ctx.__exit__() - return [w.category for w in warning_log] + return [w.category for w in log] + a = np.arange(9).reshape(3, 3) # All the different functions raise a warning, but not an error, and # 'a' is not modified: @@ -2028,17 +2023,13 @@ class TestRecord(TestCase): assert_raises(ValueError, a.__getitem__, sixu('\u03e0')) def test_field_names_deprecation(self): - import warnings - from numpy.testing.utils import WarningManager + def collect_warning_types(f, *args, **kwargs): - ctx = WarningManager(record=True) - warning_log = ctx.__enter__() - warnings.simplefilter("always") - try: + with warnings.catch_warnings(record=True) as log: + warnings.simplefilter("always") f(*args, **kwargs) - finally: - ctx.__exit__() - return [w.category for w in warning_log] + return [w.category for w in log] + a = np.zeros((1,), dtype=[('f1', 'i4'), ('f2', 'i4'), ('f3', [('sf1', 'i4')])]) @@ -2500,36 +2491,38 @@ class TestStackedNeighborhoodIter(TestCase): assert_array_equal(l, r) class TestWarnings(object): + def test_complex_warning(self): x = np.array([1,2]) y = np.array([1-2j,1+2j]) - warn_ctx = WarningManager() - warn_ctx.__enter__() - try: + with warnings.catch_warnings(): warnings.simplefilter("error", np.ComplexWarning) assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y) assert_equal(x, [1,2]) - finally: - warn_ctx.__exit__() class TestMinScalarType(object): + def test_usigned_shortshort(self): dt = np.min_scalar_type(2**8-1) wanted = np.dtype('uint8') assert_equal(wanted, dt) + def test_usigned_short(self): dt = np.min_scalar_type(2**16-1) wanted = np.dtype('uint16') assert_equal(wanted, dt) + def test_usigned_int(self): dt = np.min_scalar_type(2**32-1) wanted = np.dtype('uint32') assert_equal(wanted, dt) + def test_usigned_longlong(self): dt = np.min_scalar_type(2**63-1) wanted = np.dtype('uint64') assert_equal(wanted, dt) + def test_object(self): dt = np.min_scalar_type(2**64) wanted = np.dtype('O') diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 492b08cb9..740957215 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -16,7 +16,7 @@ from numpy.testing import ( assert_almost_equal, assert_array_equal, assert_array_almost_equal, assert_raises, assert_warns, dec ) -from numpy.testing.utils import _assert_valid_refcount, WarningManager +from numpy.testing.utils import _assert_valid_refcount from numpy.compat import asbytes, asunicode, asbytes_nested, long, sixu rlevel = 1 @@ -1545,13 +1545,9 @@ class TestRegression(TestCase): for tp in [np.csingle, np.cdouble, np.clongdouble]: x = tp(1+2j) assert_warns(np.ComplexWarning, float, x) - warn_ctx = WarningManager() - warn_ctx.__enter__() - try: + with warnings.catch_warnings(): warnings.simplefilter('ignore') assert_equal(float(x), float(x.real)) - finally: - warn_ctx.__exit__() def test_complex_scalar_complex_cast(self): for tp in [np.csingle, np.cdouble, np.clongdouble]: |