Skip to content

Commit 44d5873

Browse files
committed
Fix check_duplicate_key.py and remove os.path
1 parent 26f2fb2 commit 44d5873

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

pytensor/misc/check_duplicate_key.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import os
21
import pickle
32
import sys
3+
from pathlib import Path
44

55
from pytensor.configdefaults import config
66

@@ -11,35 +11,29 @@
1111
dirs = []
1212
if len(sys.argv) > 1:
1313
for compiledir in sys.argv[1:]:
14-
dirs.extend([os.path.join(compiledir, d) for d in os.listdir(compiledir)])
14+
dirs.extend([x.resolve() for x in Path(compiledir).iterdir()])
1515
else:
16-
dirs = os.listdir(config.compiledir)
17-
dirs = [os.path.join(config.compiledir, d) for d in dirs]
16+
dirs = [x.resolve() for x in config.compiledir.iterdir()]
1817
keys: dict = {} # key -> nb seen
1918
mods: dict = {}
2019
for dir in dirs:
21-
key = None
20+
if not dir.is_dir():
21+
continue
2222
try:
23-
with open(os.path.join(dir, "key.pkl")) as f:
24-
key = f.read()
23+
key = (dir / "key.pkl").read_bytes()
2524
keys.setdefault(key, 0)
2625
keys[key] += 1
27-
del f
28-
except OSError:
26+
except FileNotFoundError:
2927
# print dir, "don't have a key.pkl file"
3028
pass
3129
try:
32-
path = os.path.join(dir, "mod.cpp")
33-
if not os.path.exists(path):
34-
path = os.path.join(dir, "mod.cu")
35-
with open(path) as f:
36-
mod = f.read()
30+
path = dir / "mod.cpp"
31+
if not path.exists():
32+
path = dir / "mod.cu"
33+
mod = path.read_text(encoding="utf-8")
3734
mods.setdefault(mod, ())
3835
mods[mod] += (key,)
39-
del mod
40-
del f
41-
del path
42-
except OSError:
36+
except FileNotFoundError:
4337
print(dir, "don't have a mod.{cpp,cu} file")
4438

4539
if DISPLAY_DUPLICATE_KEYS:

0 commit comments

Comments
 (0)