Skip to content

Commit 7417997

Browse files
committed
Move the remaining test os.path calls to pathlib
The three os.path.abspath assertions in test_find_nvidia_binaries.py become str(...absolute()), and the os.path.isdir in test_utils_find_sub_dirs.py becomes Path.is_dir(). import os stays in the binaries test for os.sep.
1 parent 8e3d5c7 commit 7417997

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

cuda_pathfinder/cuda/pathfinder/_binaries/find_nvidia_binary_utility.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _resolve_ctk_root_via_canary() -> str | None:
147147
return ctk_root
148148

149149

150-
def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[Path]) -> str | None:
150+
def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[Path]) -> Path | None:
151151
"""Resolve ``normalized_name`` against ``dirs`` in order."""
152152
seen: set[Path] = set()
153153
for directory in dirs:
@@ -162,7 +162,7 @@ def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[Path]) -> str | No
162162
# search dir would otherwise leak a relative result). os.path.abspath
163163
# has no pathlib equivalent: Path.absolute() does not normalize and
164164
# Path.resolve() would also follow symlinks.
165-
return os.path.abspath(candidate)
165+
return Path(os.path.abspath(candidate))
166166
return None
167167

168168

@@ -283,7 +283,8 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
283283
candidate_names = (f"{utility_name}.bat", normalized_name)
284284
found = _resolve_names_in_trusted_dirs(candidate_names, dirs)
285285
else:
286-
found = _resolve_in_trusted_dirs(normalized_name, dirs)
286+
resolved = _resolve_in_trusted_dirs(normalized_name, dirs)
287+
found = None if resolved is None else str(resolved)
287288
if found is not None:
288289
return found
289290

@@ -299,7 +300,8 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
299300
if IS_WINDOWS and utility_name == "compute-sanitizer":
300301
found = _find_windows_compute_sanitizer(cuda_path)
301302
else:
302-
found = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(cuda_path))
303+
resolved = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(cuda_path))
304+
found = None if resolved is None else str(resolved)
303305
if found is not None:
304306
return found
305307

@@ -308,5 +310,6 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
308310
if ctk_root is not None:
309311
if IS_WINDOWS and utility_name == "compute-sanitizer":
310312
return _find_windows_compute_sanitizer(ctk_root)
311-
return _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(Path(ctk_root)))
313+
resolved = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(Path(ctk_root)))
314+
return None if resolved is None else str(resolved)
312315
return None

cuda_pathfinder/tests/test_find_nvidia_binaries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def test_find_binary_first_matching_dir_wins(monkeypatch, mocker):
571571
result = find_nvidia_binary_utility("nvcc")
572572

573573
# Conda comes before CUDA_HOME, so the Conda hit wins and CUDA_HOME is never probed.
574-
assert result == os.path.abspath(conda_nvcc)
574+
assert result == str(conda_nvcc.absolute())
575575
assert checked == [site_dir / "nvcc", conda_nvcc]
576576

577577

@@ -592,7 +592,7 @@ def test_find_binary_ctk_root_canary_fallback(monkeypatch, mocker):
592592

593593
result = find_nvidia_binary_utility("nvcc")
594594

595-
assert result == os.path.abspath(ctk_nvcc)
595+
assert result == str(ctk_nvcc.absolute())
596596
canary_mock.assert_called_once_with()
597597
# No earlier trusted dirs existed, so the only probe is the canary bin dir.
598598
assert checked == [ctk_nvcc]
@@ -637,7 +637,7 @@ def test_find_binary_canary_not_consulted_when_found_earlier(monkeypatch, mocker
637637

638638
result = find_nvidia_binary_utility("nvcc")
639639

640-
assert result == os.path.abspath(conda_nvcc)
640+
assert result == str(conda_nvcc.absolute())
641641
canary_mock.assert_not_called()
642642

643643

0 commit comments

Comments
 (0)