Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cuda_core/build_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def _build_cuda_core(debug=False):
# This function populates "_extensions".
global _extensions

# Resolve CUDA first so the pathfinder import repairs PEP 517 namespace shadowing before importing bindings.
cuda_path = _get_cuda_path()

# Add cuda-bindings to sys.path so Cython can find .pxd files
# This is needed for editable installs where meta path finders don't work for Cython
# We need to add the directory containing the 'cuda' package so Cython can resolve
Expand Down Expand Up @@ -178,7 +181,7 @@ def get_sources(mod_name):

return sources

all_include_dirs = [os.path.join(_get_cuda_path(), "include")]
all_include_dirs = [os.path.join(cuda_path, "include")]
extra_compile_args = []
extra_link_args = []
extra_cythonize_kwargs = {}
Expand Down
30 changes: 30 additions & 0 deletions cuda_core/tests/test_build_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
These tests require Cython to be installed (build_hooks.py imports it).
"""

import builtins
import importlib.util
import os
import tempfile
Expand Down Expand Up @@ -50,6 +51,35 @@ def _load_build_hooks():
build_hooks = _load_build_hooks()


@pytest.mark.agent_authored(model="gpt-5.6")
def test_cuda_path_is_resolved_before_importing_bindings(monkeypatch):
"""PEP 517 namespace repair runs before cuda.bindings is imported."""
events = []

class StopBuildError(Exception):
pass

def get_cuda_path():
events.append("cuda-path")
return "/cuda"

original_import = builtins.__import__

def stop_at_bindings_import(name, *args, **kwargs):
if name == "cuda.bindings":
events.append("cuda-bindings")
raise StopBuildError
return original_import(name, *args, **kwargs)

monkeypatch.setattr(build_hooks, "_get_cuda_path", get_cuda_path)
monkeypatch.setattr(builtins, "__import__", stop_at_bindings_import)

with pytest.raises(StopBuildError):
build_hooks._build_cuda_core()

assert events == ["cuda-path", "cuda-bindings"]


def _check_version_detection(
cuda_version, expected_major, *, use_cuda_path=True, use_cuda_home=False, cuda_core_build_major=None
):
Expand Down
Loading