GH-115060: Speed up pathlib.Path.glob() by omitting initial stat() (#117831)

Since 6258844c, paths that might not exist can be fed into pathlib's
globbing implementation, which will call `os.scandir()` / `os.lstat()` only
when strictly necessary. This allows us to drop an initial `self.is_dir()`
call, which saves a `stat()`.

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
Barney Gale 2024-04-14 00:08:03 +01:00 committed by GitHub
parent 3095d02642
commit a74f117dab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 20 additions and 10 deletions

View file

@ -705,10 +705,8 @@ class PathBase(PurePathBase):
anchor, parts = pattern._stack
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
if not self.is_dir():
return iter([])
select = self._glob_selector(parts, case_sensitive, recurse_symlinks)
return select(self, exists=True)
return select(self)
def rglob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
"""Recursively yield all existing files (of any kind, including