Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify match() code slightly.
  • Loading branch information
barneygale committed Jan 20, 2024
commit 7f9222a852307c4eddeb6d474e6110f36f2fc15b
19 changes: 10 additions & 9 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,17 @@ def match(self, path_pattern, *, case_sensitive=None):
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.pathmod)
sep = path_pattern.pathmod.sep
delta = len(self.parts) - len(path_pattern.parts)
if delta < 0:
return False # Path is too short.
if delta > 0 and path_pattern.anchor:
return False # Path is too long.
if not path_pattern.parts:
our_parts = self.parts[::-1]
pat_parts = path_pattern.parts[::-1]
Comment thread
barneygale marked this conversation as resolved.
Outdated
if not pat_parts:
raise ValueError("empty pattern")
for path, pattern in zip(reversed(self.parts), reversed(path_pattern.parts)):
match = _compile_pattern(pattern, sep, case_sensitive, recursive=False)
if match(path) is None:
if len(our_parts) < len(pat_parts):
return False
if len(our_parts) > len(pat_parts) and path_pattern.anchor:
return False
for our_part, pat_part in zip(our_parts, pat_parts):
match = _compile_pattern(pat_part, sep, case_sensitive, recursive=False)
if match(our_part) is None:
return False
return True

Expand Down