diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/arrayprint.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_datetime.py | 72 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_einsum.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_function_base.py | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_memmap.py | 16 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 26 | ||||
-rw-r--r-- | numpy/core/tests/test_nditer.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 43 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 39 | ||||
-rw-r--r-- | numpy/core/tests/test_scalarmath.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 7 |
12 files changed, 130 insertions, 119 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 282fbd1cf..b05082e9d 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -20,7 +20,7 @@ from functools import reduce from . import numerictypes as _nt from .umath import maximum, minimum, absolute, not_equal, isnan, isinf from .multiarray import (array, format_longfloat, datetime_as_string, - datetime_data) + datetime_data, dtype) from .fromnumeric import ravel from .numeric import asarray @@ -734,7 +734,9 @@ class TimedeltaFormat(object): def __init__(self, data): if data.dtype.kind == 'm': nat_value = array(['NaT'], dtype=data.dtype)[0] - v = data[not_equal(data, nat_value)].view('i8') + int_dtype = dtype(data.dtype.byteorder + 'i8') + int_view = data.view(int_dtype) + v = int_view[not_equal(int_view, nat_value.view(int_dtype))] if len(v) > 0: # Max str length of non-NaT elements max_str_len = max(len(str(maximum.reduce(v))), @@ -748,7 +750,8 @@ class TimedeltaFormat(object): self._nat = "'NaT'".rjust(max_str_len) def __call__(self, x): - if x + 1 == x: + # TODO: After NAT == NAT deprecation should be simplified: + if (x + 1).view('i8') == x.view('i8'): return self._nat else: return self.format % x.astype('i8') diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index 601f09c09..e443b3be0 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -1,7 +1,6 @@ from __future__ import division, absolute_import, print_function import pickle -import warnings import numpy import numpy as np @@ -9,7 +8,7 @@ import datetime from numpy.compat import asbytes from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_raises, - assert_warns, dec + assert_warns, dec, suppress_warnings ) # Use pytz to test out various time zones if available @@ -129,10 +128,11 @@ class TestDateTime(TestCase): # regression tests for GH6452 assert_equal(np.datetime64('NaT'), np.datetime64('2000') + np.timedelta64('NaT')) - # nb. we may want to make NaT != NaT true in the future; this test - # verifies the existing behavior (and that it should not warn) - assert_(np.datetime64('NaT') == np.datetime64('NaT', 'us')) - assert_(np.datetime64('NaT', 'us') == np.datetime64('NaT')) + # nb. we may want to make NaT != NaT true in the future + with suppress_warnings() as sup: + sup.filter(FutureWarning, ".*NAT ==") + assert_(np.datetime64('NaT') == np.datetime64('NaT', 'us')) + assert_(np.datetime64('NaT', 'us') == np.datetime64('NaT')) def test_datetime_scalar_construction(self): # Construct with different units @@ -572,6 +572,12 @@ class TestDateTime(TestCase): a = np.array([-1, 'NaT', 1234567], dtype='m') assert_equal(str(a), "[ -1 'NaT' 1234567]") + # Test with other byteorder: + a = np.array([-1, 'NaT', 1234567], dtype='>m') + assert_equal(str(a), "[ -1 'NaT' 1234567]") + a = np.array([-1, 'NaT', 1234567], dtype='<m') + assert_equal(str(a), "[ -1 'NaT' 1234567]") + def test_pickle(self): # Check that pickle roundtripping works dt = np.dtype('M8[7D]') @@ -989,8 +995,8 @@ class TestDateTime(TestCase): assert_raises(TypeError, np.multiply, 1.5, dta) # NaTs - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', category=RuntimeWarning) + with suppress_warnings() as sup: + sup.filter(RuntimeWarning, "invalid value encountered in multiply") nat = np.timedelta64('NaT') def check(a, b, res): assert_equal(a * b, res) @@ -1053,8 +1059,8 @@ class TestDateTime(TestCase): assert_raises(TypeError, np.divide, 1.5, dta) # NaTs - with warnings.catch_warnings(): - warnings.filterwarnings('ignore', category=RuntimeWarning) + with suppress_warnings() as sup: + sup.filter(RuntimeWarning, r".*encountered in true\_divide") nat = np.timedelta64('NaT') for tp in (int, float): assert_equal(np.timedelta64(1) / tp(0), nat) @@ -1092,27 +1098,31 @@ class TestDateTime(TestCase): td_nat = np.timedelta64('NaT', 'h') td_other = np.timedelta64(1, 'h') - for op in [np.equal, np.less, np.less_equal, - np.greater, np.greater_equal]: - if op(dt_nat, dt_nat): - assert_warns(FutureWarning, op, dt_nat, dt_nat) - if op(dt_nat, dt_other): - assert_warns(FutureWarning, op, dt_nat, dt_other) - if op(dt_other, dt_nat): - assert_warns(FutureWarning, op, dt_other, dt_nat) - if op(td_nat, td_nat): - assert_warns(FutureWarning, op, td_nat, td_nat) - if op(td_nat, td_other): - assert_warns(FutureWarning, op, td_nat, td_other) - if op(td_other, td_nat): - assert_warns(FutureWarning, op, td_other, td_nat) - - assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat) - assert_(np.not_equal(dt_nat, dt_other)) - assert_(np.not_equal(dt_other, dt_nat)) - assert_warns(FutureWarning, np.not_equal, td_nat, td_nat) - assert_(np.not_equal(td_nat, td_other)) - assert_(np.not_equal(td_other, td_nat)) + with suppress_warnings() as sup: + # The assert warns contexts will again see the warning: + sup.filter(FutureWarning, ".*NAT") + + for op in [np.equal, np.less, np.less_equal, + np.greater, np.greater_equal]: + if op(dt_nat, dt_nat): + assert_warns(FutureWarning, op, dt_nat, dt_nat) + if op(dt_nat, dt_other): + assert_warns(FutureWarning, op, dt_nat, dt_other) + if op(dt_other, dt_nat): + assert_warns(FutureWarning, op, dt_other, dt_nat) + if op(td_nat, td_nat): + assert_warns(FutureWarning, op, td_nat, td_nat) + if op(td_nat, td_other): + assert_warns(FutureWarning, op, td_nat, td_other) + if op(td_other, td_nat): + assert_warns(FutureWarning, op, td_other, td_nat) + + assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat) + assert_(np.not_equal(dt_nat, dt_other)) + assert_(np.not_equal(dt_other, dt_nat)) + assert_warns(FutureWarning, np.not_equal, td_nat, td_nat) + assert_(np.not_equal(td_nat, td_other)) + assert_(np.not_equal(td_other, td_nat)) def test_datetime_minmax(self): # The metadata of the result should become the GCD diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 547280b23..e03edb2ea 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -644,6 +644,8 @@ class TestTestDeprecated(object): warnings.warn("foo", category=DeprecationWarning) test_case_instance.assert_deprecated(foo) + test_case_instance.tearDown() + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index 77fb75f10..c31d281e9 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -1,13 +1,12 @@ from __future__ import division, absolute_import, print_function -import warnings - import numpy as np from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_array_equal, - assert_raises + assert_raises, suppress_warnings ) + class TestEinSum(TestCase): def test_einsum_errors(self): # Need enough arguments @@ -282,8 +281,8 @@ class TestEinSum(TestCase): assert_equal(np.einsum(a, [0], b, [1]), np.outer(a, b)) # Suppress the complex warnings for the 'as f8' tests - with warnings.catch_warnings(): - warnings.simplefilter('ignore', np.ComplexWarning) + with suppress_warnings() as sup: + sup.filter(np.ComplexWarning) # matvec(a,b) / a.dot(b) where a is matrix, b is vector for n in range(1, 17): diff --git a/numpy/core/tests/test_function_base.py b/numpy/core/tests/test_function_base.py index 0fabb2588..9b20c4ff5 100644 --- a/numpy/core/tests/test_function_base.py +++ b/numpy/core/tests/test_function_base.py @@ -4,7 +4,7 @@ from numpy import (logspace, linspace, geomspace, dtype, array, finfo, typecodes, arange, isnan, ndarray, sqrt) from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_raises, - assert_array_equal, assert_allclose + assert_array_equal, assert_allclose, suppress_warnings ) @@ -205,8 +205,10 @@ class TestLinspace(TestCase): def test_corner(self): y = list(linspace(0, 1, 1)) assert_(y == [0.0], y) - y = list(linspace(0, 1, 2.5)) - assert_(y == [0.0, 1.0]) + with suppress_warnings() as sup: + sup.filter(DeprecationWarning, ".*safely interpreted as an integer") + y = list(linspace(0, 1, 2.5)) + assert_(y == [0.0, 1.0]) def test_type(self): t1 = linspace(0, 1, 0).dtype diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index 4aa02e26f..30c8b7c54 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -12,7 +12,7 @@ from numpy.compat import Path from numpy import arange, allclose, asarray from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_array_equal, - dec + dec, suppress_warnings ) class TestMemmap(TestCase): @@ -146,13 +146,15 @@ class TestMemmap(TestCase): fp = memmap(self.tmpfp, dtype=self.dtype, shape=self.shape) fp[:] = self.data - for unary_op in [sum, average, product]: - result = unary_op(fp) - assert_(isscalar(result)) - assert_(result.__class__ is self.data[0, 0].__class__) + with suppress_warnings() as sup: + sup.filter(FutureWarning, "np.average currently does not preserve") + for unary_op in [sum, average, product]: + result = unary_op(fp) + assert_(isscalar(result)) + assert_(result.__class__ is self.data[0, 0].__class__) - assert_(unary_op(fp, axis=0).__class__ is ndarray) - assert_(unary_op(fp, axis=1).__class__ is ndarray) + assert_(unary_op(fp, axis=0).__class__ is ndarray) + assert_(unary_op(fp, axis=1).__class__ is ndarray) for binary_op in [add, subtract, multiply]: assert_(binary_op(fp, self.data).__class__ is ndarray) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 977378cfa..2b585f4dc 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -29,7 +29,7 @@ from numpy.testing import ( TestCase, run_module_suite, assert_, assert_raises, assert_equal, assert_almost_equal, assert_array_equal, assert_array_almost_equal, assert_allclose, IS_PYPY, HAS_REFCOUNT, - assert_array_less, runstring, dec, SkipTest, temppath + assert_array_less, runstring, dec, SkipTest, temppath, suppress_warnings ) # Need to test an object that does not fully implement math interface @@ -826,8 +826,8 @@ class TestStructured(TestCase): # This comparison invokes deprecated behaviour, and will probably # start raising an error eventually. What we really care about in this # test is just that it doesn't return True. - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) + with suppress_warnings() as sup: + sup.filter(FutureWarning, "elementwise == comparison failed") assert_equal(x == y, False) x = np.zeros((1,), dtype=[('a', ('f4', (2, 1))), ('b', 'i1')]) @@ -835,8 +835,8 @@ class TestStructured(TestCase): # This comparison invokes deprecated behaviour, and will probably # start raising an error eventually. What we really care about in this # test is just that it doesn't return True. - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) + with suppress_warnings() as sup: + sup.filter(FutureWarning, "elementwise == comparison failed") assert_equal(x == y, False) # Check that structured arrays that are different only in @@ -3818,11 +3818,11 @@ class TestIO(object): def fail(*args, **kwargs): raise io.IOError('Can not tell or seek') - f = io.open(self.filename, 'rb', buffering=0) - f.seek = fail - f.tell = fail - y = np.fromfile(self.filename, dtype=self.dtype) - assert_array_equal(y, self.x.flat) + with io.open(self.filename, 'rb', buffering=0) as f: + f.seek = fail + f.tell = fail + y = np.fromfile(self.filename, dtype=self.dtype) + assert_array_equal(y, self.x.flat) def test_largish_file(self): # check the fallocate path on files > 16MB @@ -6039,11 +6039,11 @@ class TestNewBufferProtocol(object): class TestArrayAttributeDeletion(object): def test_multiarray_writable_attributes_deletion(self): - """ticket #2046, should not seqfault, raise AttributeError""" + # ticket #2046, should not seqfault, raise AttributeError a = np.ones(2) attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat'] - with warnings.catch_warnings(): - warnings.simplefilter('ignore') + with suppress_warnings() as sup: + sup.filter(DeprecationWarning, "Assigning the 'data' attribute") for s in attr: assert_raises(AttributeError, delattr, a, s) diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index f83d81624..3b5aaa28d 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -1,7 +1,6 @@ from __future__ import division, absolute_import, print_function import sys -import warnings import numpy as np from numpy import array, arange, nditer, all @@ -9,7 +8,7 @@ from numpy.compat import asbytes, sixu from numpy.core.multiarray_tests import test_nditer_too_large from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_array_equal, - assert_raises, dec, HAS_REFCOUNT + assert_raises, dec, HAS_REFCOUNT, suppress_warnings ) @@ -1624,8 +1623,8 @@ def test_iter_buffered_cast_byteswapped(): assert_equal(a, 2*np.arange(10, dtype='f4')) - try: - warnings.simplefilter("ignore", np.ComplexWarning) + with suppress_warnings() as sup: + sup.filter(np.ComplexWarning) a = np.arange(10, dtype='f8').newbyteorder().byteswap() i = nditer(a, ['buffered', 'external_loop'], @@ -1637,8 +1636,6 @@ def test_iter_buffered_cast_byteswapped(): v[...] *= 2 assert_equal(a, 2*np.arange(10, dtype='f8')) - finally: - warnings.simplefilter("default", np.ComplexWarning) def test_iter_buffered_cast_byteswapped_complex(): # Test that buffering can handle a cast which requires swap->cast->copy diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 990d13a3e..c31e9e07c 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -12,7 +12,7 @@ from numpy.random import rand, randint, randn from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_raises, assert_raises_regex, assert_array_equal, assert_almost_equal, - assert_array_almost_equal, dec, HAS_REFCOUNT + assert_array_almost_equal, dec, HAS_REFCOUNT, suppress_warnings ) @@ -1976,27 +1976,26 @@ class TestCreationFuncs(TestCase): fill_kwarg = {} if fill_value is not None: fill_kwarg = {'fill_value': fill_value} - with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - for size, ndims, order, dtype in itertools.product(*par): - shape = ndims * [size] - - # do not fill void type - if fill_kwarg and dtype.str.startswith('|V'): - continue - - arr = func(shape, order=order, dtype=dtype, - **fill_kwarg) - - assert_equal(arr.dtype, dtype) - assert_(getattr(arr.flags, self.orders[order])) - - if fill_value is not None: - if dtype.str.startswith('|S'): - val = str(fill_value) - else: - val = fill_value - assert_equal(arr, dtype.type(val)) + + for size, ndims, order, dtype in itertools.product(*par): + shape = ndims * [size] + + # do not fill void type + if fill_kwarg and dtype.str.startswith('|V'): + continue + + arr = func(shape, order=order, dtype=dtype, + **fill_kwarg) + + assert_equal(arr.dtype, dtype) + assert_(getattr(arr.flags, self.orders[order])) + + if fill_value is not None: + if dtype.str.startswith('|S'): + val = str(fill_value) + else: + val = fill_value + assert_equal(arr, dtype.type(val)) def test_zeros(self): self.check_function(np.zeros) diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 022438ab1..5966ff688 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -15,7 +15,7 @@ import numpy as np from numpy.testing import ( run_module_suite, TestCase, assert_, assert_equal, assert_almost_equal, assert_array_equal, assert_array_almost_equal, - assert_raises, assert_warns, dec + assert_raises, assert_warns, dec, suppress_warnings ) from numpy.testing.utils import _assert_valid_refcount, HAS_REFCOUNT from numpy.compat import asbytes, asunicode, asbytes_nested, long, sixu @@ -138,8 +138,8 @@ class TestRegression(TestCase): self.assertTrue(a[0] != 'auto') b = np.linspace(0, 10, 11) # This should return true for now, but will eventually raise an error: - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) + with suppress_warnings() as sup: + sup.filter(FutureWarning) self.assertTrue(b != 'auto') self.assertTrue(b[0] != 'auto') @@ -811,9 +811,10 @@ class TestRegression(TestCase): # This might seem odd as compared to the value error below. This # is due to the fact that the new code always uses "nonzero" logic # and the boolean special case is not taken. - with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - warnings.simplefilter('ignore', np.VisibleDeprecationWarning) + with suppress_warnings() as sup: + sup.filter(DeprecationWarning) + sup.filter(FutureWarning) + sup.filter(np.VisibleDeprecationWarning) self.assertRaises(IndexError, ia, x, s, np.zeros(9, dtype=float)) self.assertRaises(IndexError, ia, x, s, np.zeros(11, dtype=float)) # Old special case (different code path): @@ -1518,19 +1519,17 @@ class TestRegression(TestCase): dtypes = [x for x in np.typeDict.values() if (issubclass(x, np.number) and not issubclass(x, np.timedelta64))] - a = np.array([], dtypes[0]) + a = np.array([], np.bool_) # not x[0] because it is unordered failures = [] - # ignore complex warnings - with warnings.catch_warnings(): - warnings.simplefilter('ignore', np.ComplexWarning) - for x in dtypes: - b = a.astype(x) - for y in dtypes: - c = a.astype(y) - try: - np.dot(b, c) - except TypeError: - failures.append((x, y)) + + for x in dtypes: + b = a.astype(x) + for y in dtypes: + c = a.astype(y) + try: + np.dot(b, c) + except TypeError: + failures.append((x, y)) if failures: raise AssertionError("Failures: %r" % failures) @@ -1621,8 +1620,8 @@ class TestRegression(TestCase): for tp in [np.csingle, np.cdouble, np.clongdouble]: x = tp(1+2j) assert_warns(np.ComplexWarning, float, x) - with warnings.catch_warnings(): - warnings.simplefilter('ignore') + with suppress_warnings() as sup: + sup.filter(np.ComplexWarning) assert_equal(float(x), float(x.real)) def test_complex_scalar_complex_cast(self): diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index 0ae013f12..87b050bb9 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -2,14 +2,14 @@ from __future__ import division, absolute_import, print_function import sys import itertools -import warnings import operator import numpy as np from numpy.testing.utils import _gen_alignment_data from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_raises, - assert_almost_equal, assert_allclose, assert_array_equal, IS_PYPY + assert_almost_equal, assert_allclose, assert_array_equal, IS_PYPY, + suppress_warnings ) types = [np.bool_, np.byte, np.ubyte, np.short, np.ushort, np.intc, np.uintc, @@ -240,9 +240,8 @@ class TestModulus(TestCase): assert_(rem >= -b, 'dt: %s' % dt) # Check nans, inf - with warnings.catch_warnings(): - warnings.simplefilter('always') - warnings.simplefilter('ignore', RuntimeWarning) + with suppress_warnings() as sup: + sup.filter(RuntimeWarning, "invalid value encountered in remainder") for dt in np.typecodes['Float']: fone = np.array(1.0, dtype=dt) fzer = np.array(0.0, dtype=dt) diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index db5621279..a800de918 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -11,7 +11,7 @@ import numpy as np from numpy.testing import ( TestCase, run_module_suite, assert_, assert_equal, assert_raises, assert_array_equal, assert_almost_equal, assert_array_almost_equal, - dec, assert_allclose, assert_no_warnings + dec, assert_allclose, assert_no_warnings, suppress_warnings ) @@ -300,9 +300,8 @@ class TestRemainder(TestCase): assert_(rem >= -b, 'dt: %s' % dt) # Check nans, inf - with warnings.catch_warnings(): - warnings.simplefilter('always') - warnings.simplefilter('ignore', RuntimeWarning) + with suppress_warnings() as sup: + sup.filter(RuntimeWarning, "invalid value encountered in remainder") for dt in np.typecodes['Float']: fone = np.array(1.0, dtype=dt) fzer = np.array(0.0, dtype=dt) |