annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/scipy/conftest.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 # Pytest customization
jpayne@68 2 import os
jpayne@68 3 import pytest
jpayne@68 4 import warnings
jpayne@68 5
jpayne@68 6 import numpy as np
jpayne@68 7 import numpy.testing as npt
jpayne@68 8 from scipy._lib._fpumode import get_fpu_mode
jpayne@68 9 from scipy._lib._testutils import FPUModeChangeWarning
jpayne@68 10 from scipy._lib import _pep440
jpayne@68 11
jpayne@68 12
jpayne@68 13 def pytest_configure(config):
jpayne@68 14 config.addinivalue_line("markers",
jpayne@68 15 "slow: Tests that are very slow.")
jpayne@68 16 config.addinivalue_line("markers",
jpayne@68 17 "xslow: mark test as extremely slow (not run unless explicitly requested)")
jpayne@68 18 config.addinivalue_line("markers",
jpayne@68 19 "xfail_on_32bit: mark test as failing on 32-bit platforms")
jpayne@68 20 try:
jpayne@68 21 import pytest_timeout # noqa:F401
jpayne@68 22 except Exception:
jpayne@68 23 config.addinivalue_line(
jpayne@68 24 "markers", 'timeout: mark a test for a non-default timeout')
jpayne@68 25
jpayne@68 26
jpayne@68 27 def _get_mark(item, name):
jpayne@68 28 if _pep440.parse(pytest.__version__) >= _pep440.Version("3.6.0"):
jpayne@68 29 mark = item.get_closest_marker(name)
jpayne@68 30 else:
jpayne@68 31 mark = item.get_marker(name)
jpayne@68 32 return mark
jpayne@68 33
jpayne@68 34
jpayne@68 35 def pytest_runtest_setup(item):
jpayne@68 36 mark = _get_mark(item, "xslow")
jpayne@68 37 if mark is not None:
jpayne@68 38 try:
jpayne@68 39 v = int(os.environ.get('SCIPY_XSLOW', '0'))
jpayne@68 40 except ValueError:
jpayne@68 41 v = False
jpayne@68 42 if not v:
jpayne@68 43 pytest.skip("very slow test; set environment variable SCIPY_XSLOW=1 to run it")
jpayne@68 44 mark = _get_mark(item, 'xfail_on_32bit')
jpayne@68 45 if mark is not None and np.intp(0).itemsize < 8:
jpayne@68 46 pytest.xfail('Fails on our 32-bit test platform(s): %s' % (mark.args[0],))
jpayne@68 47
jpayne@68 48 # Older versions of threadpoolctl have an issue that may lead to this
jpayne@68 49 # warning being emitted, see gh-14441
jpayne@68 50 with npt.suppress_warnings() as sup:
jpayne@68 51 sup.filter(pytest.PytestUnraisableExceptionWarning)
jpayne@68 52
jpayne@68 53 try:
jpayne@68 54 from threadpoolctl import threadpool_limits
jpayne@68 55
jpayne@68 56 HAS_THREADPOOLCTL = True
jpayne@68 57 except Exception: # observed in gh-14441: (ImportError, AttributeError)
jpayne@68 58 # Optional dependency only. All exceptions are caught, for robustness
jpayne@68 59 HAS_THREADPOOLCTL = False
jpayne@68 60
jpayne@68 61 if HAS_THREADPOOLCTL:
jpayne@68 62 # Set the number of openmp threads based on the number of workers
jpayne@68 63 # xdist is using to prevent oversubscription. Simplified version of what
jpayne@68 64 # sklearn does (it can rely on threadpoolctl and its builtin OpenMP helper
jpayne@68 65 # functions)
jpayne@68 66 try:
jpayne@68 67 xdist_worker_count = int(os.environ['PYTEST_XDIST_WORKER_COUNT'])
jpayne@68 68 except KeyError:
jpayne@68 69 # raises when pytest-xdist is not installed
jpayne@68 70 return
jpayne@68 71
jpayne@68 72 if not os.getenv('OMP_NUM_THREADS'):
jpayne@68 73 max_openmp_threads = os.cpu_count() // 2 # use nr of physical cores
jpayne@68 74 threads_per_worker = max(max_openmp_threads // xdist_worker_count, 1)
jpayne@68 75 try:
jpayne@68 76 threadpool_limits(threads_per_worker, user_api='blas')
jpayne@68 77 except Exception:
jpayne@68 78 # May raise AttributeError for older versions of OpenBLAS.
jpayne@68 79 # Catch any error for robustness.
jpayne@68 80 return
jpayne@68 81
jpayne@68 82
jpayne@68 83 @pytest.fixture(scope="function", autouse=True)
jpayne@68 84 def check_fpu_mode(request):
jpayne@68 85 """
jpayne@68 86 Check FPU mode was not changed during the test.
jpayne@68 87 """
jpayne@68 88 old_mode = get_fpu_mode()
jpayne@68 89 yield
jpayne@68 90 new_mode = get_fpu_mode()
jpayne@68 91
jpayne@68 92 if old_mode != new_mode:
jpayne@68 93 warnings.warn("FPU mode changed from {0:#x} to {1:#x} during "
jpayne@68 94 "the test".format(old_mode, new_mode),
jpayne@68 95 category=FPUModeChangeWarning, stacklevel=0)