summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index ef7c47c..3d68c16 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -86,6 +86,12 @@ def _make_selector(pattern_parts, flavour, case_sensitive):
return cls(pat, child_parts, flavour, case_sensitive)
+@functools.lru_cache(maxsize=256)
+def _compile_pattern(pat, case_sensitive):
+ flags = re.NOFLAG if case_sensitive else re.IGNORECASE
+ return re.compile(fnmatch.translate(pat), flags).match
+
+
class _Selector:
"""A selector matches a specific glob pattern part against the children
of a given path."""
@@ -133,8 +139,7 @@ class _WildcardSelector(_Selector):
if case_sensitive is None:
# TODO: evaluate case-sensitivity of each directory in _select_from()
case_sensitive = _is_case_sensitive(flavour)
- flags = re.NOFLAG if case_sensitive else re.IGNORECASE
- self.match = re.compile(fnmatch.translate(pat), flags=flags).fullmatch
+ self.match = _compile_pattern(pat, case_sensitive)
def _select_from(self, parent_path, scandir):
try:
@@ -680,22 +685,25 @@ class PurePath(object):
name = self._tail[-1].partition('.')[0].partition(':')[0].rstrip(' ')
return name.upper() in _WIN_RESERVED_NAMES
- def match(self, path_pattern):
+ def match(self, path_pattern, *, case_sensitive=None):
"""
Return True if this path matches the given pattern.
"""
+ if case_sensitive is None:
+ case_sensitive = _is_case_sensitive(self._flavour)
pat = self.with_segments(path_pattern)
if not pat.parts:
raise ValueError("empty pattern")
- pat_parts = pat._parts_normcase
- parts = self._parts_normcase
+ pat_parts = pat.parts
+ parts = self.parts
if pat.drive or pat.root:
if len(pat_parts) != len(parts):
return False
elif len(pat_parts) > len(parts):
return False
for part, pat in zip(reversed(parts), reversed(pat_parts)):
- if not fnmatch.fnmatchcase(part, pat):
+ match = _compile_pattern(pat, case_sensitive)
+ if not match(part):
return False
return True