Skip to content

Commit

Permalink
Move validation into _pattern_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Jan 20, 2024
1 parent 980e4f3 commit 3e71a3e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 8 additions & 4 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,21 @@ def _pattern_stack(self):
"""Stack of path components, to be used with patterns in glob()."""
parts = self._tail.copy()
pattern = self._raw_path
if pattern.endswith('**'):
if self.anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
elif not parts:
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
elif pattern[-1] in (self.pathmod.sep, self.pathmod.altsep):
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
parts.append('')
elif parts[-1] == '**':
# GH-70303: '**' only matches directories. Add trailing slash.
warnings.warn(
"Pattern ending '**' will match files and directories in a "
"future Python release. Add a trailing slash to match only "
"directories and remove this warning.",
FutureWarning, 4)
parts.append('')
elif pattern[-1] in (self.pathmod.sep, self.pathmod.altsep):
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
parts.append('')
parts.reverse()
return parts

Expand Down
10 changes: 4 additions & 6 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ def is_absolute(self):
@property
def _pattern_stack(self):
"""Stack of path components, to be used with patterns in glob()."""
return self._stack[1]
anchor, parts = self._stack
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
return parts

def match(self, path_pattern, *, case_sensitive=None):
"""
Expand Down Expand Up @@ -733,11 +736,6 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
"""
if not isinstance(pattern, PurePathBase):
pattern = self.with_segments(pattern)
if pattern.anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
elif not pattern.parts:
raise ValueError("Unacceptable pattern: {!r}".format(pattern))

if case_sensitive is None:
# TODO: evaluate case-sensitivity of each directory in _select_children().
case_sensitive = _is_case_sensitive(self.pathmod)
Expand Down

0 comments on commit 3e71a3e

Please sign in to comment.