Skip to content

Commit b664d91

Browse files
authored
GH-113225: Speed up pathlib._abc.PathBase.glob() (#113556)
`PathBase._scandir()` is implemented using `iterdir()`, so we can use its results directly, rather than passing them through `_make_child_relpath()`.
1 parent db1c882 commit b664d91

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Lib/pathlib/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ def iterdir(self):
299299
def _scandir(self):
300300
return os.scandir(self)
301301

302+
def _make_child_entry(self, entry):
303+
# Transform an entry yielded from _scandir() into a path object.
304+
return self._make_child_relpath(entry.name)
305+
302306
def absolute(self):
303307
"""Return an absolute version of this path
304308
No normalization or symlink resolution is performed.

Lib/pathlib/_abc.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ def _select_children(parent_paths, dir_only, follow_symlinks, match):
8787
continue
8888
except OSError:
8989
continue
90-
name = entry.name
91-
if match(name):
92-
yield parent_path._make_child_relpath(name)
90+
if match(entry.name):
91+
yield parent_path._make_child_entry(entry)
9392

9493

9594
def _select_recursive(parent_paths, dir_only, follow_symlinks):
@@ -112,12 +111,12 @@ def _select_recursive(parent_paths, dir_only, follow_symlinks):
112111
for entry in entries:
113112
try:
114113
if entry.is_dir(follow_symlinks=follow_symlinks):
115-
paths.append(path._make_child_relpath(entry.name))
114+
paths.append(path._make_child_entry(entry))
116115
continue
117116
except OSError:
118117
pass
119118
if not dir_only:
120-
yield path._make_child_relpath(entry.name)
119+
yield path._make_child_entry(entry)
121120

122121

123122
def _select_unique(paths):
@@ -788,6 +787,10 @@ def _scandir(self):
788787
from contextlib import nullcontext
789788
return nullcontext(self.iterdir())
790789

790+
def _make_child_entry(self, entry):
791+
# Transform an entry yielded from _scandir() into a path object.
792+
return entry
793+
791794
def _make_child_relpath(self, name):
792795
path_str = str(self)
793796
tail = self._tail

0 commit comments

Comments
 (0)