Skip to content
Merged
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
Merge branch 'main' into gh-102613-remove-selector-classes
  • Loading branch information
barneygale committed May 29, 2023
commit 6da6a83f779126fdfa20072097b736feb7c6d50a
24 changes: 14 additions & 10 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ def _compile_pattern(pat, case_sensitive):
return re.compile(fnmatch.translate(pat), flags).match


def _select_children(paths, dir_only, match):
def _select_children(paths, dir_only, follow_symlinks, match):
"""Yield direct children of given paths, filtering by name and type."""
if follow_symlinks is None:
follow_symlinks = True
for path in paths:
try:
# We must close the scandir() object before proceeding to
Expand All @@ -86,7 +88,7 @@ def _select_children(paths, dir_only, match):
for entry in entries:
if dir_only:
try:
if not entry.is_dir(follow_symlinks=follow_dirlinks):
if not entry.is_dir(follow_symlinks=follow_symlinks):
continue
except OSError:
continue
Expand All @@ -95,11 +97,13 @@ def _select_children(paths, dir_only, match):
yield path._make_child_relpath(name)


def _select_recursive(paths):
"""Yield given paths and all their subdirectories, recursively."""
def _select_recursive(paths, follow_symlinks):
"""Yield given paths and all their subdiretories, recursively."""
if follow_symlinks is None:
follow_symlinks = False
for path in paths:
yield path
for dirpath, dirnames, _ in path.walk():
for dirpath, dirnames, _ in path.walk(follow_symlinks=follow_symlinks):
for dirname in dirnames:
yield dirpath._make_child_relpath(dirname)

Expand Down Expand Up @@ -923,7 +927,7 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
pattern_parts.append('')
return self._glob(pattern_parts, case_sensitive)
return self._glob(pattern_parts, case_sensitive, follow_symlinks)

def rglob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
"""Recursively yield all existing files (of any kind, including
Expand All @@ -936,9 +940,9 @@ def rglob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern and pattern[-1] in (self._flavour.sep, self._flavour.altsep):
pattern_parts.append('')
return self._glob(['**'] + pattern_parts, case_sensitive)
return self._glob(['**'] + pattern_parts, case_sensitive, follow_symlinks)

def _glob(self, pattern_parts, case_sensitive):
def _glob(self, pattern_parts, case_sensitive, follow_symlinks):
if case_sensitive is None:
# TODO: evaluate case-sensitivity of each directory in _select_children().
case_sensitive = _is_case_sensitive(self._flavour)
Expand All @@ -957,7 +961,7 @@ def _glob(self, pattern_parts, case_sensitive):
# Collapse adjacent '**' segments.
while part_idx < len(pattern_parts) and pattern_parts[part_idx] == '**':
part_idx += 1
paths = _select_recursive(paths)
paths = _select_recursive(paths, follow_symlinks)
# De-duplicate if we've already seen a '**' segment.
if deduplicate:
paths = _select_unique(paths)
Expand All @@ -967,7 +971,7 @@ def _glob(self, pattern_parts, case_sensitive):
else:
dir_only = part_idx < len(pattern_parts)
match = _compile_pattern(part, case_sensitive)
paths = _select_children(paths, dir_only, match)
paths = _select_children(paths, dir_only, follow_symlinks, match)
return paths

def walk(self, top_down=True, on_error=None, follow_symlinks=False):
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.