test_extending.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os, sys
  2. import pytest
  3. import warnings
  4. import shutil
  5. import subprocess
  6. try:
  7. import cffi
  8. except ImportError:
  9. cffi = None
  10. if sys.flags.optimize > 1:
  11. # no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1
  12. # cffi cannot succeed
  13. cffi = None
  14. try:
  15. with warnings.catch_warnings(record=True) as w:
  16. # numba issue gh-4733
  17. warnings.filterwarnings('always', '', DeprecationWarning)
  18. import numba
  19. except ImportError:
  20. numba = None
  21. try:
  22. import cython
  23. from Cython.Compiler.Version import version as cython_version
  24. except ImportError:
  25. cython = None
  26. else:
  27. from distutils.version import LooseVersion
  28. # Cython 0.29.14 is required for Python 3.8 and there are
  29. # other fixes in the 0.29 series that are needed even for earlier
  30. # Python versions.
  31. # Note: keep in sync with the one in pyproject.toml
  32. required_version = LooseVersion('0.29.14')
  33. if LooseVersion(cython_version) < required_version:
  34. # too old or wrong cython, skip the test
  35. cython = None
  36. @pytest.mark.skipif(cython is None, reason="requires cython")
  37. @pytest.mark.slow
  38. def test_cython(tmp_path):
  39. examples = os.path.join(os.path.dirname(__file__), '..', '_examples')
  40. # CPython 3.5 and below does not handle __fspath__ well: see bpo-26027
  41. shutil.copytree(examples, str(tmp_path / '_examples'))
  42. subprocess.check_call([sys.executable, 'setup.py', 'build'],
  43. cwd=str(tmp_path / '_examples' / 'cython'))
  44. @pytest.mark.skipif(numba is None or cffi is None,
  45. reason="requires numba and cffi")
  46. def test_numba():
  47. from numpy.random._examples.numba import extending
  48. @pytest.mark.skipif(cffi is None, reason="requires cffi")
  49. def test_cffi():
  50. from numpy.random._examples.cffi import extending