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:
@@ -560,14 +560,14 @@ def resolve_package_path(path: Path) -> Optional[Path]:
|
||||
|
||||
|
||||
def visit(
|
||||
path: py.path.local, recurse: Callable[[py.path.local], bool],
|
||||
) -> Iterator[py.path.local]:
|
||||
"""Walk path recursively, in breadth-first order.
|
||||
path: str, recurse: Callable[["os.DirEntry[str]"], bool]
|
||||
) -> Iterator["os.DirEntry[str]"]:
|
||||
"""Walk a directory recursively, in breadth-first order.
|
||||
|
||||
Entries at each directory level are sorted.
|
||||
"""
|
||||
entries = sorted(path.listdir())
|
||||
entries = sorted(os.scandir(path), key=lambda entry: entry.name)
|
||||
yield from entries
|
||||
for entry in entries:
|
||||
if entry.check(dir=1) and recurse(entry):
|
||||
yield from visit(entry, recurse)
|
||||
if entry.is_dir(follow_symlinks=False) and recurse(entry):
|
||||
yield from visit(entry.path, recurse)
|
||||
|
||||
Reference in New Issue
Block a user