diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-09 12:47:33 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-09 12:47:33 (GMT) |
commit | f14c2632806ec19b0d58c2c1f721c6a31b535209 (patch) | |
tree | a8a42d4b4b1c6607efb0b0365dc7046674059cc0 /Lib/fnmatch.py | |
parent | c7eaede21ecb5259f2bd9e160a89b82d19cfb19b (diff) | |
download | cpython-f14c2632806ec19b0d58c2c1f721c6a31b535209.zip cpython-f14c2632806ec19b0d58c2c1f721c6a31b535209.tar.gz cpython-f14c2632806ec19b0d58c2c1f721c6a31b535209.tar.bz2 |
Also temporarily revert r83871, to fix compilation on buildbots
Diffstat (limited to 'Lib/fnmatch.py')
-rw-r--r-- | Lib/fnmatch.py | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 726fbe5..7c20e74 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -12,9 +12,19 @@ corresponding to PATTERN. (It does not compile it.) import os import posixpath import re -import functools -__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"] +__all__ = ["filter", "fnmatch", "fnmatchcase", "purge", "translate"] + +_cache = {} # Maps text patterns to compiled regexen. +_cacheb = {} # Ditto for bytes patterns. +_MAXCACHE = 100 # Maximum size of caches. + + +def purge(): + """Clear the pattern cache.""" + _cache.clear() + _cacheb.clear() + def fnmatch(name, pat): """Test whether FILENAME matches PATTERN. @@ -35,21 +45,28 @@ def fnmatch(name, pat): pat = os.path.normcase(pat) return fnmatchcase(name, pat) -@functools.lru_cache(maxsize=250) -def _compile_pattern(pat, is_bytes=False): - if is_bytes: - pat_str = str(pat, 'ISO-8859-1') - res_str = translate(pat_str) - res = bytes(res_str, 'ISO-8859-1') - else: - res = translate(pat) - return re.compile(res).match + +def _compile_pattern(pat): + cache = _cacheb if isinstance(pat, bytes) else _cache + regex = cache.get(pat) + if regex is None: + if isinstance(pat, bytes): + pat_str = str(pat, 'ISO-8859-1') + res_str = translate(pat_str) + res = bytes(res_str, 'ISO-8859-1') + else: + res = translate(pat) + if len(cache) >= _MAXCACHE: + cache.clear() + cache[pat] = regex = re.compile(res) + return regex.match + def filter(names, pat): """Return the subset of the list NAMES that match PAT.""" result = [] pat = os.path.normcase(pat) - match = _compile_pattern(pat, isinstance(pat, bytes)) + match = _compile_pattern(pat) if os.path is posixpath: # normcase on posix is NOP. Optimize it away from the loop. for name in names: @@ -61,13 +78,14 @@ def filter(names, pat): result.append(name) return result + def fnmatchcase(name, pat): """Test whether FILENAME matches PATTERN, including case. This is a version of fnmatch() which doesn't case-normalize its arguments. """ - match = _compile_pattern(pat, isinstance(pat, bytes)) + match = _compile_pattern(pat) return match(name) is not None |