Skip to content

Commit 2effaf1

Browse files
committed
Fix check_duplicate_key.py and remove os.path
1 parent 320f66a commit 2effaf1

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

pytensor/misc/check_duplicate_key.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
21
import pickle
32
import sys
43
from collections import Counter
4+
from pathlib import Path
55

66
from pytensor.configdefaults import config
77

@@ -12,32 +12,25 @@
1212
dirs: list = []
1313
if len(sys.argv) > 1:
1414
for compiledir in sys.argv[1:]:
15-
dirs.extend(os.path.join(compiledir, d) for d in os.listdir(compiledir))
15+
dirs.extend(x.resolve() for x in Path(compiledir).iterdir())
1616
else:
17-
dirs = os.listdir(config.compiledir)
18-
dirs = [os.path.join(config.compiledir, d) for d in dirs]
17+
dirs = [x.resolve() for x in config.compiledir.iterdir()]
1918
keys: Counter[bytes] = Counter() # key -> nb seen
2019
mods: dict = {}
2120
for dir in dirs:
22-
key = None
21+
if not dir.is_dir():
22+
continue
2323
try:
24-
with open(os.path.join(dir, "key.pkl"), "rb") as f:
25-
key = f.read()
24+
key = (dir / "key.pkl").read_bytes()
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-
with open(path) as fmod:
34-
mod = fmod.read()
30+
mod = (dir / "mod.cpp").read_text(encoding="utf-8")
3531
mods.setdefault(mod, ())
3632
mods[mod] += (key,)
37-
del mod
38-
del fmod
39-
del path
40-
except OSError:
33+
except FileNotFoundError:
4134
print(dir, "don't have a mod.cpp file")
4235

4336
if DISPLAY_DUPLICATE_KEYS:

0 commit comments

Comments
 (0)