Skip to content

Commit

Permalink
Replace more os.path with pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Armavica authored and ricardoV94 committed Jul 10, 2024
1 parent 1bdbddf commit a99d067
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 32 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ line-length = 88
exclude = ["doc/", "pytensor/_version.py"]

[tool.ruff.lint]
select = ["C", "E", "F", "I", "UP", "W", "RUF", "PERF"]
select = ["C", "E", "F", "I", "UP", "W", "RUF", "PERF", "PTH"]
ignore = ["C408", "C901", "E501", "E741", "RUF012", "PERF203"]


Expand All @@ -136,6 +136,7 @@ lines-after-imports = 2
# TODO: Get rid of these:
"**/__init__.py" = ["F401", "E402", "F403"]
"pytensor/tensor/linalg.py" = ["F403"]
"pytensor/link/c/cmodule.py" = ["PTH"]
# For the tests we skip because `pytest.importorskip` is used:
"tests/link/jax/test_scalar.py" = ["E402"]
"tests/link/jax/test_tensor_basic.py" = ["E402"]
Expand Down
27 changes: 4 additions & 23 deletions pytensor/link/c/lazylinker_c.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import errno
import logging
import os
import sys
import warnings
from importlib import reload
Expand Down Expand Up @@ -43,23 +41,12 @@ def try_reload():
# compile_str()) but if another lazylinker_ext does exist then it will be
# imported and compile_str won't get called at all.
location = config.compiledir / "lazylinker_ext"
if not location.exists():
try:
# Try to make the location
os.mkdir(location)
except OSError as e:
# If we get an error, verify that the error was # 17, the
# path already exists, and that it is a directory Note: we
# can't check if it exists before making it, because we
# are not holding the lock right now, so we could race
# another process and get error 17 if we lose the race
assert e.errno == errno.EEXIST
assert location.is_dir()
location.mkdir(exist_ok=True)

init_file = location / "__init__.py"
if not init_file.exists():
try:
with open(init_file, "w"):
with init_file.open("w"):
pass
except OSError as e:
if init_file.exists():
Expand Down Expand Up @@ -129,12 +116,7 @@ def try_reload():
code = cfile.read_text("utf-8")

loc = config.compiledir / dirname
if not loc.exists():
try:
os.mkdir(loc)
except OSError as e:
assert e.errno == errno.EEXIST
assert loc.exists()
loc.mkdir(exist_ok=True)

args = GCC_compiler.compile_args()
GCC_compiler.compile_str(dirname, code, location=loc, preargs=args)
Expand All @@ -147,8 +129,7 @@ def try_reload():
# imported at the same time: we need to make sure we do not
# reload the now outdated __init__.pyc below.
init_pyc = loc / "__init__.pyc"
if init_pyc.is_file():
os.remove(init_pyc)
init_pyc.unlink(missing_ok=True)

try_import()
try_reload()
Expand Down
2 changes: 1 addition & 1 deletion pytensor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def subprocess_Popen(command: str | list[str], **params):
# with the default None values.
stdin = None
if "stdin" not in params:
stdin = open(os.devnull)
stdin = Path(os.devnull).open()
params["stdin"] = stdin.fileno()

try:
Expand Down
8 changes: 5 additions & 3 deletions tests/misc/test_pkl_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os
import shutil
from pathlib import Path
from tempfile import mkdtemp

from pytensor.misc.pkl_utils import StripPickler
from pytensor.tensor.type import matrix


# FIXME: this test looks weird
class TestStripPickler:
def setup_method(self):
# Work in a temporary directory to avoid cluttering the repository
self.origdir = os.getcwd()
self.origdir = Path.cwd()
self.tmpdir = mkdtemp()
os.chdir(self.tmpdir)

Expand All @@ -20,9 +22,9 @@ def teardown_method(self):
shutil.rmtree(self.tmpdir)

def test_basic(self):
with open("test.pkl", "wb") as f:
with Path("test.pkl").open("wb"):
m = matrix()
dest_pkl = "my_test.pkl"
with open(dest_pkl, "wb") as f:
with Path(dest_pkl).open("wb") as f:
strip_pickler = StripPickler(f, protocol=-1)
strip_pickler.dump(m)
7 changes: 4 additions & 3 deletions tests/scan/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pickle
import shutil
import sys
from pathlib import Path
from tempfile import mkdtemp

import numpy as np
Expand Down Expand Up @@ -327,15 +328,15 @@ def f_pow2(x_tm1):
[state, n_steps], output, updates=updates, allow_input_downcast=True
)

origdir = os.getcwd()
origdir = Path.cwd()
tmpdir = None
try:
tmpdir = mkdtemp()
os.chdir(tmpdir)

with open("tmp_scan_test_pickle.pkl", "wb") as f_out:
with Path("tmp_scan_test_pickle.pkl").open("wb") as f_out:
pickle.dump(_my_f, f_out, protocol=-1)
with open("tmp_scan_test_pickle.pkl", "rb") as f_in:
with Path("tmp_scan_test_pickle.pkl").open("rb") as f_in:
my_f = pickle.load(f_in)
finally:
# Get back to the original dir, and delete the temporary one.
Expand Down
3 changes: 2 additions & 1 deletion tests/tensor/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from copy import copy
from itertools import combinations
from pathlib import Path
from tempfile import mkstemp

import numpy as np
Expand Down Expand Up @@ -442,7 +443,7 @@ def teardown_method(self):
gc.collect()
for f, fname in self.tmp_files:
os.close(f)
os.remove(fname)
Path(fname).unlink()

@pytest.mark.skipif(skip, reason="Skipped")
def test_good(self):
Expand Down

0 comments on commit a99d067

Please sign in to comment.