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