Merge pull request #9183 from bluetech/rm-redundent-osfspath

Remove redundant explicit os.fspath calls
This commit is contained in:
Ran Benita 2021-10-09 19:14:55 +03:00 committed by GitHub
commit 14a879b6d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 17 deletions

View File

@ -342,7 +342,7 @@ else:
try:
_write_pyc_fp(fp, source_stat, co)
os.rename(proc_pyc, os.fspath(pyc))
os.rename(proc_pyc, pyc)
except OSError as e:
state.trace(f"error writing pyc file at {pyc}: {e}")
# we ignore any failure to write the cache file
@ -356,13 +356,12 @@ else:
def _rewrite_test(fn: Path, config: Config) -> Tuple[os.stat_result, types.CodeType]:
"""Read and rewrite *fn* and return the code object."""
fn_ = os.fspath(fn)
stat = os.stat(fn_)
with open(fn_, "rb") as f:
source = f.read()
tree = ast.parse(source, filename=fn_)
rewrite_asserts(tree, source, fn_, config)
co = compile(tree, fn_, "exec", dont_inherit=True)
stat = os.stat(fn)
source = fn.read_bytes()
strfn = str(fn)
tree = ast.parse(source, filename=strfn)
rewrite_asserts(tree, source, strfn, config)
co = compile(tree, strfn, "exec", dont_inherit=True)
return stat, co
@ -374,14 +373,14 @@ def _read_pyc(
Return rewritten code if successful or None if not.
"""
try:
fp = open(os.fspath(pyc), "rb")
fp = open(pyc, "rb")
except OSError:
return None
with fp:
# https://www.python.org/dev/peps/pep-0552/
has_flags = sys.version_info >= (3, 7)
try:
stat_result = os.stat(os.fspath(source))
stat_result = os.stat(source)
mtime = int(stat_result.st_mtime)
size = stat_result.st_size
data = fp.read(16 if has_flags else 12)
@ -863,7 +862,7 @@ class AssertionRewriter(ast.NodeVisitor):
"assertion is always true, perhaps remove parentheses?"
),
category=None,
filename=os.fspath(self.module_path),
filename=self.module_path,
lineno=assert_.lineno,
)
@ -1105,7 +1104,7 @@ def try_makedirs(cache_dir: Path) -> bool:
Returns True if successful or if it already exists.
"""
try:
os.makedirs(os.fspath(cache_dir), exist_ok=True)
os.makedirs(cache_dir, exist_ok=True)
except (FileNotFoundError, NotADirectoryError, FileExistsError):
# One of the path components was not a directory:
# - we're in a zip file

View File

@ -1372,9 +1372,7 @@ class Pytester:
"""
__tracebackhide__ = True
cmdargs = tuple(
os.fspath(arg) if isinstance(arg, os.PathLike) else arg for arg in cmdargs
)
cmdargs = tuple(os.fspath(arg) for arg in cmdargs)
p1 = self.path.joinpath("stdout")
p2 = self.path.joinpath("stderr")
print("running:", *cmdargs)

View File

@ -1681,7 +1681,7 @@ def test_try_makedirs(monkeypatch, tmp_path: Path) -> None:
# monkeypatch to simulate all error situations
def fake_mkdir(p, exist_ok=False, *, exc):
assert isinstance(p, str)
assert isinstance(p, Path)
raise exc
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=FileNotFoundError()))

View File

@ -595,7 +595,7 @@ class TestConfigAPI:
def test_getconftest_pathlist(self, pytester: Pytester, tmp_path: Path) -> None:
somepath = tmp_path.joinpath("x", "y", "z")
p = tmp_path.joinpath("conftest.py")
p.write_text(f"mylist = {['.', os.fspath(somepath)]}")
p.write_text(f"mylist = {['.', str(somepath)]}")
config = pytester.parseconfigure(p)
assert (
config._getconftest_pathlist("notexist", path=tmp_path, rootpath=tmp_path)