Skip to content

Commit c284726

Browse files
barneygaleaisk
authored andcommitted
pythonGH-113225: Speed up pathlib.Path.glob() (python#113226)
Use `os.DirEntry.path` as the string representation of child paths, unless the parent path is empty, in which case we use the entry `name`.
1 parent 8c17f66 commit c284726

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Lib/pathlib/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,13 @@ def _scandir(self):
301301

302302
def _make_child_entry(self, entry):
303303
# Transform an entry yielded from _scandir() into a path object.
304-
return self._make_child_relpath(entry.name)
304+
path_str = entry.name if str(self) == '.' else entry.path
305+
path = self.with_segments(path_str)
306+
path._str = path_str
307+
path._drv = self.drive
308+
path._root = self.root
309+
path._tail_cached = self._tail + [entry.name]
310+
return path
305311

306312
def absolute(self):
307313
"""Return an absolute version of this path
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up :meth:`pathlib.Path.glob` by using :attr:`os.DirEntry.path` where possible.

0 commit comments

Comments
 (0)