GH-127807: pathlib ABCs: remove a few private attributes (#127851)

From `PurePathBase` delete `_globber`, `_stack` and `_pattern_str`, and
from `PathBase` delete `_glob_selector`. This helps avoid an unpleasant
surprise for a users who try to use these names.
This commit is contained in:
Barney Gale 2024-12-22 01:41:38 +00:00 committed by GitHub
parent a959ea1b0a
commit f5ba74b819
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 56 deletions

View file

@ -5,7 +5,7 @@ import os
import posixpath
import sys
from errno import EINVAL, EXDEV
from glob import _StringGlobber
from glob import _StringGlobber, _no_recurse_symlinks
from itertools import chain
from stat import S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
from _collections_abc import Sequence
@ -112,7 +112,6 @@ class PurePath(PurePathBase):
'_hash',
)
parser = os.path
_globber = _StringGlobber
def __new__(cls, *args, **kwargs):
"""Construct a PurePath from one or several strings and or existing
@ -513,13 +512,22 @@ class PurePath(PurePathBase):
from urllib.parse import quote_from_bytes
return prefix + quote_from_bytes(os.fsencode(path))
@property
def _pattern_str(self):
"""The path expressed as a string, for use in pattern-matching."""
def full_match(self, pattern, *, case_sensitive=None):
"""
Return True if this path matches the given glob-style pattern. The
pattern is matched against the entire path.
"""
if not isinstance(pattern, PurePathBase):
pattern = self.with_segments(pattern)
if case_sensitive is None:
case_sensitive = self.parser is posixpath
# The string representation of an empty path is a single dot ('.'). Empty
# paths shouldn't match wildcards, so we change it to the empty string.
path_str = str(self)
return '' if path_str == '.' else path_str
path = str(self) if self.parts else ''
pattern = str(pattern) if pattern.parts else ''
globber = _StringGlobber(self.parser.sep, case_sensitive, recursive=True)
return globber.compile(pattern)(path) is not None
# Subclassing os.PathLike makes isinstance() checks slower,
# which in turn makes Path construction slower. Register instead!
@ -749,8 +757,18 @@ class Path(PathBase, PurePath):
kind, including directories) matching the given relative pattern.
"""
sys.audit("pathlib.Path.glob", self, pattern)
if case_sensitive is None:
case_sensitive = self.parser is posixpath
case_pedantic = False
else:
# The user has expressed a case sensitivity choice, but we don't
# know the case sensitivity of the underlying filesystem, so we
# must use scandir() for everything, including non-wildcard parts.
case_pedantic = True
parts = self._parse_pattern(pattern)
select = self._glob_selector(parts[::-1], case_sensitive, recurse_symlinks)
recursive = True if recurse_symlinks else _no_recurse_symlinks
globber = _StringGlobber(self.parser.sep, case_sensitive, case_pedantic, recursive)
select = globber.selector(parts[::-1])
root = str(self)
paths = select(root)