Skip to content

Commit

Permalink
pythonGH-113225: Speed up pathlib._abc.PathBase.glob()
Browse files Browse the repository at this point in the history
`PathBase._scandir()` is implemented using `iterdir()`, so we can use its
results directly, rather than passing them through `_make_child_relpath()`.
  • Loading branch information
barneygale committed Dec 28, 2023
1 parent 1b19d73 commit 319689f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ def iterdir(self):
def _scandir(self):
return os.scandir(self)

def _make_child_entry(self, entry):
# Transform an entry yielded from _scandir() into a path object.
return self._make_child_relpath(entry.name)

def absolute(self):
"""Return an absolute version of this path
No normalization or symlink resolution is performed.
Expand Down
13 changes: 8 additions & 5 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ def _select_children(parent_paths, dir_only, follow_symlinks, match):
continue
except OSError:
continue
name = entry.name
if match(name):
yield parent_path._make_child_relpath(name)
if match(entry.name):
yield parent_path._make_child_entry(entry)


def _select_recursive(parent_paths, dir_only, follow_symlinks):
Expand All @@ -112,12 +111,12 @@ def _select_recursive(parent_paths, dir_only, follow_symlinks):
for entry in entries:
try:
if entry.is_dir(follow_symlinks=follow_symlinks):
paths.append(path._make_child_relpath(entry.name))
paths.append(path._make_child_entry(entry))
continue
except OSError:
pass
if not dir_only:
yield path._make_child_relpath(entry.name)
yield path._make_child_entry(entry)


def _select_unique(paths):
Expand Down Expand Up @@ -788,6 +787,10 @@ def _scandir(self):
from contextlib import nullcontext
return nullcontext(self.iterdir())

def _make_child_entry(self, entry):
# Transform an entry yielded from _scandir() into a path object.
return entry

def _make_child_relpath(self, name):
path_str = str(self)
tail = self._tail
Expand Down

0 comments on commit 319689f

Please sign in to comment.