pathlib: make visit() independent of py.path.local, use os.scandir

`os.scandir()`, introduced in Python 3.5, is much faster than
`os.listdir()`. See https://www.python.org/dev/peps/pep-0471/.

It also has a `DirEntry` which can be used to further reduce syscalls in
some cases.
This commit is contained in:
Ran Benita
2020-07-05 23:11:47 +03:00
parent c15bb5d3de
commit 3633b691d8
4 changed files with 26 additions and 22 deletions

View File

@@ -642,23 +642,24 @@ class Package(Module):
):
yield Module.from_parent(self, fspath=init_module)
pkg_prefixes = set() # type: Set[py.path.local]
for path in visit(this_path, recurse=self._recurse):
for direntry in visit(str(this_path), recurse=self._recurse):
path = py.path.local(direntry.path)
# We will visit our own __init__.py file, in which case we skip it.
is_file = path.isfile()
if is_file:
if path.basename == "__init__.py" and path.dirpath() == this_path:
if direntry.is_file():
if direntry.name == "__init__.py" and path.dirpath() == this_path:
continue
parts_ = parts(path.strpath)
parts_ = parts(direntry.path)
if any(
str(pkg_prefix) in parts_ and pkg_prefix.join("__init__.py") != path
for pkg_prefix in pkg_prefixes
):
continue
if is_file:
if direntry.is_file():
yield from self._collectfile(path)
elif not path.isdir():
elif not direntry.is_dir():
# Broken symlink or invalid/missing file.
continue
elif path.join("__init__.py").check(file=1):