7846: limit fnmatch pattern cache to _MAXCACHE=100 entries.
Patch by Andrew Clegg.
diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py
index 30c0a92..26ae4cc 100644
--- a/Lib/fnmatch.py
+++ b/Lib/fnmatch.py
@@ -16,6 +16,7 @@
_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 @@
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