Skip to content

Commit 2d5b704

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 184881a commit 2d5b704

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
@@ -77,7 +77,7 @@ def _resolve_ctk_root_via_canary() -> str | None:
7777
return ctk_root
7878

7979

80-
def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[Path]) -> str | None:
80+
def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[Path]) -> Path | None:
8181
"""Resolve ``normalized_name`` against ``dirs`` in order."""
8282
seen: set[Path] = set()
8383
for directory in dirs:
@@ -92,7 +92,7 @@ def _resolve_in_trusted_dirs(normalized_name: str, dirs: list[Path]) -> str | No
9292
# search dir would otherwise leak a relative result). os.path.abspath
9393
# has no pathlib equivalent: Path.absolute() does not normalize and
9494
# Path.resolve() would also follow symlinks.
95-
return os.path.abspath(candidate)
95+
return Path(os.path.abspath(candidate))
9696
return None
9797

9898

@@ -213,7 +213,8 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
213213
candidate_names = (f"{utility_name}.bat", normalized_name)
214214
found = _resolve_names_in_trusted_dirs(candidate_names, dirs)
215215
else:
216-
found = _resolve_in_trusted_dirs(normalized_name, dirs)
216+
resolved = _resolve_in_trusted_dirs(normalized_name, dirs)
217+
found = None if resolved is None else str(resolved)
217218
if found is not None:
218219
return found
219220

@@ -229,7 +230,8 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
229230
if IS_WINDOWS and utility_name == "compute-sanitizer":
230231
found = _find_windows_compute_sanitizer(cuda_path)
231232
else:
232-
found = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(cuda_path))
233+
resolved = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(Path(cuda_path)))
234+
found = None if resolved is None else str(resolved)
233235
if found is not None:
234236
return found
235237

@@ -238,5 +240,6 @@ def find_nvidia_binary_utility(utility_name: str) -> str | None:
238240
if ctk_root is not None:
239241
if IS_WINDOWS and utility_name == "compute-sanitizer":
240242
return _find_windows_compute_sanitizer(ctk_root)
241-
return _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(Path(ctk_root)))
243+
resolved = _resolve_in_trusted_dirs(normalized_name, _ctk_bin_subdirs(Path(ctk_root)))
244+
return None if resolved is None else str(resolved)
242245
return None

cuda_pathfinder/tests/test_find_nvidia_binaries.py

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

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

409409

@@ -424,7 +424,7 @@ def test_find_binary_ctk_root_canary_fallback(monkeypatch, mocker):
424424

425425
result = find_nvidia_binary_utility("nvcc")
426426

427-
assert result == os.path.abspath(ctk_nvcc)
427+
assert result == str(ctk_nvcc.absolute())
428428
canary_mock.assert_called_once_with()
429429
# No earlier trusted dirs existed, so the only probe is the canary bin dir.
430430
assert checked == [ctk_nvcc]
@@ -469,7 +469,7 @@ def test_find_binary_canary_not_consulted_when_found_earlier(monkeypatch, mocker
469469

470470
result = find_nvidia_binary_utility("nvcc")
471471

472-
assert result == os.path.abspath(conda_nvcc)
472+
assert result == str(conda_nvcc.absolute())
473473
canary_mock.assert_not_called()
474474

475475

0 commit comments

Comments
 (0)