Skip to content

Commit 11916d6

Browse files
committed
Return Path from the _static_libs internals
Follow-up to the review feedback on #2489: the str-compatibility constraint applies only to the public API. The try_* methods and _no_such_file_in_dir now work in Path throughout. str() is applied once, where abs_path is stored on the public LocatedStaticLib and LocatedBitcodeLib. The relative-path constants go from os.path.join(...) to forward-slash literals, matching how site_packages_dirs is already written in the same dicts; Path normalizes the separator on Windows. One behavior change: a CUDA_PATH or CONDA_PREFIX containing redundant separators ("//", "/.") now produces a normalized abs_path, because Path collapses them. Differential fuzzing against the pre-revision code (16k lookups over randomized trees, comparing located paths and full error text) shows no other difference, and none at all when those variables are free of redundant separators. Signed-off-by: LeSingh1 <[email protected]>
1 parent d796d2a commit 11916d6

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

‎cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class _BitcodeLibInfo(TypedDict):
3636
_SUPPORTED_BITCODE_LIBS_INFO: dict[str, _BitcodeLibInfo] = {
3737
"device": {
3838
"filename": "libdevice.10.bc",
39-
"rel_path": str(Path("nvvm", "libdevice")),
39+
"rel_path": "nvvm/libdevice",
4040
"site_packages_dirs": (
4141
"nvidia/cu13/nvvm/libdevice",
4242
"nvidia/cuda_nvcc/nvvm/libdevice",
@@ -65,15 +65,14 @@ class _BitcodeLibInfo(TypedDict):
6565
)
6666

6767

68-
def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None:
69-
directory = Path(dir_path)
68+
def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None:
7069
error_messages.append(f"No such file: {directory / filename}")
7170
if directory.is_dir():
72-
attachments.append(f' listdir("{dir_path}"):')
71+
attachments.append(f' listdir("{directory}"):')
7372
for node in sorted(node_path.name for node_path in directory.iterdir()):
7473
attachments.append(f" {node}")
7574
else:
76-
attachments.append(f' Directory does not exist: "{dir_path}"')
75+
attachments.append(f' Directory does not exist: "{directory}"')
7776

7877

7978
class _FindBitcodeLib:
@@ -88,39 +87,39 @@ def __init__(self, name: str) -> None:
8887
self.error_messages: list[str] = []
8988
self.attachments: list[str] = []
9089

91-
def try_site_packages(self) -> str | None:
90+
def try_site_packages(self) -> Path | None:
9291
for rel_dir in self.site_packages_dirs:
9392
sub_dir = tuple(rel_dir.split("/"))
9493
for abs_dir in find_sub_dirs_all_sitepackages(sub_dir):
9594
file_path = Path(abs_dir, self.filename)
9695
if file_path.is_file():
97-
return str(file_path)
96+
return file_path
9897
return None
9998

100-
def try_with_conda_prefix(self) -> str | None:
99+
def try_with_conda_prefix(self) -> Path | None:
101100
conda_prefix = os.environ.get("CONDA_PREFIX")
102101
if not conda_prefix:
103102
return None
104103

105104
anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix)
106105
file_path = anchor / self.rel_path / self.filename
107106
if file_path.is_file():
108-
return str(file_path)
107+
return file_path
109108
return None
110109

111-
def try_with_cuda_home(self) -> str | None:
110+
def try_with_cuda_home(self) -> Path | None:
112111
cuda_home = get_cuda_path_or_home()
113112
if cuda_home is None:
114113
self.error_messages.append("CUDA_HOME/CUDA_PATH not set")
115114
return None
116115

117-
cuda_home_path = Path(cuda_home)
118-
file_path = cuda_home_path / self.rel_path / self.filename
116+
anchor = Path(cuda_home)
117+
file_path = anchor / self.rel_path / self.filename
119118
if file_path.is_file():
120-
return str(file_path)
119+
return file_path
121120

122121
_no_such_file_in_dir(
123-
str(cuda_home_path / self.rel_path),
122+
anchor / self.rel_path,
124123
self.filename,
125124
self.error_messages,
126125
self.attachments,
@@ -146,7 +145,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
146145
if abs_path is not None:
147146
return LocatedBitcodeLib(
148147
name=name,
149-
abs_path=abs_path,
148+
abs_path=str(abs_path),
150149
filename=finder.filename,
151150
found_via="site-packages",
152151
)
@@ -155,7 +154,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
155154
if abs_path is not None:
156155
return LocatedBitcodeLib(
157156
name=name,
158-
abs_path=abs_path,
157+
abs_path=str(abs_path),
159158
filename=finder.filename,
160159
found_via="conda",
161160
)
@@ -164,7 +163,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
164163
if abs_path is not None:
165164
return LocatedBitcodeLib(
166165
name=name,
167-
abs_path=abs_path,
166+
abs_path=str(abs_path),
168167
filename=finder.filename,
169168
found_via="CUDA_PATH",
170169
)

‎cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ def _cudadevrt_info() -> _StaticLibInfo:
6161
SUPPORTED_STATIC_LIBS: tuple[str, ...] = tuple(sorted(_SUPPORTED_STATIC_LIBS_INFO.keys()))
6262

6363

64-
def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None:
65-
directory = Path(dir_path)
64+
def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None:
6665
error_messages.append(f"No such file: {directory / filename}")
6766
if directory.is_dir():
68-
attachments.append(f' listdir("{dir_path}"):')
67+
attachments.append(f' listdir("{directory}"):')
6968
for node in sorted(node_path.name for node_path in directory.iterdir()):
7069
attachments.append(f" {node}")
7170
else:
72-
attachments.append(f' Directory does not exist: "{dir_path}"')
71+
attachments.append(f' Directory does not exist: "{directory}"')
7372

7473

7574
class _FindStaticLib:
@@ -85,16 +84,16 @@ def __init__(self, name: str) -> None:
8584
self.error_messages: list[str] = []
8685
self.attachments: list[str] = []
8786

88-
def try_site_packages(self) -> str | None:
87+
def try_site_packages(self) -> Path | None:
8988
for rel_dir in self.site_packages_dirs:
9089
sub_dir = tuple(rel_dir.split("/"))
9190
for abs_dir in find_sub_dirs_all_sitepackages(sub_dir):
9291
file_path = Path(abs_dir, self.filename)
9392
if file_path.is_file():
94-
return str(file_path)
93+
return file_path
9594
return None
9695

97-
def try_with_conda_prefix(self) -> str | None:
96+
def try_with_conda_prefix(self) -> Path | None:
9897
conda_prefix = os.environ.get("CONDA_PREFIX")
9998
if not conda_prefix:
10099
return None
@@ -103,23 +102,23 @@ def try_with_conda_prefix(self) -> str | None:
103102
for rel_path in self.conda_rel_paths:
104103
file_path = anchor / rel_path / self.filename
105104
if file_path.is_file():
106-
return str(file_path)
105+
return file_path
107106
return None
108107

109-
def try_with_cuda_home(self) -> str | None:
108+
def try_with_cuda_home(self) -> Path | None:
110109
cuda_home = get_cuda_path_or_home()
111110
if cuda_home is None:
112111
self.error_messages.append("CUDA_HOME/CUDA_PATH not set")
113112
return None
114113

115-
cuda_home_path = Path(cuda_home)
114+
anchor = Path(cuda_home)
116115
for rel_path in self.ctk_rel_paths:
117-
file_path = cuda_home_path / rel_path / self.filename
116+
file_path = anchor / rel_path / self.filename
118117
if file_path.is_file():
119-
return str(file_path)
118+
return file_path
120119

121120
_no_such_file_in_dir(
122-
str(cuda_home_path / self.ctk_rel_paths[0]),
121+
anchor / self.ctk_rel_paths[0],
123122
self.filename,
124123
self.error_messages,
125124
self.attachments,
@@ -145,7 +144,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
145144
if abs_path is not None:
146145
return LocatedStaticLib(
147146
name=name,
148-
abs_path=abs_path,
147+
abs_path=str(abs_path),
149148
filename=finder.filename,
150149
found_via="site-packages",
151150
)
@@ -154,7 +153,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
154153
if abs_path is not None:
155154
return LocatedStaticLib(
156155
name=name,
157-
abs_path=abs_path,
156+
abs_path=str(abs_path),
158157
filename=finder.filename,
159158
found_via="conda",
160159
)
@@ -163,7 +162,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
163162
if abs_path is not None:
164163
return LocatedStaticLib(
165164
name=name,
166-
abs_path=abs_path,
165+
abs_path=str(abs_path),
167166
filename=finder.filename,
168167
found_via="CUDA_PATH",
169168
)

0 commit comments

Comments
 (0)