gh-74598: add fnmatch.filterfalse for excluding names matching a patern (#121185)

This commit is contained in:
Bénédikt Tran 2025-04-08 12:11:25 +02:00 committed by GitHub
parent ee3657209b
commit 3eda146035
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 102 additions and 40 deletions

View file

@ -9,12 +9,15 @@ expression. They cache the compiled regular expressions for speed.
The function translate(PATTERN) returns a regular expression
corresponding to PATTERN. (It does not compile it.)
"""
import functools
import itertools
import os
import posixpath
import re
import functools
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
__all__ = ["filter", "filterfalse", "fnmatch", "fnmatchcase", "translate"]
def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN.
@ -35,6 +38,7 @@ def fnmatch(name, pat):
pat = os.path.normcase(pat)
return fnmatchcase(name, pat)
@functools.lru_cache(maxsize=32768, typed=True)
def _compile_pattern(pat):
if isinstance(pat, bytes):
@ -45,6 +49,7 @@ def _compile_pattern(pat):
res = translate(pat)
return re.compile(res).match
def filter(names, pat):
"""Construct a list from those elements of the iterable NAMES that match PAT."""
result = []
@ -61,6 +66,22 @@ def filter(names, pat):
result.append(name)
return result
def filterfalse(names, pat):
"""Construct a list from those elements of the iterable NAMES that do not match PAT."""
pat = os.path.normcase(pat)
match = _compile_pattern(pat)
if os.path is posixpath:
# normcase on posix is NOP. Optimize it away from the loop.
return list(itertools.filterfalse(match, names))
result = []
for name in names:
if match(os.path.normcase(name)) is None:
result.append(name)
return result
def fnmatchcase(name, pat):
"""Test whether FILENAME matches PATTERN, including case.
@ -80,9 +101,11 @@ def translate(pat):
parts, star_indices = _translate(pat, '*', '.')
return _join_translated_parts(parts, star_indices)
_re_setops_sub = re.compile(r'([&~|])').sub
_re_escape = functools.lru_cache(maxsize=512)(re.escape)
def _translate(pat, star, question_mark):
res = []
add = res.append