Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-127456: pathlib ABCs: remove Parser.sep #127725

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 15 additions & 13 deletions 28 Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
resemble pathlib's PurePath and Path respectively.
"""

import functools
import operator
import posixpath
from errno import EINVAL
Expand All @@ -29,11 +28,6 @@ class UnsupportedOperation(NotImplementedError):
pass


@functools.cache
def _is_case_sensitive(parser):
return parser.normcase('Aa') == 'Aa'


class PathGlobber(_GlobberBase):
"""
Class providing shell-style globbing for path objects.
Expand Down Expand Up @@ -64,8 +58,16 @@ class PurePathBase:
'_raw_paths',
)
parser = posixpath
_sep = '/'
_case_sensitive = True
_globber = PathGlobber

def __init_subclass__(cls):
super().__init_subclass__()
if 'parser' in cls.__dict__:
cls._sep = cls.parser.join('a', 'a').strip('a')
cls._case_sensitive = cls.parser.normcase('Aa') == 'Aa'

def __init__(self, *args):
for arg in args:
if not isinstance(arg, str):
Expand Down Expand Up @@ -100,7 +102,7 @@ def __str__(self):
def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
return str(self).replace(self.parser.sep, '/')
return str(self).replace(self._sep, '/')

@property
def drive(self):
Expand Down Expand Up @@ -316,8 +318,8 @@ def match(self, path_pattern, *, case_sensitive=None):
if not isinstance(path_pattern, PurePathBase):
path_pattern = self.with_segments(path_pattern)
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser)
sep = path_pattern.parser.sep
case_sensitive = self._case_sensitive
sep = path_pattern._sep
path_parts = self.parts[::-1]
pattern_parts = path_pattern.parts[::-1]
if not pattern_parts:
Expand All @@ -341,8 +343,8 @@ def full_match(self, pattern, *, case_sensitive=None):
if not isinstance(pattern, PurePathBase):
pattern = self.with_segments(pattern)
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser)
globber = self._globber(pattern.parser.sep, case_sensitive, recursive=True)
case_sensitive = self._case_sensitive
globber = self._globber(pattern._sep, case_sensitive, recursive=True)
match = globber.compile(pattern._pattern_str)
return match(self._pattern_str) is not None

Expand Down Expand Up @@ -587,15 +589,15 @@ def iterdir(self):

def _glob_selector(self, parts, case_sensitive, recurse_symlinks):
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser)
case_sensitive = self._case_sensitive
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
recursive = True if recurse_symlinks else _no_recurse_symlinks
globber = self._globber(self.parser.sep, case_sensitive, case_pedantic, recursive)
globber = self._globber(self._sep, case_sensitive, case_pedantic, recursive)
return globber.selector(parts)

def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
Expand Down
1 change: 0 additions & 1 deletion 1 Lib/pathlib/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Parser(Protocol):
subclass references its path parser via a 'parser' class attribute.
"""

sep: str
def join(self, path: str, *paths: str) -> str: ...
def split(self, path: str) -> tuple[str, str]: ...
def splitdrive(self, path: str) -> tuple[str, str]: ...
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.