diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2012-02-08 19:51:45 -0800 |
---|---|---|
committer | Mark Wiebe <mwwiebe@gmail.com> | 2012-02-08 19:51:45 -0800 |
commit | a2bb1cc9b6fc37583494d9a3e14b0ace59d210a5 (patch) | |
tree | 105ce1da1f9e24933d86bcd5cbf5a05cbc445f17 | |
parent | c8c2082755457f4c16b0af7e1ca091dbb690341d (diff) | |
download | numpy-a2bb1cc9b6fc37583494d9a3e14b0ace59d210a5.tar.gz |
BUG: Fix improper usage of warning filters in the tests
-rw-r--r-- | numpy/core/tests/test_maskna.py | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_memmap.py | 1 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 17 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 12 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 76 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 11 | ||||
-rw-r--r-- | numpy/ma/tests/test_mrecords.py | 17 |
7 files changed, 88 insertions, 52 deletions
diff --git a/numpy/core/tests/test_maskna.py b/numpy/core/tests/test_maskna.py index 958dc8900..341ed0bc2 100644 --- a/numpy/core/tests/test_maskna.py +++ b/numpy/core/tests/test_maskna.py @@ -2,7 +2,7 @@ import numpy as np from numpy.compat import asbytes from numpy.testing import * import sys, warnings - +from numpy.testing.utils import WarningManager def test_array_maskna_flags(): a = np.arange(3) @@ -200,6 +200,8 @@ def test_array_maskna_astype(): dtdst.append(np.dtype('datetime64[D]')) dtdst.append(np.dtype('timedelta64[s]')) + warn_ctx = WarningManager() + warn_ctx.__enter__() try: warnings.simplefilter("ignore", np.ComplexWarning) for dt1 in dtsrc: @@ -212,7 +214,7 @@ def test_array_maskna_astype(): assert_(b.flags.ownmaskna, msg) assert_(np.isna(b[1]), msg) finally: - warnings.simplefilter("default", np.ComplexWarning) + warn_ctx.__exit__() def test_array_maskna_repr(): diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index fa4eb9de0..ed07532d7 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -1,6 +1,5 @@ from tempfile import NamedTemporaryFile, mktemp import os -import warnings from numpy import memmap from numpy import arange, allclose diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index adcd73872..6641a99f0 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -7,6 +7,9 @@ from nose import SkipTest from numpy.core import * from numpy.core.multiarray_tests import test_neighborhood_iterator, test_neighborhood_iterator_oob +import warnings +from numpy.testing.utils import WarningManager + # Need to test an object that does not fully implement math interface from datetime import timedelta @@ -1996,15 +1999,17 @@ class TestStackedNeighborhoodIter(TestCase): class TestWarnings(object): def test_complex_warning(self): - import warnings - x = np.array([1,2]) y = np.array([1-2j,1+2j]) - warnings.simplefilter("error", np.ComplexWarning) - assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y) - assert_equal(x, [1,2]) - warnings.simplefilter("default", np.ComplexWarning) + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + warnings.simplefilter("error", np.ComplexWarning) + assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y) + assert_equal(x, [1,2]) + finally: + warn_ctx.__exit__() if sys.version_info >= (2, 6): diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 5440949a1..55d5b6ec6 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1406,11 +1406,13 @@ class TestRegression(TestCase): for tp in [np.csingle, np.cdouble, np.clongdouble]: x = tp(1+2j) assert_warns(np.ComplexWarning, float, x) - ctx = WarningManager() - ctx.__enter__() - warnings.simplefilter('ignore') - assert_equal(float(x), float(x.real)) - ctx.__exit__() + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + 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]: diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 356534a25..04ca3fb4e 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -6,6 +6,7 @@ from tempfile import mkstemp, NamedTemporaryFile import time from datetime import datetime import warnings +from numpy.testing.utils import WarningManager import numpy as np import numpy.ma as ma @@ -460,13 +461,19 @@ class TestLoadTxt(TestCase): assert_array_equal(x, a) def test_empty_file(self): - warnings.filterwarnings("ignore", message="loadtxt: Empty input file:") - c = StringIO() - x = np.loadtxt(c) - assert_equal(x.shape, (0,)) - x = np.loadtxt(c, dtype=np.int64) - assert_equal(x.shape, (0,)) - assert_(x.dtype == np.int64) + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + warnings.filterwarnings("ignore", + message="loadtxt: Empty input file:") + c = StringIO() + x = np.loadtxt(c) + assert_equal(x.shape, (0,)) + x = np.loadtxt(c, dtype=np.int64) + assert_equal(x.shape, (0,)) + assert_(x.dtype == np.int64) + finally: + warn_ctx.__exit__() def test_unused_converter(self): @@ -707,25 +714,29 @@ class TestFromTxt(TestCase): assert_equal(test, ctrl) def test_skip_footer_with_invalid(self): - basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n' - warnings.filterwarnings("ignore") - # Footer too small to get rid of all invalid values - assert_raises(ValueError, np.genfromtxt, - StringIO(basestr), skip_footer=1) -# except ValueError: -# pass - a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False) - assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) - # - a = np.genfromtxt(StringIO(basestr), skip_footer=3) - assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) - # - basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n' - a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False) - assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]])) - a = np.genfromtxt(StringIO(basestr), skip_footer=3, invalid_raise=False) - assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]])) - warnings.resetwarnings() + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n' + warnings.filterwarnings("ignore") + # Footer too small to get rid of all invalid values + assert_raises(ValueError, np.genfromtxt, + StringIO(basestr), skip_footer=1) + # except ValueError: + # pass + a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False) + assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) + # + a = np.genfromtxt(StringIO(basestr), skip_footer=3) + assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])) + # + basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n' + a = np.genfromtxt(StringIO(basestr), skip_footer=1, invalid_raise=False) + assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]])) + a = np.genfromtxt(StringIO(basestr), skip_footer=3, invalid_raise=False) + assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]])) + finally: + warn_ctx.__exit__() def test_header(self): @@ -1030,10 +1041,15 @@ M 33 21.99 def test_empty_file(self): "Test that an empty file raises the proper warning." - warnings.filterwarnings("ignore", message="genfromtxt: Empty input file:") - data = StringIO() - test = np.genfromtxt(data) - assert_equal(test, np.array([])) + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + warnings.filterwarnings("ignore", message="genfromtxt: Empty input file:") + data = StringIO() + test = np.genfromtxt(data) + assert_equal(test, np.array([])) + finally: + warn_ctx.__exit__() def test_fancy_dtype_alt(self): "Check that a nested dtype isn't MIA" diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 95219beb1..f4ec54dc4 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -18,6 +18,7 @@ import numpy.ma.core from numpy.ma.core import * from numpy.compat import asbytes, asbytes_nested +from numpy.testing.utils import WarningManager pi = np.pi @@ -426,9 +427,13 @@ class TestMaskedArray(TestCase): assert_equal(1.0, float(array([[1]]))) self.assertRaises(TypeError, float, array([1, 1])) # - warnings.simplefilter('ignore', UserWarning) - assert_(np.isnan(float(array([1], mask=[1])))) - warnings.simplefilter('default', UserWarning) + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + warnings.simplefilter('ignore', UserWarning) + assert_(np.isnan(float(array([1], mask=[1])))) + finally: + warn_ctx.__exit__() # a = array([1, 2, 3], mask=[1, 0, 0]) self.assertRaises(TypeError, lambda:float(a)) diff --git a/numpy/ma/tests/test_mrecords.py b/numpy/ma/tests/test_mrecords.py index a6ee33b72..1e14042b0 100644 --- a/numpy/ma/tests/test_mrecords.py +++ b/numpy/ma/tests/test_mrecords.py @@ -22,6 +22,9 @@ from numpy.ma.testutils import * import numpy.ma as ma from numpy.ma import masked, nomask +import warnings +from numpy.testing.utils import WarningManager + from numpy.ma.mrecords import MaskedRecords, mrecarray, fromarrays, \ fromtextfile, fromrecords, addfield @@ -136,11 +139,15 @@ class TestMRecords(TestCase): rdata = data.view(MaskedRecords) val = ma.array([10,20,30], mask=[1,0,0]) # - import warnings - warnings.simplefilter("ignore") - rdata['num'] = val - assert_equal(rdata.num, val) - assert_equal(rdata.num.mask, [1,0,0]) + warn_ctx = WarningManager() + warn_ctx.__enter__() + try: + warnings.simplefilter("ignore") + rdata['num'] = val + assert_equal(rdata.num, val) + assert_equal(rdata.num.mask, [1,0,0]) + finally: + warn_ctx.__exit__() def test_set_fields_mask(self): "Tests setting the mask of a field." |