4
4
import sys
5
5
import warnings
6
6
from importlib import reload
7
+ from pathlib import Path
7
8
from types import ModuleType
8
9
9
10
import pytensor
21
22
22
23
def try_import ():
23
24
global lazylinker_ext
24
- sys .path [0 :0 ] = [config .compiledir ]
25
+ sys .path [0 :0 ] = [str ( config .compiledir ) ]
25
26
import lazylinker_ext
26
27
27
28
del sys .path [0 ]
28
29
29
30
30
31
def try_reload ():
31
- sys .path [0 :0 ] = [config .compiledir ]
32
+ sys .path [0 :0 ] = [str ( config .compiledir ) ]
32
33
reload (lazylinker_ext )
33
34
del sys .path [0 ]
34
35
@@ -41,8 +42,8 @@ def try_reload():
41
42
# Note that these lines may seem redundant (they are repeated in
42
43
# compile_str()) but if another lazylinker_ext does exist then it will be
43
44
# 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 ():
46
47
try :
47
48
# Try to make the location
48
49
os .mkdir (location )
@@ -53,18 +54,18 @@ def try_reload():
53
54
# are not holding the lock right now, so we could race
54
55
# another process and get error 17 if we lose the race
55
56
assert e .errno == errno .EEXIST
56
- assert os . path . isdir ( location )
57
+ assert location . is_dir ( )
57
58
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 ():
60
61
try :
61
62
with open (init_file , "w" ):
62
63
pass
63
64
except OSError as e :
64
- if os . path . exists (init_file ):
65
+ if init_file . exists ():
65
66
pass # has already been created
66
67
else :
67
- e .args += (f"{ location } exist? { os . path . exists (location )} " ,)
68
+ e .args += (f"{ location } exist? { location . exists ()} " ,)
68
69
raise
69
70
70
71
_need_reload = False
@@ -109,10 +110,8 @@ def try_reload():
109
110
raise
110
111
_logger .info ("Compiling new CVM" )
111
112
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 ():
116
115
# This can happen in not normal case. We just
117
116
# disable the c clinker. If we are here the user
118
117
# didn't disable the compiler, so print a warning.
@@ -130,27 +129,27 @@ def try_reload():
130
129
with open (cfile ) as f :
131
130
code = f .read ()
132
131
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 ():
135
134
try :
136
135
os .mkdir (loc )
137
136
except OSError as e :
138
137
assert e .errno == errno .EEXIST
139
- assert os . path . exists (loc )
138
+ assert loc . exists ()
140
139
141
140
args = GCC_compiler .compile_args ()
142
141
GCC_compiler .compile_str (dirname , code , location = loc , preargs = args )
143
142
# Save version into the __init__.py file.
144
- init_py = os . path . join ( loc , "__init__.py" )
143
+ init_py = loc / "__init__.py"
145
144
146
145
with open (init_py , "w" ) as f :
147
146
f .write (f"_version = { version } \n " )
148
147
149
148
# If we just compiled the module for the first time, then it was
150
149
# imported at the same time: we need to make sure we do not
151
150
# 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 ( ):
154
153
os .remove (init_pyc )
155
154
156
155
try_import ()
0 commit comments