diff options
Diffstat (limited to 'numpy/testing/_private/utils.py')
-rw-r--r-- | numpy/testing/_private/utils.py | 128 |
1 files changed, 52 insertions, 76 deletions
diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 53930b78c..f2fd9d2ae 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -16,9 +16,10 @@ from tempfile import mkdtemp, mkstemp from unittest.case import SkipTest from warnings import WarningMessage import pprint +import sysconfig import numpy as np -from numpy.core import( +from numpy.core import ( intp, float32, empty, arange, array_repr, ndarray, isnat, array) import numpy.linalg.lapack_lite @@ -29,14 +30,14 @@ __all__ = [ 'assert_array_equal', 'assert_array_less', 'assert_string_equal', 'assert_array_almost_equal', 'assert_raises', 'build_err_msg', 'decorate_methods', 'jiffies', 'memusage', 'print_assert_equal', - 'raises', 'rundocs', 'runstring', 'verbose', 'measure', + 'rundocs', 'runstring', 'verbose', 'measure', 'assert_', 'assert_array_almost_equal_nulp', 'assert_raises_regex', 'assert_array_max_ulp', 'assert_warns', 'assert_no_warnings', 'assert_allclose', 'IgnoreException', 'clear_and_catch_warnings', 'SkipTest', 'KnownFailureException', 'temppath', 'tempdir', 'IS_PYPY', - 'HAS_REFCOUNT', 'suppress_warnings', 'assert_array_compare', + 'HAS_REFCOUNT', "IS_WASM", 'suppress_warnings', 'assert_array_compare', 'assert_no_gc_cycles', 'break_cycles', 'HAS_LAPACK64', 'IS_PYSTON', - '_OLD_PROMOTION' + '_OLD_PROMOTION', 'IS_MUSL', '_SUPPORTS_SVE' ] @@ -48,6 +49,7 @@ class KnownFailureException(Exception): KnownFailureTest = KnownFailureException # backwards compat verbose = 0 +IS_WASM = platform.machine() in ["wasm32", "wasm64"] IS_PYPY = sys.implementation.name == 'pypy' IS_PYSTON = hasattr(sys, "pyston_version_info") HAS_REFCOUNT = getattr(sys, 'getrefcount', None) is not None and not IS_PYSTON @@ -55,27 +57,18 @@ HAS_LAPACK64 = numpy.linalg.lapack_lite._ilp64 _OLD_PROMOTION = lambda: np._get_promotion_state() == 'legacy' - -def import_nose(): - """ Import nose only when needed. - """ - nose_is_good = True - minimum_nose_version = (1, 0, 0) - try: - import nose - except ImportError: - nose_is_good = False - else: - if nose.__versioninfo__ < minimum_nose_version: - nose_is_good = False - - if not nose_is_good: - msg = ('Need nose >= %d.%d.%d for tests - see ' - 'https://nose.readthedocs.io' % - minimum_nose_version) - raise ImportError(msg) - - return nose +IS_MUSL = False +try: + from packaging.tags import sys_tags + _tags = list(sys_tags()) + if 'musllinux' in _tags[0].platform: + IS_MUSL = True +except ImportError: + # fallback to sysconfig (might be flaky) + # value could be None. + v = sysconfig.get_config_var('HOST_GNU_TYPE') or '' + if 'musl' in v: + IS_MUSL = True def assert_(val, msg=''): @@ -477,7 +470,7 @@ def print_assert_equal(test_string, actual, desired): @np._no_nep50_warning() -def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): +def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True): """ Raises an AssertionError if two items are not equal up to desired precision. @@ -604,7 +597,8 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True): @np._no_nep50_warning() -def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True): +def assert_approx_equal(actual, desired, significant=7, err_msg='', + verbose=True): """ Raises an AssertionError if two items are not equal up to significant digits. @@ -708,7 +702,8 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='', precision=6, equal_nan=True, equal_inf=True, *, strict=False): __tracebackhide__ = True # Hide traceback for py.test - from numpy.core import array, array2string, isnan, inf, bool_, errstate, all, max, object_ + from numpy.core import (array2string, isnan, inf, bool_, errstate, + all, max, object_) x = np.asanyarray(x) y = np.asanyarray(y) @@ -834,10 +829,10 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='', max_abs_error = max(error) if getattr(error, 'dtype', object_) == object_: remarks.append('Max absolute difference: ' - + str(max_abs_error)) + + str(max_abs_error)) else: remarks.append('Max absolute difference: ' - + array2string(max_abs_error)) + + array2string(max_abs_error)) # note: this definition of relative error matches that one # used by assert_allclose (found in np.isclose) @@ -849,10 +844,10 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='', max_rel_error = max(error[nonzero] / abs(y[nonzero])) if getattr(error, 'dtype', object_) == object_: remarks.append('Max relative difference: ' - + str(max_rel_error)) + + str(max_rel_error)) else: remarks.append('Max relative difference: ' - + array2string(max_rel_error)) + + array2string(max_rel_error)) err_msg += '\n' + '\n'.join(remarks) msg = build_err_msg([ox, oy], err_msg, @@ -898,6 +893,8 @@ def assert_array_equal(x, y, err_msg='', verbose=True, *, strict=False): type of the array_like objects does not match. The special handling for scalars mentioned in the Notes section is disabled. + .. versionadded:: 1.24.0 + Raises ------ AssertionError @@ -1063,13 +1060,13 @@ def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): """ __tracebackhide__ = True # Hide traceback for py.test - from numpy.core import number, float_, result_type, array + from numpy.core import number, float_, result_type from numpy.core.numerictypes import issubdtype from numpy.core.fromnumeric import any as npany def compare(x, y): try: - if npany(gisinf(x)) or npany( gisinf(y)): + if npany(gisinf(x)) or npany(gisinf(y)): xinfid = gisinf(x) yinfid = gisinf(y) if not (xinfid == yinfid).all(): @@ -1111,8 +1108,6 @@ def assert_array_less(x, y, err_msg='', verbose=True): compared, no assertion is raised if both objects have NaNs in the same positions. - - Parameters ---------- x : array_like @@ -1134,8 +1129,6 @@ def assert_array_less(x, y, err_msg='', verbose=True): assert_array_equal: tests objects for equality assert_array_almost_equal: test objects for equality up to precision - - Examples -------- >>> np.testing.assert_array_less([1.0, 1.0, np.nan], [1.1, 2.0, np.nan]) @@ -1302,42 +1295,21 @@ def rundocs(filename=None, raise_on_error=True): raise AssertionError("Some doctests failed:\n%s" % "\n".join(msg)) -def raises(*args): - """Decorator to check for raised exceptions. - - The decorated test function must raise one of the passed exceptions to - pass. If you want to test many assertions about exceptions in a single - test, you may want to use `assert_raises` instead. - - .. warning:: - This decorator is nose specific, do not use it if you are using a - different test framework. - - Parameters - ---------- - args : exceptions - The test passes if any of the passed exceptions is raised. - - Raises - ------ - AssertionError - - Examples - -------- - - Usage:: - - @raises(TypeError, ValueError) - def test_raises_type_error(): - raise TypeError("This test passes") +def check_support_sve(): + """ + gh-22982 + """ + + import subprocess + cmd = 'lscpu' + try: + return "sve" in (subprocess.Popen(cmd, stdout=subprocess.PIPE, + shell=True).communicate()[0]).decode('utf-8') + except OSError: + return False - @raises(Exception) - def test_that_fails_by_passing(): - pass - """ - nose = import_nose() - return nose.tools.raises(*args) +_SUPPORTS_SVE = check_support_sve() # # assert_raises and assert_raises_regex are taken from unittest. @@ -1349,8 +1321,10 @@ class _Dummy(unittest.TestCase): def nop(self): pass + _d = _Dummy('nop') + def assert_raises(*args, **kwargs): """ assert_raises(exception_class, callable, *args, **kwargs) @@ -1377,7 +1351,7 @@ def assert_raises(*args, **kwargs): """ __tracebackhide__ = True # Hide traceback for py.test - return _d.assertRaises(*args,**kwargs) + return _d.assertRaises(*args, **kwargs) def assert_raises_regex(exception_class, expected_regexp, *args, **kwargs): @@ -1528,7 +1502,7 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0, equal_nan=True, Given two array_like objects, check that their shapes and all elements are equal (but see the Notes for the special handling of a scalar). An - exception is raised if the shapes mismatch or any values conflict. In + exception is raised if the shapes mismatch or any values conflict. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions. @@ -1625,7 +1599,7 @@ def assert_array_almost_equal_nulp(x, y, nulp=1): ----- An assertion is raised if the following condition is not met:: - abs(x - y) <= nulps * spacing(maximum(abs(x), abs(y))) + abs(x - y) <= nulp * spacing(maximum(abs(x), abs(y))) Examples -------- @@ -2450,6 +2424,7 @@ def assert_no_gc_cycles(*args, **kwargs): with _assert_no_gc_cycles_context(name=func.__name__): func(*args, **kwargs) + def break_cycles(): """ Break reference cycles by calling gc.collect @@ -2582,7 +2557,7 @@ def _no_tracing(func): def _get_glibc_version(): try: ver = os.confstr('CS_GNU_LIBC_VERSION').rsplit(' ')[1] - except Exception as inst: + except Exception: ver = '0.0' return ver @@ -2590,3 +2565,4 @@ def _get_glibc_version(): _glibcver = _get_glibc_version() _glibc_older_than = lambda x: (_glibcver != '0.0' and _glibcver < x) + |