code: convert from py.path to pathlib

This commit is contained in:
Ran Benita
2020-12-19 14:11:00 +02:00
parent 7aa2240832
commit 92ba96b061
7 changed files with 90 additions and 74 deletions

View File

@@ -43,6 +43,8 @@ from _pytest._io.saferepr import safeformat
from _pytest._io.saferepr import saferepr
from _pytest.compat import final
from _pytest.compat import get_real_func
from _pytest.pathlib import absolutepath
from _pytest.pathlib import bestrelpath
if TYPE_CHECKING:
from typing_extensions import Literal
@@ -78,16 +80,16 @@ class Code:
return self.raw.co_name
@property
def path(self) -> Union[py.path.local, str]:
def path(self) -> Union[Path, str]:
"""Return a path object pointing to source code, or an ``str`` in
case of ``OSError`` / non-existing file."""
if not self.raw.co_filename:
return ""
try:
p = py.path.local(self.raw.co_filename)
p = absolutepath(self.raw.co_filename)
# maybe don't try this checking
if not p.check():
raise OSError("py.path check failed.")
if not p.exists():
raise OSError("path check failed.")
return p
except OSError:
# XXX maybe try harder like the weird logic
@@ -223,7 +225,7 @@ class TracebackEntry:
return source.getstatement(self.lineno)
@property
def path(self) -> Union[py.path.local, str]:
def path(self) -> Union[Path, str]:
"""Path to the source code."""
return self.frame.code.path
@@ -336,10 +338,10 @@ class Traceback(List[TracebackEntry]):
def cut(
self,
path=None,
path: Optional[Union[Path, str]] = None,
lineno: Optional[int] = None,
firstlineno: Optional[int] = None,
excludepath: Optional[py.path.local] = None,
excludepath: Optional[Path] = None,
) -> "Traceback":
"""Return a Traceback instance wrapping part of this Traceback.
@@ -353,17 +355,19 @@ class Traceback(List[TracebackEntry]):
for x in self:
code = x.frame.code
codepath = code.path
if path is not None and codepath != path:
continue
if (
(path is None or codepath == path)
and (
excludepath is None
or not isinstance(codepath, py.path.local)
or not codepath.relto(excludepath)
)
and (lineno is None or x.lineno == lineno)
and (firstlineno is None or x.frame.code.firstlineno == firstlineno)
excludepath is not None
and isinstance(codepath, Path)
and excludepath in codepath.parents
):
return Traceback(x._rawentry, self._excinfo)
continue
if lineno is not None and x.lineno != lineno:
continue
if firstlineno is not None and x.frame.code.firstlineno != firstlineno:
continue
return Traceback(x._rawentry, self._excinfo)
return self
@overload
@@ -801,7 +805,8 @@ class FormattedExcinfo:
message = "in %s" % (entry.name)
else:
message = excinfo and excinfo.typename or ""
path = self._makepath(entry.path)
entry_path = entry.path
path = self._makepath(entry_path)
reprfileloc = ReprFileLocation(path, entry.lineno + 1, message)
localsrepr = self.repr_locals(entry.locals)
return ReprEntry(lines, reprargs, localsrepr, reprfileloc, style)
@@ -814,15 +819,15 @@ class FormattedExcinfo:
lines.extend(self.get_exconly(excinfo, indent=4))
return ReprEntry(lines, None, None, None, style)
def _makepath(self, path):
if not self.abspath:
def _makepath(self, path: Union[Path, str]) -> str:
if not self.abspath and isinstance(path, Path):
try:
np = py.path.local().bestrelpath(path)
np = bestrelpath(Path.cwd(), path)
except OSError:
return path
return str(path)
if len(np) < len(str(path)):
path = np
return path
return np
return str(path)
def repr_traceback(self, excinfo: ExceptionInfo[BaseException]) -> "ReprTraceback":
traceback = excinfo.traceback
@@ -1181,7 +1186,7 @@ class ReprFuncArgs(TerminalRepr):
tw.line("")
def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]:
def getfslineno(obj: object) -> Tuple[Union[str, Path], int]:
"""Return source location (path, lineno) for the given object.
If the source cannot be determined return ("", -1).
@@ -1203,7 +1208,7 @@ def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]:
except TypeError:
return "", -1
fspath = fn and py.path.local(fn) or ""
fspath = fn and absolutepath(fn) or ""
lineno = -1
if fspath:
try: