summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-01-04 20:48:26 (GMT)
committerGitHub <noreply@github.com>2024-01-04 20:48:26 (GMT)
commitc2e8298eba3f8d75a58e5b3636f8edc8d60e68da (patch)
tree5b182276045c17295f0c7218870aca6833ab8ca0
parent4681a5271a8598b46021cbc556ac8098ab8a1d81 (diff)
downloadcpython-c2e8298eba3f8d75a58e5b3636f8edc8d60e68da.zip
cpython-c2e8298eba3f8d75a58e5b3636f8edc8d60e68da.tar.gz
cpython-c2e8298eba3f8d75a58e5b3636f8edc8d60e68da.tar.bz2
GH-113225: Speed up `pathlib.Path.glob()` (#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`.
-rw-r--r--Lib/pathlib/__init__.py8
-rw-r--r--Misc/NEWS.d/next/Library/2023-12-17-04-43-57.gh-issue-113225.dhxhiZ.rst1
2 files changed, 8 insertions, 1 deletions
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py
index 2b4193c..79b8b49 100644
--- a/Lib/pathlib/__init__.py
+++ b/Lib/pathlib/__init__.py
@@ -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
index 0000000..7160cca
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-17-04-43-57.gh-issue-113225.dhxhiZ.rst
@@ -0,0 +1 @@
+Speed up :meth:`pathlib.Path.glob` by using :attr:`os.DirEntry.path` where possible.