|
1 | | -# SPDX-FileCopyrightText: Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
2 | 2 | # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
3 | 3 |
|
4 | | -import functools |
5 | 4 | import importlib |
6 | | -import sys |
| 5 | +import os |
| 6 | +import re |
| 7 | +from pathlib import Path |
7 | 8 |
|
| 9 | +import pytest |
| 10 | +from Cython.Build import cythonize |
| 11 | +from setuptools import Extension |
| 12 | +from setuptools.dist import Distribution |
8 | 13 |
|
9 | | -def py_func(func): |
10 | | - """ |
11 | | - Wraps func in a plain Python function. |
12 | | - """ |
| 14 | +TESTS_DIR = Path(__file__).resolve().parent |
| 15 | +CYTHON_TEST_MODULES = ["test_ccuda", "test_ccudart", "test_interoperability_cython"] |
| 16 | +TEST_NAME_RE = re.compile(r"^\s*def\s+(test_[A-Za-z0-9_]+)\s*\(") |
13 | 17 |
|
14 | | - @functools.wraps(func) |
15 | | - def wrapped(*args, **kwargs): |
16 | | - return func(*args, **kwargs) |
17 | 18 |
|
| 19 | +def _get_cuda_include_dir(): |
| 20 | + cuda_home = os.environ.get("CUDA_PATH") or os.environ.get("CUDA_HOME") |
| 21 | + if cuda_home: |
| 22 | + return Path(cuda_home) / "include" |
| 23 | + return None |
| 24 | + |
| 25 | + |
| 26 | +def build_cython_test_modules(): |
| 27 | + include_dirs = [] |
| 28 | + cuda_include_dir = _get_cuda_include_dir() |
| 29 | + if cuda_include_dir: |
| 30 | + include_dirs.append(str(cuda_include_dir)) |
| 31 | + |
| 32 | + extensions = [ |
| 33 | + Extension( |
| 34 | + name=module_name, |
| 35 | + sources=[str(TESTS_DIR / f"{module_name}.pyx")], |
| 36 | + include_dirs=include_dirs, |
| 37 | + ) |
| 38 | + for module_name in CYTHON_TEST_MODULES |
| 39 | + ] |
| 40 | + |
| 41 | + ext_modules = cythonize( |
| 42 | + extensions, |
| 43 | + compiler_directives={"language_level": "3", "freethreading_compatible": True}, |
| 44 | + nthreads=1, |
| 45 | + ) |
| 46 | + |
| 47 | + distribution = Distribution( |
| 48 | + { |
| 49 | + "name": "cuda-bindings-cython-tests", |
| 50 | + "ext_modules": ext_modules, |
| 51 | + } |
| 52 | + ) |
| 53 | + build_ext = distribution.get_command_obj("build_ext") |
| 54 | + build_ext.inplace = True |
| 55 | + build_ext.build_temp = str(TESTS_DIR / "build" / "temp") |
| 56 | + distribution.run_command("build_ext") |
| 57 | + |
| 58 | + |
| 59 | +def _import_cython_test_modules(rebuild_if_needed): |
| 60 | + imported_modules = {} |
| 61 | + build_attempted = False |
| 62 | + |
| 63 | + for module_name in CYTHON_TEST_MODULES: |
| 64 | + try: |
| 65 | + imported_modules[module_name] = importlib.import_module(module_name) |
| 66 | + except ImportError: |
| 67 | + if not rebuild_if_needed: |
| 68 | + raise |
| 69 | + |
| 70 | + if not build_attempted: |
| 71 | + build_cython_test_modules() |
| 72 | + importlib.invalidate_caches() |
| 73 | + build_attempted = True |
| 74 | + |
| 75 | + imported_modules[module_name] = importlib.import_module(module_name) |
| 76 | + |
| 77 | + return imported_modules |
| 78 | + |
| 79 | + |
| 80 | +def _discover_test_function_names(module_name): |
| 81 | + module_source = TESTS_DIR / f"{module_name}.pyx" |
| 82 | + test_names = [] |
| 83 | + |
| 84 | + with module_source.open(encoding="utf-8") as f: |
| 85 | + for line in f: |
| 86 | + match = TEST_NAME_RE.match(line) |
| 87 | + if match: |
| 88 | + test_names.append(match.group(1)) |
| 89 | + |
| 90 | + return test_names |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture(scope="session") |
| 94 | +def cython_test_modules(): |
| 95 | + return _import_cython_test_modules(rebuild_if_needed=True) |
| 96 | + |
| 97 | + |
| 98 | +def _make_wrapped_test(module_name, test_name): |
| 99 | + def wrapped(cython_test_modules): |
| 100 | + test_func = getattr(cython_test_modules[module_name], test_name) |
| 101 | + return test_func() |
| 102 | + |
| 103 | + wrapped.__name__ = test_name |
| 104 | + wrapped.__module__ = __name__ |
18 | 105 | return wrapped |
19 | 106 |
|
20 | 107 |
|
21 | | -cython_test_modules = ["test_ccuda", "test_ccudart", "test_interoperability_cython"] |
| 108 | +registered_tests = set() |
| 109 | +for module_name in CYTHON_TEST_MODULES: |
| 110 | + for test_name in _discover_test_function_names(module_name): |
| 111 | + if test_name in registered_tests: |
| 112 | + raise RuntimeError(f"duplicate cython test name discovered: {test_name}") |
| 113 | + registered_tests.add(test_name) |
| 114 | + globals()[test_name] = _make_wrapped_test(module_name, test_name) |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + build_cython_test_modules() |
22 | 119 |
|
23 | 120 |
|
24 | | -for mod in cython_test_modules: |
25 | | - try: |
26 | | - # For each callable in `mod` with name `test_*`, |
27 | | - # wrap the callable in a plain Python function |
28 | | - # and set the result as an attribute of this module. |
29 | | - mod = importlib.import_module(mod) |
30 | | - for name in dir(mod): |
31 | | - item = getattr(mod, name) |
32 | | - if callable(item) and name.startswith("test_"): |
33 | | - item = py_func(item) |
34 | | - setattr(sys.modules[__name__], name, item) |
35 | | - except ImportError: |
36 | | - raise |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments