test_cython.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import os
  2. import shutil
  3. import subprocess
  4. import sys
  5. import pytest
  6. import numpy as np
  7. from numpy.testing import IS_WASM
  8. # This import is copied from random.tests.test_extending
  9. try:
  10. import cython
  11. from Cython.Compiler.Version import version as cython_version
  12. except ImportError:
  13. cython = None
  14. else:
  15. from numpy.compat import _pep440
  16. # Cython 0.29.30 is required for Python 3.11 and there are
  17. # other fixes in the 0.29 series that are needed even for earlier
  18. # Python versions.
  19. # Note: keep in sync with the one in pyproject.toml
  20. required_version = "0.29.30"
  21. if _pep440.parse(cython_version) < _pep440.Version(required_version):
  22. # too old or wrong cython, skip the test
  23. cython = None
  24. pytestmark = pytest.mark.skipif(cython is None, reason="requires cython")
  25. @pytest.fixture
  26. def install_temp(request, tmp_path):
  27. # Based in part on test_cython from random.tests.test_extending
  28. if IS_WASM:
  29. pytest.skip("No subprocess")
  30. here = os.path.dirname(__file__)
  31. ext_dir = os.path.join(here, "examples", "cython")
  32. cytest = str(tmp_path / "cytest")
  33. shutil.copytree(ext_dir, cytest)
  34. # build the examples and "install" them into a temporary directory
  35. install_log = str(tmp_path / "tmp_install_log.txt")
  36. subprocess.check_output(
  37. [
  38. sys.executable,
  39. "setup.py",
  40. "build",
  41. "install",
  42. "--prefix", str(tmp_path / "installdir"),
  43. "--single-version-externally-managed",
  44. "--record",
  45. install_log,
  46. ],
  47. cwd=cytest,
  48. )
  49. # In order to import the built module, we need its path to sys.path
  50. # so parse that out of the record
  51. with open(install_log) as fid:
  52. for line in fid:
  53. if "checks" in line:
  54. sys.path.append(os.path.dirname(line))
  55. break
  56. else:
  57. raise RuntimeError(f'could not parse "{install_log}"')
  58. def test_is_timedelta64_object(install_temp):
  59. import checks
  60. assert checks.is_td64(np.timedelta64(1234))
  61. assert checks.is_td64(np.timedelta64(1234, "ns"))
  62. assert checks.is_td64(np.timedelta64("NaT", "ns"))
  63. assert not checks.is_td64(1)
  64. assert not checks.is_td64(None)
  65. assert not checks.is_td64("foo")
  66. assert not checks.is_td64(np.datetime64("now", "s"))
  67. def test_is_datetime64_object(install_temp):
  68. import checks
  69. assert checks.is_dt64(np.datetime64(1234, "ns"))
  70. assert checks.is_dt64(np.datetime64("NaT", "ns"))
  71. assert not checks.is_dt64(1)
  72. assert not checks.is_dt64(None)
  73. assert not checks.is_dt64("foo")
  74. assert not checks.is_dt64(np.timedelta64(1234))
  75. def test_get_datetime64_value(install_temp):
  76. import checks
  77. dt64 = np.datetime64("2016-01-01", "ns")
  78. result = checks.get_dt64_value(dt64)
  79. expected = dt64.view("i8")
  80. assert result == expected
  81. def test_get_timedelta64_value(install_temp):
  82. import checks
  83. td64 = np.timedelta64(12345, "h")
  84. result = checks.get_td64_value(td64)
  85. expected = td64.view("i8")
  86. assert result == expected
  87. def test_get_datetime64_unit(install_temp):
  88. import checks
  89. dt64 = np.datetime64("2016-01-01", "ns")
  90. result = checks.get_dt64_unit(dt64)
  91. expected = 10
  92. assert result == expected
  93. td64 = np.timedelta64(12345, "h")
  94. result = checks.get_dt64_unit(td64)
  95. expected = 5
  96. assert result == expected
  97. def test_abstract_scalars(install_temp):
  98. import checks
  99. assert checks.is_integer(1)
  100. assert checks.is_integer(np.int8(1))
  101. assert checks.is_integer(np.uint64(1))