7846: limit fnmatch pattern cache to _MAXCACHE=100 entries.

Patch by Andrew Clegg.
This commit is contained in:
R. David Murray 2010-07-09 12:23:21 +00:00
parent facb0e9a76
commit ea340a317a
4 changed files with 22 additions and 1 deletions

View file

@ -16,6 +16,7 @@ __all__ = ["filter", "fnmatch","fnmatchcase","translate"]
_cache = {} # Maps text patterns to compiled regexen.
_cacheb = {} # Ditto for bytes patterns.
_MAXCACHE = 100 # Maximum size of caches
def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN.
@ -48,6 +49,8 @@ def _compile_pattern(pat):
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