Skip to content

Commit c0d5117

Browse files
committed
Replace os.path with pathlib in lazylinker_c
1 parent 7da7bd7 commit c0d5117

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

pytensor/link/c/lazylinker_c.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import warnings
66
from importlib import reload
7+
from pathlib import Path
78
from types import ModuleType
89

910
import pytensor
@@ -21,14 +22,14 @@
2122

2223
def try_import():
2324
global lazylinker_ext
24-
sys.path[0:0] = [config.compiledir]
25+
sys.path[0:0] = [str(config.compiledir)]
2526
import lazylinker_ext
2627

2728
del sys.path[0]
2829

2930

3031
def try_reload():
31-
sys.path[0:0] = [config.compiledir]
32+
sys.path[0:0] = [str(config.compiledir)]
3233
reload(lazylinker_ext)
3334
del sys.path[0]
3435

@@ -41,8 +42,8 @@ def try_reload():
4142
# Note that these lines may seem redundant (they are repeated in
4243
# compile_str()) but if another lazylinker_ext does exist then it will be
4344
# imported and compile_str won't get called at all.
44-
location = os.path.join(config.compiledir, "lazylinker_ext")
45-
if not os.path.exists(location):
45+
location = config.compiledir / "lazylinker_ext"
46+
if not location.exists():
4647
try:
4748
# Try to make the location
4849
os.mkdir(location)
@@ -53,18 +54,18 @@ def try_reload():
5354
# are not holding the lock right now, so we could race
5455
# another process and get error 17 if we lose the race
5556
assert e.errno == errno.EEXIST
56-
assert os.path.isdir(location)
57+
assert location.is_dir()
5758

58-
init_file = os.path.join(location, "__init__.py")
59-
if not os.path.exists(init_file):
59+
init_file = location / "__init__.py"
60+
if not init_file.exists():
6061
try:
6162
with open(init_file, "w"):
6263
pass
6364
except OSError as e:
64-
if os.path.exists(init_file):
65+
if init_file.exists():
6566
pass # has already been created
6667
else:
67-
e.args += (f"{location} exist? {os.path.exists(location)}",)
68+
e.args += (f"{location} exist? {location.exists()}",)
6869
raise
6970

7071
_need_reload = False
@@ -109,10 +110,8 @@ def try_reload():
109110
raise
110111
_logger.info("Compiling new CVM")
111112
dirname = "lazylinker_ext"
112-
cfile = os.path.join(
113-
pytensor.__path__[0], "link", "c", "c_code", "lazylinker_c.c"
114-
)
115-
if not os.path.exists(cfile):
113+
cfile = Path(pytensor.__path__[0]) / "link/c/c_code/lazylinker_c.c"
114+
if not cfile.exists():
116115
# This can happen in not normal case. We just
117116
# disable the c clinker. If we are here the user
118117
# didn't disable the compiler, so print a warning.
@@ -130,27 +129,27 @@ def try_reload():
130129
with open(cfile) as f:
131130
code = f.read()
132131

133-
loc = os.path.join(config.compiledir, dirname)
134-
if not os.path.exists(loc):
132+
loc = config.compiledir / dirname
133+
if not loc.exists():
135134
try:
136135
os.mkdir(loc)
137136
except OSError as e:
138137
assert e.errno == errno.EEXIST
139-
assert os.path.exists(loc)
138+
assert loc.exists()
140139

141140
args = GCC_compiler.compile_args()
142141
GCC_compiler.compile_str(dirname, code, location=loc, preargs=args)
143142
# Save version into the __init__.py file.
144-
init_py = os.path.join(loc, "__init__.py")
143+
init_py = loc / "__init__.py"
145144

146145
with open(init_py, "w") as f:
147146
f.write(f"_version = {version}\n")
148147

149148
# If we just compiled the module for the first time, then it was
150149
# imported at the same time: we need to make sure we do not
151150
# reload the now outdated __init__.pyc below.
152-
init_pyc = os.path.join(loc, "__init__.pyc")
153-
if os.path.isfile(init_pyc):
151+
init_pyc = loc / "__init__.pyc"
152+
if init_pyc.is_file():
154153
os.remove(init_pyc)
155154

156155
try_import()

0 commit comments

Comments
 (0)