https://github.com/python/cpython/commit/bbd6d17ef85e5de885cdfab0ae567184ba1550ff
commit: bbd6d17ef85e5de885cdfab0ae567184ba1550ff
branch: main
author: Barney Gale <barney.g...@gmail.com>
committer: barneygale <barney.g...@gmail.com>
date: 2025-03-09T16:36:59Z
summary:

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

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.

files:
M Lib/glob.py
M Lib/pathlib/types.py

diff --git a/Lib/glob.py b/Lib/glob.py
index d1a6dddeeb1610..8879eff80415aa 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -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
 
 
@@ -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
diff --git a/Lib/pathlib/types.py b/Lib/pathlib/types.py
index c65e4e3ede90b1..67e084c94c6ca4 100644
--- a/Lib/pathlib/types.py
+++ b/Lib/pathlib/types.py
@@ -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):
@@ -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: ...
@@ -225,7 +226,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
 
 

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to