Skip to content

Commit 8ed361d

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 d874a16 commit 8ed361d

2 files changed

Lines changed: 33 additions & 35 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: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class _StaticLibInfo(TypedDict):
3636
_SUPPORTED_STATIC_LIBS_INFO: dict[str, _StaticLibInfo] = {
3737
"cudadevrt": {
3838
"filename": "cudadevrt.lib" if IS_WINDOWS else "libcudadevrt.a",
39-
"ctk_rel_paths": (str(Path("lib", "x64")),) if IS_WINDOWS else ("lib64", "lib"),
40-
"conda_rel_paths": ((str(Path("lib", "x64")), "lib") if IS_WINDOWS else ("lib",)),
39+
"ctk_rel_paths": ("lib/x64",) if IS_WINDOWS else ("lib64", "lib"),
40+
"conda_rel_paths": (("lib/x64", "lib") if IS_WINDOWS else ("lib",)),
4141
"site_packages_dirs": (
4242
("nvidia/cu13/lib/x64", "nvidia/cuda_runtime/lib/x64")
4343
if IS_WINDOWS
@@ -49,15 +49,14 @@ class _StaticLibInfo(TypedDict):
4949
SUPPORTED_STATIC_LIBS: tuple[str, ...] = tuple(sorted(_SUPPORTED_STATIC_LIBS_INFO.keys()))
5050

5151

52-
def _no_such_file_in_dir(dir_path: str, filename: str, error_messages: list[str], attachments: list[str]) -> None:
53-
directory = Path(dir_path)
52+
def _no_such_file_in_dir(directory: Path, filename: str, error_messages: list[str], attachments: list[str]) -> None:
5453
error_messages.append(f"No such file: {directory / filename}")
5554
if directory.is_dir():
56-
attachments.append(f' listdir("{dir_path}"):')
55+
attachments.append(f' listdir("{directory}"):')
5756
for node in sorted(node_path.name for node_path in directory.iterdir()):
5857
attachments.append(f" {node}")
5958
else:
60-
attachments.append(f' Directory does not exist: "{dir_path}"')
59+
attachments.append(f' Directory does not exist: "{directory}"')
6160

6261

6362
class _FindStaticLib:
@@ -73,16 +72,16 @@ def __init__(self, name: str) -> None:
7372
self.error_messages: list[str] = []
7473
self.attachments: list[str] = []
7574

76-
def try_site_packages(self) -> str | None:
75+
def try_site_packages(self) -> Path | None:
7776
for rel_dir in self.site_packages_dirs:
7877
sub_dir = tuple(rel_dir.split("/"))
7978
for abs_dir in find_sub_dirs_all_sitepackages(sub_dir):
8079
file_path = Path(abs_dir, self.filename)
8180
if file_path.is_file():
82-
return str(file_path)
81+
return file_path
8382
return None
8483

85-
def try_with_conda_prefix(self) -> str | None:
84+
def try_with_conda_prefix(self) -> Path | None:
8685
conda_prefix = os.environ.get("CONDA_PREFIX")
8786
if not conda_prefix:
8887
return None
@@ -91,23 +90,23 @@ def try_with_conda_prefix(self) -> str | None:
9190
for rel_path in self.conda_rel_paths:
9291
file_path = anchor / rel_path / 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_cuda_home(self) -> str | None:
96+
def try_with_cuda_home(self) -> Path | None:
9897
cuda_home = get_cuda_path_or_home()
9998
if cuda_home is None:
10099
self.error_messages.append("CUDA_HOME/CUDA_PATH not set")
101100
return None
102101

103-
cuda_home_path = Path(cuda_home)
102+
anchor = Path(cuda_home)
104103
for rel_path in self.ctk_rel_paths:
105-
file_path = cuda_home_path / rel_path / self.filename
104+
file_path = anchor / rel_path / self.filename
106105
if file_path.is_file():
107-
return str(file_path)
106+
return file_path
108107

109108
_no_such_file_in_dir(
110-
str(cuda_home_path / self.ctk_rel_paths[0]),
109+
anchor / self.ctk_rel_paths[0],
111110
self.filename,
112111
self.error_messages,
113112
self.attachments,
@@ -133,7 +132,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
133132
if abs_path is not None:
134133
return LocatedStaticLib(
135134
name=name,
136-
abs_path=abs_path,
135+
abs_path=str(abs_path),
137136
filename=finder.filename,
138137
found_via="site-packages",
139138
)
@@ -142,7 +141,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
142141
if abs_path is not None:
143142
return LocatedStaticLib(
144143
name=name,
145-
abs_path=abs_path,
144+
abs_path=str(abs_path),
146145
filename=finder.filename,
147146
found_via="conda",
148147
)
@@ -151,7 +150,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
151150
if abs_path is not None:
152151
return LocatedStaticLib(
153152
name=name,
154-
abs_path=abs_path,
153+
abs_path=str(abs_path),
155154
filename=finder.filename,
156155
found_via="CUDA_PATH",
157156
)

0 commit comments

Comments
 (0)