# HG changeset patch # Parent bd0f73a9538e05f526feaf05821e68bdcff498fa Limit the number of results cached by filecmp.cmp(). diff --git a/Lib/filecmp.py b/Lib/filecmp.py --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -11,11 +11,18 @@ import os import stat +from collections import OrderedDict from itertools import ifilter, ifilterfalse, imap, izip +try: + from thread import allocate_lock as Lock +except: + from dummy_thread import allocate_lock as Lock __all__ = ["cmp","dircmp","cmpfiles"] -_cache = {} +_cache = OrderedDict() +_cache_lock = Lock() +_MAXCACHE = 500 BUFSIZE=8*1024 def cmp(f1, f2, shallow=1): @@ -48,10 +55,18 @@ if s1[1] != s2[1]: return False - result = _cache.get((f1, f2)) - if result and (s1, s2) == result[:2]: - return result[2] + with _cache_lock: + result = _cache.get((f1, f2)) + if result: + _cache.move_to_end((f1, f2)) # Record recent use of key (f1, f2). + if (s1, s2) == result[:2]: + return result[2] # Cache entry is not stale. Use it. + outcome = _do_cmp(f1, f2) + with _cache_lock: + _cache[f1, f2] = s1, s2, outcome + if len(_cache) > _MAXCACHE: + _cache.popitem(0) # Cache is full. Drop least recently used entry. _cache[f1, f2] = s1, s2, outcome return outcome