Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ def _resolve_ctk_root_via_canary() -> str | None:
return ctk_root


def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[str]) -> str | None:
"""Resolve ``normalized_name`` against ``dirs`` in order."""
seen: set[str] = set()
for directory in dirs:
if directory in seen:
continue
assert directory
seen.add(directory)
candidate = os.path.join(directory, normalized_name)
if _is_executable_candidate(candidate):
return str(resolve_path(candidate))
return None


def _resolve_names_in_trusted_dirs(candidate_names: tuple[str, ...], dirs: list[str]) -> str | None:
"""Resolve ordered candidate names within each trusted directory."""
seen: set[str] = set()
Expand Down Expand Up @@ -211,7 +197,7 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
candidate_names = (f"{utility_name}.bat", normalized_name)
found = _resolve_names_in_trusted_dirs(candidate_names, dirs)
else:
found = _resolve_in_trusted_dirs(normalized_name, dirs)
found = _resolve_names_in_trusted_dirs((normalized_name,), dirs)
if found is not None:
return found

Expand All @@ -227,7 +213,7 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
if IS_WINDOWS and utility_name == "compute-sanitizer":
found = _find_windows_compute_sanitizer(cuda_path)
else:
found = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(cuda_path))
found = _resolve_names_in_trusted_dirs((normalized_name,), _ctk_bin_subdirs(cuda_path))
if found is not None:
return found

Expand All @@ -236,5 +222,5 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
if ctk_root is not None:
if IS_WINDOWS and utility_name == "compute-sanitizer":
return _find_windows_compute_sanitizer(ctk_root)
return _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(ctk_root))
return _resolve_names_in_trusted_dirs((normalized_name,), _ctk_bin_subdirs(ctk_root))
return None
22 changes: 12 additions & 10 deletions cuda_pathfinder/tests/test_find_nvidia_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,11 @@ def test_cwd_is_not_searched(self, tmp_path, monkeypatch):
monkeypatch.chdir(evil_cwd)

# A trusted dir with no binary returns None, never the CWD copy.
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", [str(empty)]) is None
assert binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [str(empty)]) is None
# When a trusted dir holds it, that path wins regardless of CWD.
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", [str(empty), str(trusted)]) == trusted_nvcc
assert (
binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [str(empty), str(trusted)]) == trusted_nvcc
)

def test_first_trusted_dir_wins(self, tmp_path):
first = tmp_path / "a"
Expand All @@ -639,18 +641,18 @@ def test_first_trusted_dir_wins(self, tmp_path):
second.mkdir()
first_nvcc = self._make_executable(first, "nvcc")
self._make_executable(second, "nvcc")
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", [str(first), str(second)]) == first_nvcc
assert binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [str(first), str(second)]) == first_nvcc

def test_duplicate_dirs_skipped(self, tmp_path):
present = tmp_path / "p"
present.mkdir()
nvcc = self._make_executable(present, "nvcc")
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", [str(present), str(present)]) == nvcc
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", []) is None
assert binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [str(present), str(present)]) == nvcc
assert binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), []) is None

def test_empty_dir_asserts(self):
with pytest.raises(AssertionError):
binary_finder_module._resolve_in_trusted_dirs("nvcc", [""])
binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [""])

@pytest.mark.skipif(binary_finder_module.IS_WINDOWS, reason="POSIX execute-bit semantics")
def test_non_executable_file_rejected_on_posix(self, tmp_path):
Expand All @@ -660,9 +662,9 @@ def test_non_executable_file_rejected_on_posix(self, tmp_path):
with open(path, "w", encoding="utf-8") as handle:
handle.write("")
os.chmod(path, 0o644)
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", [str(directory)]) is None
assert binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [str(directory)]) is None
os.chmod(path, 0o700)
assert binary_finder_module._resolve_in_trusted_dirs("nvcc", [str(directory)]) == path
assert binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [str(directory)]) == path


@pytest.mark.usefixtures("clear_find_binary_cache")
Expand All @@ -684,7 +686,7 @@ def test_caching_per_utility():
assert nvdisasm1 != nvcc1


def test_resolve_in_trusted_dirs_returns_absolute_path(tmp_path, monkeypatch, mocker):
def test_resolve_names_in_trusted_dirs_returns_absolute_path(tmp_path, monkeypatch, mocker):
"""A match found under a relative search dir must be absolutized.

``find_nvidia_binary_utility`` documents an absolute, separator-resolved
Expand All @@ -702,7 +704,7 @@ def test_resolve_in_trusted_dirs_returns_absolute_path(tmp_path, monkeypatch, mo

# Anchor CWD so relative candidates have a deterministic resolved path.
monkeypatch.chdir(tmp_path)
result = binary_finder_module._resolve_in_trusted_dirs("nvcc", [rel_dir])
result = binary_finder_module._resolve_names_in_trusted_dirs(("nvcc",), [rel_dir])

assert os.path.isabs(result)
assert result == str((tmp_path / candidate).resolve())
Loading