]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-113225: Speed up `pathlib.Path.glob()` (#113226)
authorBarney Gale <barney.gale@gmail.com>
Thu, 4 Jan 2024 20:48:26 +0000 (20:48 +0000)
committerGitHub <noreply@github.com>
Thu, 4 Jan 2024 20:48:26 +0000 (20:48 +0000)
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`.

Lib/pathlib/__init__.py
Misc/NEWS.d/next/Library/2023-12-17-04-43-57.gh-issue-113225.dhxhiZ.rst [new file with mode: 0644]

index 2b4193c400a0998fb7c6678902dc8be1fc5487c7..79b8b4917f6cc4bea2a20705ba9b7a5ac49b1df1 100644 (file)
@@ -301,7 +301,13 @@ class Path(_abc.PathBase, PurePath):
 
     def _make_child_entry(self, entry):
         # Transform an entry yielded from _scandir() into a path object.
-        return self._make_child_relpath(entry.name)
+        path_str = entry.name if str(self) == '.' else entry.path
+        path = self.with_segments(path_str)
+        path._str = path_str
+        path._drv = self.drive
+        path._root = self.root
+        path._tail_cached = self._tail + [entry.name]
+        return path
 
     def absolute(self):
         """Return an absolute version of this path
diff --git a/Misc/NEWS.d/next/Library/2023-12-17-04-43-57.gh-issue-113225.dhxhiZ.rst b/Misc/NEWS.d/next/Library/2023-12-17-04-43-57.gh-issue-113225.dhxhiZ.rst
new file mode 100644 (file)
index 0000000..7160cca
--- /dev/null
@@ -0,0 +1 @@
+Speed up :meth:`pathlib.Path.glob` by using :attr:`os.DirEntry.path` where possible.