8000 GH-117586: Speed up `pathlib.Path.glob()` by working with strings by barneygale · Pull Request #117589 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-117586: Speed up pathlib.Path.glob() by working with strings #117589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix handling of missing root path.
  • Loading branch information
barneygale committed Apr 6, 2024
commit d6314ac9af650bf3b072c75248887b6ddf8c836f
2 changes: 2 additions & 0 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=False):
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
parts.append('')
parts.reverse()
if not self.is_dir():
return iter([])
select = self._glob_selector(parts, case_sensitive, recurse_symlinks)
return map(self.with_segments, select(str(self), exists=True))

Expand Down
4 changes: 2 additions & 2 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,6 @@ def _make_child_relpath(self, name):
return self.joinpath(name)

def _glob_selector(self, parts, case_sensitive, recurse_symlinks):
if not self.is_dir():
return iter([])
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser)
recursive = True if recurse_symlinks else glob._no_recurse_symlinks
Expand All @@ -707,6 +705,8 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
anchor, parts = pattern._stack
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
if not self.is_dir():
return iter([])
select = self._glob_selector(parts, case_sensitive, recurse_symlinks)
return select(self, exists=True)

Expand Down
0