test_extending.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import pytest
  3. import shutil
  4. import subprocess
  5. import sys
  6. import warnings
  7. import numpy as np
  8. from numpy.distutils.misc_util import exec_mod_from_location
  9. from numpy.testing import IS_WASM
  10. try:
  11. import cffi
  12. except ImportError:
  13. cffi = None
  14. if sys.flags.optimize > 1:
  15. # no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1
  16. # cffi cannot succeed
  17. cffi = None
  18. try:
  19. with warnings.catch_warnings(record=True) as w:
  20. # numba issue gh-4733
  21. warnings.filterwarnings('always', '', DeprecationWarning)
  22. import numba
  23. except (ImportError, SystemError):
  24. # Certain numpy/numba versions trigger a SystemError due to a numba bug
  25. numba = None
  26. try:
  27. import cython
  28. from Cython.Compiler.Version import version as cython_version
  29. except ImportError:
  30. cython = None
  31. else:
  32. from numpy.compat import _pep440
  33. # Cython 0.29.30 is required for Python 3.11 and there are
  34. # other fixes in the 0.29 series that are needed even for earlier
  35. # Python versions.
  36. # Note: keep in sync with the one in pyproject.toml
  37. required_version = '0.29.30'
  38. if _pep440.parse(cython_version) < _pep440.Version(required_version):
  39. # too old or wrong cython, skip the test
  40. cython = None
  41. @pytest.mark.skipif(IS_WASM, reason="Can't start subprocess")
  42. @pytest.mark.skipif(cython is None, reason="requires cython")
  43. @pytest.mark.slow
  44. def test_cython(tmp_path):
  45. srcdir = os.path.join(os.path.dirname(__file__), '..')
  46. shutil.copytree(srcdir, tmp_path / 'random')
  47. # build the examples and "install" them into a temporary directory
  48. build_dir = tmp_path / 'random' / '_examples' / 'cython'
  49. subprocess.check_call([sys.executable, 'setup.py', 'build', 'install',
  50. '--prefix', str(tmp_path / 'installdir'),
  51. '--single-version-externally-managed',
  52. '--record', str(tmp_path/ 'tmp_install_log.txt'),
  53. ],
  54. cwd=str(build_dir),
  55. )
  56. # gh-16162: make sure numpy's __init__.pxd was used for cython
  57. # not really part of this test, but it is a convenient place to check
  58. with open(build_dir / 'extending.c') as fid:
  59. txt_to_find = 'NumPy API declarations from "numpy/__init__.pxd"'
  60. for i, line in enumerate(fid):
  61. if txt_to_find in line:
  62. break
  63. else:
  64. assert False, ("Could not find '{}' in C file, "
  65. "wrong pxd used".format(txt_to_find))
  66. # get the path to the so's
  67. so1 = so2 = None
  68. with open(tmp_path /'tmp_install_log.txt') as fid:
  69. for line in fid:
  70. if 'extending.' in line:
  71. so1 = line.strip()
  72. if 'extending_distributions' in line:
  73. so2 = line.strip()
  74. assert so1 is not None
  75. assert so2 is not None
  76. # import the so's without adding the directory to sys.path
  77. exec_mod_from_location('extending', so1)
  78. extending_distributions = exec_mod_from_location(
  79. 'extending_distributions', so2)
  80. # actually test the cython c-extension
  81. from numpy.random import PCG64
  82. values = extending_distributions.uniforms_ex(PCG64(0), 10, 'd')
  83. assert values.shape == (10,)
  84. assert values.dtype == np.float64
  85. @pytest.mark.skipif(numba is None or cffi is None,
  86. reason="requires numba and cffi")
  87. def test_numba():
  88. from numpy.random._examples.numba import extending # noqa: F401
  89. @pytest.mark.skipif(cffi is None, reason="requires cffi")
  90. def test_cffi():
  91. from numpy.random._examples.cffi import extending # noqa: F401