Skip to content

Commit 6b8104b

Browse files
committed
Remove os.path in link/c/op.py
1 parent 8fe653c commit 6b8104b

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

pytensor/link/c/op.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import inspect
2-
import os
32
import re
43
import warnings
54
from collections.abc import Callable, Collection
5+
from pathlib import Path
66
from re import Pattern
77
from typing import TYPE_CHECKING, Any, ClassVar, cast
88

@@ -288,28 +288,32 @@ class ExternalCOp(COp):
288288
_cop_num_outputs: int | None = None
289289

290290
@classmethod
291-
def get_path(cls, f: str) -> str:
291+
def get_path(cls, f: Path) -> Path:
292292
"""Convert a path relative to the location of the class file into an absolute path.
293293
294294
Paths that are already absolute are passed through unchanged.
295295
296296
"""
297-
if not os.path.isabs(f):
297+
if not f.is_absolute():
298298
class_file = inspect.getfile(cls)
299-
class_dir = os.path.dirname(class_file)
300-
f = os.path.realpath(os.path.join(class_dir, f))
299+
class_dir = Path(class_file).parent
300+
f = (class_dir / f).resolve()
301301
return f
302302

303-
def __init__(self, func_files: str | list[str], func_name: str | None = None):
303+
def __init__(
304+
self,
305+
func_files: str | Path | list[str] | list[Path],
306+
func_name: str | None = None,
307+
):
304308
"""
305309
Sections are loaded from files in order with sections in later
306310
files overriding sections in previous files.
307311
308312
"""
309313
if not isinstance(func_files, list):
310-
self.func_files = [func_files]
314+
self.func_files = [Path(func_files)]
311315
else:
312-
self.func_files = func_files
316+
self.func_files = [Path(func_file) for func_file in func_files]
313317

314318
self.func_codes: list[str] = []
315319
# Keep the original name. If we reload old pickle, we want to
@@ -334,12 +338,11 @@ def __init__(self, func_files: str | list[str], func_name: str | None = None):
334338
"Cannot have an `op_code_cleanup` section and specify `func_name`"
335339
)
336340

337-
def load_c_code(self, func_files: list[str]) -> None:
341+
def load_c_code(self, func_files: list[Path]) -> None:
338342
"""Loads the C code to perform the `Op`."""
339-
func_files = [self.get_path(f) for f in func_files]
340343
for func_file in func_files:
341-
with open(func_file) as f:
342-
self.func_codes.append(f.read())
344+
func_file = self.get_path(func_file)
345+
self.func_codes.append(func_file.read_text(encoding="utf-8"))
343346

344347
# If both the old section markers and the new section markers are
345348
# present, raise an error because we don't know which ones to follow.

0 commit comments

Comments
 (0)