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

View File

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

View File

@ -1681,7 +1681,7 @@ def test_try_makedirs(monkeypatch, tmp_path: Path) -> None:
# monkeypatch to simulate all error situations # monkeypatch to simulate all error situations
def fake_mkdir(p, exist_ok=False, *, exc): def fake_mkdir(p, exist_ok=False, *, exc):
assert isinstance(p, str) assert isinstance(p, Path)
raise exc raise exc
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=FileNotFoundError())) 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: def test_getconftest_pathlist(self, pytester: Pytester, tmp_path: Path) -> None:
somepath = tmp_path.joinpath("x", "y", "z") somepath = tmp_path.joinpath("x", "y", "z")
p = tmp_path.joinpath("conftest.py") 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) config = pytester.parseconfigure(p)
assert ( assert (
config._getconftest_pathlist("notexist", path=tmp_path, rootpath=tmp_path) config._getconftest_pathlist("notexist", path=tmp_path, rootpath=tmp_path)