conftest.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. """
  2. Pytest configuration and fixtures for the Numpy test suite.
  3. """
  4. import os
  5. import sys
  6. import tempfile
  7. import warnings
  8. from contextlib import contextmanager
  9. from pathlib import Path
  10. import hypothesis
  11. import pytest
  12. import numpy
  13. from numpy._core._multiarray_tests import get_fpu_mode
  14. from numpy.testing._private.utils import NOGIL_BUILD
  15. try:
  16. from scipy_doctest.conftest import dt_config
  17. HAVE_SCPDT = True
  18. except ModuleNotFoundError:
  19. HAVE_SCPDT = False
  20. try:
  21. import pytest_run_parallel # noqa: F401
  22. PARALLEL_RUN_AVALIABLE = True
  23. except ModuleNotFoundError:
  24. PARALLEL_RUN_AVALIABLE = False
  25. _old_fpu_mode = None
  26. _collect_results = {}
  27. # Use a known and persistent tmpdir for hypothesis' caches, which
  28. # can be automatically cleared by the OS or user.
  29. hypothesis.configuration.set_hypothesis_home_dir(
  30. os.path.join(tempfile.gettempdir(), ".hypothesis")
  31. )
  32. # We register two custom profiles for Numpy - for details see
  33. # https://hypothesis.readthedocs.io/en/latest/settings.html
  34. # The first is designed for our own CI runs; the latter also
  35. # forces determinism and is designed for use via np.test()
  36. hypothesis.settings.register_profile(
  37. name="numpy-profile", deadline=None, print_blob=True,
  38. )
  39. hypothesis.settings.register_profile(
  40. name="np.test() profile",
  41. deadline=None, print_blob=True, database=None, derandomize=True,
  42. suppress_health_check=list(hypothesis.HealthCheck),
  43. )
  44. # Note that the default profile is chosen based on the presence
  45. # of pytest.ini, but can be overridden by passing the
  46. # --hypothesis-profile=NAME argument to pytest.
  47. _pytest_ini = os.path.join(os.path.dirname(__file__), "..", "pytest.ini")
  48. hypothesis.settings.load_profile(
  49. "numpy-profile" if os.path.isfile(_pytest_ini) else "np.test() profile"
  50. )
  51. # The experimentalAPI is used in _umath_tests
  52. os.environ["NUMPY_EXPERIMENTAL_DTYPE_API"] = "1"
  53. def pytest_configure(config):
  54. config.addinivalue_line("markers",
  55. "valgrind_error: Tests that are known to error under valgrind.")
  56. config.addinivalue_line("markers",
  57. "leaks_references: Tests that are known to leak references.")
  58. config.addinivalue_line("markers",
  59. "slow: Tests that are very slow.")
  60. config.addinivalue_line("markers",
  61. "slow_pypy: Tests that are very slow on pypy.")
  62. if not PARALLEL_RUN_AVALIABLE:
  63. config.addinivalue_line("markers",
  64. "parallel_threads(n): run the given test function in parallel "
  65. "using `n` threads.",
  66. )
  67. config.addinivalue_line("markers",
  68. "iterations(n): run the given test function `n` times in each thread",
  69. )
  70. config.addinivalue_line("markers",
  71. "thread_unsafe: mark the test function as single-threaded",
  72. )
  73. def pytest_addoption(parser):
  74. parser.addoption("--available-memory", action="store", default=None,
  75. help=("Set amount of memory available for running the "
  76. "test suite. This can result to tests requiring "
  77. "especially large amounts of memory to be skipped. "
  78. "Equivalent to setting environment variable "
  79. "NPY_AVAILABLE_MEM. Default: determined"
  80. "automatically."))
  81. gil_enabled_at_start = True
  82. if NOGIL_BUILD:
  83. gil_enabled_at_start = sys._is_gil_enabled()
  84. def pytest_sessionstart(session):
  85. available_mem = session.config.getoption('available_memory')
  86. if available_mem is not None:
  87. os.environ['NPY_AVAILABLE_MEM'] = available_mem
  88. def pytest_terminal_summary(terminalreporter, exitstatus, config):
  89. if NOGIL_BUILD and not gil_enabled_at_start and sys._is_gil_enabled():
  90. tr = terminalreporter
  91. tr.ensure_newline()
  92. tr.section("GIL re-enabled", sep="=", red=True, bold=True)
  93. tr.line("The GIL was re-enabled at runtime during the tests.")
  94. tr.line("This can happen with no test failures if the RuntimeWarning")
  95. tr.line("raised by Python when this happens is filtered by a test.")
  96. tr.line("")
  97. tr.line("Please ensure all new C modules declare support for running")
  98. tr.line("without the GIL. Any new tests that intentionally imports ")
  99. tr.line("code that re-enables the GIL should do so in a subprocess.")
  100. pytest.exit("GIL re-enabled during tests", returncode=1)
  101. # FIXME when yield tests are gone.
  102. @pytest.hookimpl(tryfirst=True)
  103. def pytest_itemcollected(item):
  104. """
  105. Check FPU precision mode was not changed during test collection.
  106. The clumsy way we do it here is mainly necessary because numpy
  107. still uses yield tests, which can execute code at test collection
  108. time.
  109. """
  110. global _old_fpu_mode
  111. mode = get_fpu_mode()
  112. if _old_fpu_mode is None:
  113. _old_fpu_mode = mode
  114. elif mode != _old_fpu_mode:
  115. _collect_results[item] = (_old_fpu_mode, mode)
  116. _old_fpu_mode = mode
  117. # mark f2py tests as thread unsafe
  118. if Path(item.fspath).parent == Path(__file__).parent / 'f2py' / 'tests':
  119. item.add_marker(pytest.mark.thread_unsafe(
  120. reason="f2py tests are thread-unsafe"))
  121. @pytest.fixture(scope="function", autouse=True)
  122. def check_fpu_mode(request):
  123. """
  124. Check FPU precision mode was not changed during the test.
  125. """
  126. old_mode = get_fpu_mode()
  127. yield
  128. new_mode = get_fpu_mode()
  129. if old_mode != new_mode:
  130. raise AssertionError(f"FPU precision mode changed from {old_mode:#x} to "
  131. f"{new_mode:#x} during the test")
  132. collect_result = _collect_results.get(request.node)
  133. if collect_result is not None:
  134. old_mode, new_mode = collect_result
  135. raise AssertionError(f"FPU precision mode changed from {old_mode:#x} to "
  136. f"{new_mode:#x} when collecting the test")
  137. @pytest.fixture(autouse=True)
  138. def add_np(doctest_namespace):
  139. doctest_namespace['np'] = numpy
  140. if HAVE_SCPDT:
  141. @contextmanager
  142. def warnings_errors_and_rng(test=None):
  143. """Filter out the wall of DeprecationWarnings.
  144. """
  145. msgs = ["The numpy.linalg.linalg",
  146. "The numpy.fft.helper",
  147. "dep_util",
  148. "pkg_resources",
  149. "numpy.core.umath",
  150. "msvccompiler",
  151. "Deprecated call",
  152. "numpy.core",
  153. "Importing from numpy.matlib",
  154. "This function is deprecated.", # random_integers
  155. "Data type alias 'a'", # numpy.rec.fromfile
  156. "Arrays of 2-dimensional vectors", # matlib.cross
  157. "NumPy warning suppression and assertion utilities are deprecated."
  158. ]
  159. msg = "|".join(msgs)
  160. msgs_r = [
  161. "invalid value encountered",
  162. "divide by zero encountered"
  163. ]
  164. msg_r = "|".join(msgs_r)
  165. with warnings.catch_warnings():
  166. warnings.filterwarnings(
  167. 'ignore', category=DeprecationWarning, message=msg
  168. )
  169. warnings.filterwarnings(
  170. 'ignore', category=RuntimeWarning, message=msg_r
  171. )
  172. yield
  173. # find and check doctests under this context manager
  174. dt_config.user_context_mgr = warnings_errors_and_rng
  175. # numpy specific tweaks from refguide-check
  176. dt_config.rndm_markers.add('#uninitialized')
  177. dt_config.rndm_markers.add('# uninitialized')
  178. # make the checker pick on mismatched dtypes
  179. dt_config.strict_check = True
  180. import doctest
  181. dt_config.optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
  182. # recognize the StringDType repr
  183. dt_config.check_namespace['StringDType'] = numpy.dtypes.StringDType
  184. # temporary skips
  185. dt_config.skiplist = {
  186. 'numpy.savez', # unclosed file
  187. 'numpy.matlib.savez',
  188. 'numpy.__array_namespace_info__',
  189. 'numpy.matlib.__array_namespace_info__',
  190. }
  191. # xfail problematic tutorials
  192. dt_config.pytest_extra_xfail = {
  193. 'how-to-verify-bug.rst': '',
  194. 'c-info.ufunc-tutorial.rst': '',
  195. 'basics.interoperability.rst': 'needs pandas',
  196. 'basics.dispatch.rst': 'errors out in /testing/overrides.py',
  197. 'basics.subclassing.rst': '.. testcode:: admonitions not understood',
  198. 'misc.rst': 'manipulates warnings',
  199. }
  200. # ignores are for things fail doctest collection (optionals etc)
  201. dt_config.pytest_extra_ignore = [
  202. 'numpy/distutils',
  203. 'numpy/_core/cversions.py',
  204. 'numpy/_pyinstaller',
  205. 'numpy/random/_examples',
  206. 'numpy/f2py/_backends/_distutils.py',
  207. ]