test_cython.py 3.4 KB

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