m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.4 KiB

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