Skip to content

Commit 75a9de6

Browse files
committed
pythonGH-130614: pathlib ABCs: support alternate separator in full_match()
In `pathlib.types._JoinablePath.full_match()`, treat alternate path separators in the path and pattern as if they were primary separators. e.g. if the parser is `ntpath`, then `P(r'foo/bar\baz').full_match(r'*\*/*')` is true.
1 parent edd1eca commit 75a9de6

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Lib/glob.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
320320

321321

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

330330

@@ -360,8 +360,9 @@ def concat_path(path, text):
360360

361361
# High-level methods
362362

363-
def compile(self, pat):
364-
return _compile_pattern(pat, self.sep, self.case_sensitive, self.recursive)
363+
def compile(self, pat, altsep=None):
364+
seps = (self.sep, altsep) if altsep else self.sep
365+
return _compile_pattern(pat, seps, self.case_sensitive, self.recursive)
365366

366367
def selector(self, parts):
367368
"""Returns a function that selects from a given path, walking and

Lib/pathlib/types.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from glob import _PathGlobber, _no_recurse_symlinks
1515
from pathlib import PurePath, Path
1616
from pathlib._os import magic_open, ensure_distinct_paths, copy_file
17-
from typing import Protocol, runtime_checkable
17+
from typing import Optional, Protocol, runtime_checkable
1818

1919

2020
def _explode_path(path):
@@ -44,6 +44,7 @@ class _PathParser(Protocol):
4444
"""
4545

4646
sep: str
47+
altsep: Optional[str]
4748
def split(self, path: str) -> tuple[str, str]: ...
4849
def splitext(self, path: str) -> tuple[str, str]: ...
4950
def normcase(self, path: str) -> str: ...
@@ -223,7 +224,7 @@ def full_match(self, pattern, *, case_sensitive=None):
223224
if case_sensitive is None:
224225
case_sensitive = self.parser.normcase('Aa') == 'Aa'
225226
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
226-
match = globber.compile(str(pattern))
227+
match = globber.compile(str(pattern), altsep=pattern.parser.altsep)
227228
return match(str(self)) is not None
228229

229230

0 commit comments

Comments
 (0)