diff options
author | Barney Gale <barney.gale@gmail.com> | 2023-12-28 22:23:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-28 22:23:01 (GMT) |
commit | b664d9159964f0609d50dabd02f71af0227d8718 (patch) | |
tree | 0610319a5087fec881ea54ddc8121ab90c0d664d | |
parent | db1c88223986efe3076eb3b229a8b6db59bae284 (diff) | |
download | cpython-b664d9159964f0609d50dabd02f71af0227d8718.zip cpython-b664d9159964f0609d50dabd02f71af0227d8718.tar.gz cpython-b664d9159964f0609d50dabd02f71af0227d8718.tar.bz2 |
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()`.
-rw-r--r-- | Lib/pathlib/__init__.py | 4 | ||||
-rw-r--r-- | Lib/pathlib/_abc.py | 13 |
2 files changed, 12 insertions, 5 deletions
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index ab87b49..2b4193c 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -299,6 +299,10 @@ class Path(_abc.PathBase, PurePath): 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. diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index efe56ec..f75b20a 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -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): @@ -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): @@ -788,6 +787,10 @@ class PathBase(PurePathBase): 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 |