Skip to content
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
Next Next commit
Optimize pathlib.Path.glob() handling of ../ pattern segments
These segments do not require a `stat()` call, as the selector's
`_select_from()` method is called after we've established that the
parent is a directory.
  • Loading branch information
barneygale committed May 2, 2023
commit f268157234eb4833b0781ad56677c35f7af8383f
12 changes: 12 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def _make_selector(pattern_parts, flavour):
return _TerminatingSelector()
if pat == '**':
cls = _RecursiveWildcardSelector
elif pat == '..':
cls = _ParentSelector
elif '**' in pat:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
elif _is_wildcard_pattern(pat):
Expand Down Expand Up @@ -112,6 +114,16 @@ def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
yield parent_path


class _ParentSelector(_Selector):
def __init__(self, name, child_parts, case_sensitive):
_Selector.__init__(self, child_parts, case_sensitive)

def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
path = parent_path._make_child_relpath('..')
for p in self.successor._select_from(path, is_dir, exists, scandir, normcase):
yield p


class _PreciseSelector(_Selector):

def __init__(self, name, child_parts, flavour):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve performance of :meth:`pathlib.Path.glob` when evaluating patterns
that contain ``'../'`` segments.