提交 a99d0670 authored 作者: Virgile Andreani's avatar Virgile Andreani 提交者: Ricardo Vieira

Replace more os.path with pathlib

上级 1bdbddfe
...@@ -125,7 +125,7 @@ line-length = 88 ...@@ -125,7 +125,7 @@ line-length = 88
exclude = ["doc/", "pytensor/_version.py"] exclude = ["doc/", "pytensor/_version.py"]
[tool.ruff.lint] [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"] ignore = ["C408", "C901", "E501", "E741", "RUF012", "PERF203"]
...@@ -136,6 +136,7 @@ lines-after-imports = 2 ...@@ -136,6 +136,7 @@ lines-after-imports = 2
# TODO: Get rid of these: # TODO: Get rid of these:
"**/__init__.py" = ["F401", "E402", "F403"] "**/__init__.py" = ["F401", "E402", "F403"]
"pytensor/tensor/linalg.py" = ["F403"] "pytensor/tensor/linalg.py" = ["F403"]
"pytensor/link/c/cmodule.py" = ["PTH"]
# For the tests we skip because `pytest.importorskip` is used: # For the tests we skip because `pytest.importorskip` is used:
"tests/link/jax/test_scalar.py" = ["E402"] "tests/link/jax/test_scalar.py" = ["E402"]
"tests/link/jax/test_tensor_basic.py" = ["E402"] "tests/link/jax/test_tensor_basic.py" = ["E402"]
......
import errno
import logging import logging
import os
import sys import sys
import warnings import warnings
from importlib import reload from importlib import reload
...@@ -43,23 +41,12 @@ try: ...@@ -43,23 +41,12 @@ try:
# compile_str()) but if another lazylinker_ext does exist then it will be # compile_str()) but if another lazylinker_ext does exist then it will be
# imported and compile_str won't get called at all. # imported and compile_str won't get called at all.
location = config.compiledir / "lazylinker_ext" location = config.compiledir / "lazylinker_ext"
if not location.exists(): location.mkdir(exist_ok=True)
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()
init_file = location / "__init__.py" init_file = location / "__init__.py"
if not init_file.exists(): if not init_file.exists():
try: try:
with open(init_file, "w"): with init_file.open("w"):
pass pass
except OSError as e: except OSError as e:
if init_file.exists(): if init_file.exists():
...@@ -129,12 +116,7 @@ except ImportError: ...@@ -129,12 +116,7 @@ except ImportError:
code = cfile.read_text("utf-8") code = cfile.read_text("utf-8")
loc = config.compiledir / dirname loc = config.compiledir / dirname
if not loc.exists(): loc.mkdir(exist_ok=True)
try:
os.mkdir(loc)
except OSError as e:
assert e.errno == errno.EEXIST
assert loc.exists()
args = GCC_compiler.compile_args() args = GCC_compiler.compile_args()
GCC_compiler.compile_str(dirname, code, location=loc, preargs=args) GCC_compiler.compile_str(dirname, code, location=loc, preargs=args)
...@@ -147,8 +129,7 @@ except ImportError: ...@@ -147,8 +129,7 @@ except ImportError:
# imported at the same time: we need to make sure we do not # imported at the same time: we need to make sure we do not
# reload the now outdated __init__.pyc below. # reload the now outdated __init__.pyc below.
init_pyc = loc / "__init__.pyc" init_pyc = loc / "__init__.pyc"
if init_pyc.is_file(): init_pyc.unlink(missing_ok=True)
os.remove(init_pyc)
try_import() try_import()
try_reload() try_reload()
......
...@@ -156,7 +156,7 @@ def subprocess_Popen(command: str | list[str], **params): ...@@ -156,7 +156,7 @@ def subprocess_Popen(command: str | list[str], **params):
# with the default None values. # with the default None values.
stdin = None stdin = None
if "stdin" not in params: if "stdin" not in params:
stdin = open(os.devnull) stdin = Path(os.devnull).open()
params["stdin"] = stdin.fileno() params["stdin"] = stdin.fileno()
try: try:
......
import os import os
import shutil import shutil
from pathlib import Path
from tempfile import mkdtemp from tempfile import mkdtemp
from pytensor.misc.pkl_utils import StripPickler from pytensor.misc.pkl_utils import StripPickler
from pytensor.tensor.type import matrix from pytensor.tensor.type import matrix
# FIXME: this test looks weird
class TestStripPickler: class TestStripPickler:
def setup_method(self): def setup_method(self):
# Work in a temporary directory to avoid cluttering the repository # Work in a temporary directory to avoid cluttering the repository
self.origdir = os.getcwd() self.origdir = Path.cwd()
self.tmpdir = mkdtemp() self.tmpdir = mkdtemp()
os.chdir(self.tmpdir) os.chdir(self.tmpdir)
...@@ -20,9 +22,9 @@ class TestStripPickler: ...@@ -20,9 +22,9 @@ class TestStripPickler:
shutil.rmtree(self.tmpdir) shutil.rmtree(self.tmpdir)
def test_basic(self): def test_basic(self):
with open("test.pkl", "wb") as f: with Path("test.pkl").open("wb"):
m = matrix() m = matrix()
dest_pkl = "my_test.pkl" 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 = StripPickler(f, protocol=-1)
strip_pickler.dump(m) strip_pickler.dump(m)
...@@ -13,6 +13,7 @@ import os ...@@ -13,6 +13,7 @@ import os
import pickle import pickle
import shutil import shutil
import sys import sys
from pathlib import Path
from tempfile import mkdtemp from tempfile import mkdtemp
import numpy as np import numpy as np
...@@ -327,15 +328,15 @@ class TestScan: ...@@ -327,15 +328,15 @@ class TestScan:
[state, n_steps], output, updates=updates, allow_input_downcast=True [state, n_steps], output, updates=updates, allow_input_downcast=True
) )
origdir = os.getcwd() origdir = Path.cwd()
tmpdir = None tmpdir = None
try: try:
tmpdir = mkdtemp() tmpdir = mkdtemp()
os.chdir(tmpdir) 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) 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) my_f = pickle.load(f_in)
finally: finally:
# Get back to the original dir, and delete the temporary one. # Get back to the original dir, and delete the temporary one.
......
import os import os
from copy import copy from copy import copy
from itertools import combinations from itertools import combinations
from pathlib import Path
from tempfile import mkstemp from tempfile import mkstemp
import numpy as np import numpy as np
...@@ -442,7 +443,7 @@ def makeTester( ...@@ -442,7 +443,7 @@ def makeTester(
gc.collect() gc.collect()
for f, fname in self.tmp_files: for f, fname in self.tmp_files:
os.close(f) os.close(f)
os.remove(fname) Path(fname).unlink()
@pytest.mark.skipif(skip, reason="Skipped") @pytest.mark.skipif(skip, reason="Skipped")
def test_good(self): def test_good(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论