Skip to content

Commit 8af5587

Browse files
authored
main: avoid Path(Path(...)) calls, they're slow (#9147)
2 parents 5fc3e35 + c86ceb4 commit 8af5587

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/_pytest/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,18 @@ def pytest_runtest_logreport(
538538
pytest_collectreport = pytest_runtest_logreport
539539

540540
def isinitpath(self, path: Union[str, "os.PathLike[str]"]) -> bool:
541-
return Path(path) in self._initialpaths
541+
# Optimization: Path(Path(...)) is much slower than isinstance.
542+
path_ = path if isinstance(path, Path) else Path(path)
543+
return path_ in self._initialpaths
542544

543545
def gethookproxy(self, fspath: "os.PathLike[str]"):
546+
# Optimization: Path(Path(...)) is much slower than isinstance.
547+
path = fspath if isinstance(fspath, Path) else Path(fspath)
548+
pm = self.config.pluginmanager
544549
# Check if we have the common case of running
545550
# hooks with all conftest.py files.
546-
pm = self.config.pluginmanager
547551
my_conftestmodules = pm._getconftestmodules(
548-
Path(fspath),
552+
path,
549553
self.config.getoption("importmode"),
550554
rootpath=self.config.rootpath,
551555
)

0 commit comments

Comments
 (0)