Merge pull request #7839 from asottile/py36_compat_fspath

py36+: remove _pytest.compat.fspath
This commit is contained in:
Anthony Sottile 2020-10-03 07:42:54 -07:00 committed by GitHub
commit c2a197f351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 21 deletions

View File

@ -33,7 +33,6 @@ from _pytest.assertion import util
from _pytest.assertion.util import ( # noqa: F401 from _pytest.assertion.util import ( # noqa: F401
format_explanation as _format_explanation, format_explanation as _format_explanation,
) )
from _pytest.compat import fspath
from _pytest.config import Config from _pytest.config import Config
from _pytest.main import Session from _pytest.main import Session
from _pytest.pathlib import fnmatch_ex from _pytest.pathlib import fnmatch_ex
@ -306,7 +305,7 @@ if sys.platform == "win32":
pyc: Path, pyc: Path,
) -> bool: ) -> bool:
try: try:
with atomic_write(fspath(pyc), mode="wb", overwrite=True) as fp: with atomic_write(os.fspath(pyc), mode="wb", overwrite=True) as fp:
_write_pyc_fp(fp, source_stat, co) _write_pyc_fp(fp, source_stat, co)
except OSError as e: except OSError as e:
state.trace("error writing pyc file at {}: {}".format(pyc, e)) state.trace("error writing pyc file at {}: {}".format(pyc, e))
@ -336,7 +335,7 @@ else:
try: try:
_write_pyc_fp(fp, source_stat, co) _write_pyc_fp(fp, source_stat, co)
os.rename(proc_pyc, fspath(pyc)) os.rename(proc_pyc, os.fspath(pyc))
except OSError as e: except OSError as e:
state.trace("error writing pyc file at {}: {}".format(pyc, e)) state.trace("error writing pyc file at {}: {}".format(pyc, e))
# we ignore any failure to write the cache file # we ignore any failure to write the cache file
@ -350,7 +349,7 @@ 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_ = fspath(fn) fn_ = os.fspath(fn)
stat = os.stat(fn_) stat = os.stat(fn_)
with open(fn_, "rb") as f: with open(fn_, "rb") as f:
source = f.read() source = f.read()
@ -368,12 +367,12 @@ 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(fspath(pyc), "rb") fp = open(os.fspath(pyc), "rb")
except OSError: except OSError:
return None return None
with fp: with fp:
try: try:
stat_result = os.stat(fspath(source)) stat_result = os.stat(os.fspath(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(12) data = fp.read(12)
@ -831,7 +830,7 @@ class AssertionRewriter(ast.NodeVisitor):
"assertion is always true, perhaps remove parentheses?" "assertion is always true, perhaps remove parentheses?"
), ),
category=None, category=None,
filename=fspath(self.module_path), filename=os.fspath(self.module_path),
lineno=assert_.lineno, lineno=assert_.lineno,
) )
@ -1072,7 +1071,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(fspath(cache_dir), exist_ok=True) os.makedirs(os.fspath(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

@ -2,7 +2,6 @@
import enum import enum
import functools import functools
import inspect import inspect
import os
import re import re
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
@ -55,18 +54,6 @@ def _format_args(func: Callable[..., Any]) -> str:
REGEX_TYPE = type(re.compile("")) REGEX_TYPE = type(re.compile(""))
if sys.version_info < (3, 6):
def fspath(p):
"""os.fspath replacement, useful to point out when we should replace it by the
real function once we drop py35."""
return str(p)
else:
fspath = os.fspath
def is_generator(func: object) -> bool: def is_generator(func: object) -> bool:
genfunc = inspect.isgeneratorfunction(func) genfunc = inspect.isgeneratorfunction(func)
return genfunc and not iscoroutinefunction(func) return genfunc and not iscoroutinefunction(func)