Skip to content
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

GH-130614: pathlib ABCs: support alternate separator in full_match() #130991

Merged
merged 1 commit into from
Mar 9, 2025
Merged
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
9 changes: 5 additions & 4 deletions Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):


@functools.lru_cache(maxsize=512)
def _compile_pattern(pat, sep, case_sensitive, recursive=True):
def _compile_pattern(pat, seps, case_sensitive, recursive=True):
"""Compile given glob pattern to a re.Pattern object (observing case
sensitivity)."""
flags = re.NOFLAG if case_sensitive else re.IGNORECASE
regex = translate(pat, recursive=recursive, include_hidden=True, seps=sep)
regex = translate(pat, recursive=recursive, include_hidden=True, seps=seps)
return re.compile(regex, flags=flags).match


Expand Down Expand Up @@ -360,8 +360,9 @@ def concat_path(path, text):

# High-level methods

def compile(self, pat):
return _compile_pattern(pat, self.sep, self.case_sensitive, self.recursive)
def compile(self, pat, altsep=None):
seps = (self.sep, altsep) if altsep else self.sep
return _compile_pattern(pat, seps, self.case_sensitive, self.recursive)

def selector(self, parts):
"""Returns a function that selects from a given path, walking and
Expand Down
5 changes: 3 additions & 2 deletions Lib/pathlib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from glob import _PathGlobber, _no_recurse_symlinks
from pathlib import PurePath, Path
from pathlib._os import magic_open, ensure_distinct_paths, copy_file
from typing import Protocol, runtime_checkable
from typing import Optional, Protocol, runtime_checkable


def _explode_path(path):
Expand Down Expand Up @@ -44,6 +44,7 @@ class _PathParser(Protocol):
"""

sep: str
altsep: Optional[str]
def split(self, path: str) -> tuple[str, str]: ...
def splitext(self, path: str) -> tuple[str, str]: ...
def normcase(self, path: str) -> str: ...
Expand Down Expand Up @@ -223,7 +224,7 @@ def full_match(self, pattern, *, case_sensitive=None):
if case_sensitive is None:
case_sensitive = self.parser.normcase('Aa') == 'Aa'
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
match = globber.compile(str(pattern))
match = globber.compile(str(pattern), altsep=pattern.parser.altsep)
return match(str(self)) is not None


Expand Down
Loading