Skip to content

Commit 9ef67b6

Browse files
committed
build: report cuda-bindings provenance in PEP 517 builds
Resolve the CUDA path before importing cuda.bindings so the existing pathfinder import repairs PEP 517 namespace shadowing first. Reuse the resolved path for the CUDA include directory.
1 parent 979b65f commit 9ef67b6

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎cuda_core/build_hooks.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def _build_cuda_core(debug=False):
133133
# This function populates "_extensions".
134134
global _extensions
135135

136+
# Resolve CUDA first so the pathfinder import repairs PEP 517 namespace shadowing before importing bindings.
137+
cuda_path = _get_cuda_path()
138+
136139
# Add cuda-bindings to sys.path so Cython can find .pxd files
137140
# This is needed for editable installs where meta path finders don't work for Cython
138141
# We need to add the directory containing the 'cuda' package so Cython can resolve
@@ -178,7 +181,7 @@ def get_sources(mod_name):
178181

179182
return sources
180183

181-
all_include_dirs = [os.path.join(_get_cuda_path(), "include")]
184+
all_include_dirs = [os.path.join(cuda_path, "include")]
182185
extra_compile_args = []
183186
extra_link_args = []
184187
extra_cythonize_kwargs = {}

‎cuda_core/tests/test_build_hooks.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
These tests require Cython to be installed (build_hooks.py imports it).
1717
"""
1818

19+
import builtins
1920
import importlib.util
2021
import os
2122
import tempfile
@@ -50,6 +51,35 @@ def _load_build_hooks():
5051
build_hooks = _load_build_hooks()
5152

5253

54+
@pytest.mark.agent_authored(model="gpt-5.6")
55+
def test_cuda_path_is_resolved_before_importing_bindings(monkeypatch):
56+
"""PEP 517 namespace repair runs before cuda.bindings is imported."""
57+
events = []
58+
59+
class StopBuildError(Exception):
60+
pass
61+
62+
def get_cuda_path():
63+
events.append("cuda-path")
64+
return "/cuda"
65+
66+
original_import = builtins.__import__
67+
68+
def stop_at_bindings_import(name, *args, **kwargs):
69+
if name == "cuda.bindings":
70+
events.append("cuda-bindings")
71+
raise StopBuildError
72+
return original_import(name, *args, **kwargs)
73+
74+
monkeypatch.setattr(build_hooks, "_get_cuda_path", get_cuda_path)
75+
monkeypatch.setattr(builtins, "__import__", stop_at_bindings_import)
76+
77+
with pytest.raises(StopBuildError):
78+
build_hooks._build_cuda_core()
79+
80+
assert events == ["cuda-path", "cuda-bindings"]
81+
82+
5383
def _check_version_detection(
5484
cuda_version, expected_major, *, use_cuda_path=True, use_cuda_home=False, cuda_core_build_major=None
5585
):

0 commit comments

Comments
 (0)