test_extending.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. import shutil
  3. import subprocess
  4. import sys
  5. import sysconfig
  6. import warnings
  7. from importlib.util import module_from_spec, spec_from_file_location
  8. import pytest
  9. import numpy as np
  10. from numpy.testing import IS_EDITABLE, IS_WASM
  11. try:
  12. import cffi
  13. except ImportError:
  14. cffi = None
  15. if sys.flags.optimize > 1:
  16. # no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1
  17. # cffi cannot succeed
  18. cffi = None
  19. try:
  20. with warnings.catch_warnings(record=True) as w:
  21. # numba issue gh-4733
  22. warnings.filterwarnings('always', '', DeprecationWarning)
  23. import numba
  24. except (ImportError, SystemError):
  25. # Certain numpy/numba versions trigger a SystemError due to a numba bug
  26. numba = None
  27. try:
  28. import cython
  29. from Cython.Compiler.Version import version as cython_version
  30. except ImportError:
  31. cython = None
  32. else:
  33. from numpy._utils import _pep440
  34. # Note: keep in sync with the one in pyproject.toml
  35. required_version = '3.0.6'
  36. if _pep440.parse(cython_version) < _pep440.Version(required_version):
  37. # too old or wrong cython, skip the test
  38. cython = None
  39. @pytest.mark.skipif(
  40. IS_EDITABLE,
  41. reason='Editable install cannot find .pxd headers'
  42. )
  43. @pytest.mark.skipif(
  44. sys.platform == "win32" and sys.maxsize < 2**32,
  45. reason="Failing in 32-bit Windows wheel build job, skip for now"
  46. )
  47. @pytest.mark.skipif(IS_WASM, reason="Can't start subprocess")
  48. @pytest.mark.skipif(cython is None, reason="requires cython")
  49. @pytest.mark.skipif(sysconfig.get_platform() == 'win-arm64',
  50. reason='Meson unable to find MSVC linker on win-arm64')
  51. @pytest.mark.slow
  52. @pytest.mark.thread_unsafe(
  53. reason="building cython code in a subprocess doesn't make sense to do in many "
  54. "threads and sometimes crashes"
  55. )
  56. def test_cython(tmp_path):
  57. import glob
  58. # build the examples in a temporary directory
  59. srcdir = os.path.join(os.path.dirname(__file__), '..')
  60. shutil.copytree(srcdir, tmp_path / 'random')
  61. build_dir = tmp_path / 'random' / '_examples' / 'cython'
  62. target_dir = build_dir / "build"
  63. os.makedirs(target_dir, exist_ok=True)
  64. # Ensure we use the correct Python interpreter even when `meson` is
  65. # installed in a different Python environment (see gh-24956)
  66. native_file = str(build_dir / 'interpreter-native-file.ini')
  67. with open(native_file, 'w') as f:
  68. f.write("[binaries]\n")
  69. f.write(f"python = '{sys.executable}'\n")
  70. f.write(f"python3 = '{sys.executable}'")
  71. if sys.platform == "win32":
  72. subprocess.check_call(["meson", "setup",
  73. "--buildtype=release",
  74. "--vsenv", "--native-file", native_file,
  75. str(build_dir)],
  76. cwd=target_dir,
  77. )
  78. else:
  79. subprocess.check_call(["meson", "setup",
  80. "--native-file", native_file, str(build_dir)],
  81. cwd=target_dir
  82. )
  83. subprocess.check_call(["meson", "compile", "-vv"], cwd=target_dir)
  84. # gh-16162: make sure numpy's __init__.pxd was used for cython
  85. # not really part of this test, but it is a convenient place to check
  86. g = glob.glob(str(target_dir / "*" / "extending.pyx.c"))
  87. with open(g[0]) as fid:
  88. txt_to_find = 'NumPy API declarations from "numpy/__init__'
  89. for line in fid:
  90. if txt_to_find in line:
  91. break
  92. else:
  93. assert False, f"Could not find '{txt_to_find}' in C file, wrong pxd used"
  94. # import without adding the directory to sys.path
  95. suffix = sysconfig.get_config_var('EXT_SUFFIX')
  96. def load(modname):
  97. so = (target_dir / modname).with_suffix(suffix)
  98. spec = spec_from_file_location(modname, so)
  99. mod = module_from_spec(spec)
  100. spec.loader.exec_module(mod)
  101. return mod
  102. # test that the module can be imported
  103. load("extending")
  104. load("extending_cpp")
  105. # actually test the cython c-extension
  106. extending_distributions = load("extending_distributions")
  107. from numpy.random import PCG64
  108. values = extending_distributions.uniforms_ex(PCG64(0), 10, 'd')
  109. assert values.shape == (10,)
  110. assert values.dtype == np.float64
  111. @pytest.mark.skipif(numba is None or cffi is None,
  112. reason="requires numba and cffi")
  113. def test_numba():
  114. from numpy.random._examples.numba import extending # noqa: F401
  115. @pytest.mark.skipif(cffi is None, reason="requires cffi")
  116. def test_cffi():
  117. from numpy.random._examples.cffi import extending # noqa: F401