annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/numpy/conftest.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 """
jpayne@69 2 Pytest configuration and fixtures for the Numpy test suite.
jpayne@69 3 """
jpayne@69 4 import os
jpayne@69 5 import tempfile
jpayne@69 6
jpayne@69 7 import hypothesis
jpayne@69 8 import pytest
jpayne@69 9 import numpy
jpayne@69 10
jpayne@69 11 from numpy.core._multiarray_tests import get_fpu_mode
jpayne@69 12
jpayne@69 13
jpayne@69 14 _old_fpu_mode = None
jpayne@69 15 _collect_results = {}
jpayne@69 16
jpayne@69 17 # Use a known and persistent tmpdir for hypothesis' caches, which
jpayne@69 18 # can be automatically cleared by the OS or user.
jpayne@69 19 hypothesis.configuration.set_hypothesis_home_dir(
jpayne@69 20 os.path.join(tempfile.gettempdir(), ".hypothesis")
jpayne@69 21 )
jpayne@69 22
jpayne@69 23 # We register two custom profiles for Numpy - for details see
jpayne@69 24 # https://hypothesis.readthedocs.io/en/latest/settings.html
jpayne@69 25 # The first is designed for our own CI runs; the latter also
jpayne@69 26 # forces determinism and is designed for use via np.test()
jpayne@69 27 hypothesis.settings.register_profile(
jpayne@69 28 name="numpy-profile", deadline=None, print_blob=True,
jpayne@69 29 )
jpayne@69 30 hypothesis.settings.register_profile(
jpayne@69 31 name="np.test() profile",
jpayne@69 32 deadline=None, print_blob=True, database=None, derandomize=True,
jpayne@69 33 suppress_health_check=list(hypothesis.HealthCheck),
jpayne@69 34 )
jpayne@69 35 # Note that the default profile is chosen based on the presence
jpayne@69 36 # of pytest.ini, but can be overridden by passing the
jpayne@69 37 # --hypothesis-profile=NAME argument to pytest.
jpayne@69 38 _pytest_ini = os.path.join(os.path.dirname(__file__), "..", "pytest.ini")
jpayne@69 39 hypothesis.settings.load_profile(
jpayne@69 40 "numpy-profile" if os.path.isfile(_pytest_ini) else "np.test() profile"
jpayne@69 41 )
jpayne@69 42
jpayne@69 43
jpayne@69 44 def pytest_configure(config):
jpayne@69 45 config.addinivalue_line("markers",
jpayne@69 46 "valgrind_error: Tests that are known to error under valgrind.")
jpayne@69 47 config.addinivalue_line("markers",
jpayne@69 48 "leaks_references: Tests that are known to leak references.")
jpayne@69 49 config.addinivalue_line("markers",
jpayne@69 50 "slow: Tests that are very slow.")
jpayne@69 51 config.addinivalue_line("markers",
jpayne@69 52 "slow_pypy: Tests that are very slow on pypy.")
jpayne@69 53
jpayne@69 54
jpayne@69 55 def pytest_addoption(parser):
jpayne@69 56 parser.addoption("--available-memory", action="store", default=None,
jpayne@69 57 help=("Set amount of memory available for running the "
jpayne@69 58 "test suite. This can result to tests requiring "
jpayne@69 59 "especially large amounts of memory to be skipped. "
jpayne@69 60 "Equivalent to setting environment variable "
jpayne@69 61 "NPY_AVAILABLE_MEM. Default: determined"
jpayne@69 62 "automatically."))
jpayne@69 63
jpayne@69 64
jpayne@69 65 def pytest_sessionstart(session):
jpayne@69 66 available_mem = session.config.getoption('available_memory')
jpayne@69 67 if available_mem is not None:
jpayne@69 68 os.environ['NPY_AVAILABLE_MEM'] = available_mem
jpayne@69 69
jpayne@69 70
jpayne@69 71 #FIXME when yield tests are gone.
jpayne@69 72 @pytest.hookimpl()
jpayne@69 73 def pytest_itemcollected(item):
jpayne@69 74 """
jpayne@69 75 Check FPU precision mode was not changed during test collection.
jpayne@69 76
jpayne@69 77 The clumsy way we do it here is mainly necessary because numpy
jpayne@69 78 still uses yield tests, which can execute code at test collection
jpayne@69 79 time.
jpayne@69 80 """
jpayne@69 81 global _old_fpu_mode
jpayne@69 82
jpayne@69 83 mode = get_fpu_mode()
jpayne@69 84
jpayne@69 85 if _old_fpu_mode is None:
jpayne@69 86 _old_fpu_mode = mode
jpayne@69 87 elif mode != _old_fpu_mode:
jpayne@69 88 _collect_results[item] = (_old_fpu_mode, mode)
jpayne@69 89 _old_fpu_mode = mode
jpayne@69 90
jpayne@69 91
jpayne@69 92 @pytest.fixture(scope="function", autouse=True)
jpayne@69 93 def check_fpu_mode(request):
jpayne@69 94 """
jpayne@69 95 Check FPU precision mode was not changed during the test.
jpayne@69 96 """
jpayne@69 97 old_mode = get_fpu_mode()
jpayne@69 98 yield
jpayne@69 99 new_mode = get_fpu_mode()
jpayne@69 100
jpayne@69 101 if old_mode != new_mode:
jpayne@69 102 raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
jpayne@69 103 " during the test".format(old_mode, new_mode))
jpayne@69 104
jpayne@69 105 collect_result = _collect_results.get(request.node)
jpayne@69 106 if collect_result is not None:
jpayne@69 107 old_mode, new_mode = collect_result
jpayne@69 108 raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}"
jpayne@69 109 " when collecting the test".format(old_mode,
jpayne@69 110 new_mode))
jpayne@69 111
jpayne@69 112
jpayne@69 113 @pytest.fixture(autouse=True)
jpayne@69 114 def add_np(doctest_namespace):
jpayne@69 115 doctest_namespace['np'] = numpy
jpayne@69 116
jpayne@69 117 @pytest.fixture(autouse=True)
jpayne@69 118 def env_setup(monkeypatch):
jpayne@69 119 monkeypatch.setenv('PYTHONHASHSEED', '0')
jpayne@69 120
jpayne@69 121
jpayne@69 122 @pytest.fixture(params=[True, False])
jpayne@69 123 def weak_promotion(request):
jpayne@69 124 """
jpayne@69 125 Fixture to ensure "legacy" promotion state or change it to use the new
jpayne@69 126 weak promotion (plus warning). `old_promotion` should be used as a
jpayne@69 127 parameter in the function.
jpayne@69 128 """
jpayne@69 129 state = numpy._get_promotion_state()
jpayne@69 130 if request.param:
jpayne@69 131 numpy._set_promotion_state("weak_and_warn")
jpayne@69 132 else:
jpayne@69 133 numpy._set_promotion_state("legacy")
jpayne@69 134
jpayne@69 135 yield request.param
jpayne@69 136 numpy._set_promotion_state(state)