summaryrefslogtreecommitdiffstats
path: root/Lib/functools.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-10-16 07:00:51 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-10-16 07:00:51 (GMT)
commitd8886fc831e16ab225f7e474751cc1a7b3cd01df (patch)
treebd216e577165057f7a0db675886c335e5892a80b /Lib/functools.py
parente60698317dc7772983784037d966d24d1d30dc87 (diff)
parent4b779b3785c0014224eef95c8805f166d0ef2a86 (diff)
downloadcpython-d8886fc831e16ab225f7e474751cc1a7b3cd01df.zip
cpython-d8886fc831e16ab225f7e474751cc1a7b3cd01df.tar.gz
cpython-d8886fc831e16ab225f7e474751cc1a7b3cd01df.tar.bz2
Merge
Diffstat (limited to 'Lib/functools.py')
-rw-r--r--Lib/functools.py34
1 files changed, 19 insertions, 15 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index b2bcc21..038f284 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -146,7 +146,7 @@ def lru_cache(maxsize=100):
hits = misses = 0
kwd_mark = (object(),) # separates positional and keyword args
- lock = Lock() # needed because ordereddicts aren't threadsafe
+ lock = Lock() # needed because OrderedDict isn't threadsafe
if maxsize is None:
cache = dict() # simple cache without ordering or size limit
@@ -160,13 +160,15 @@ def lru_cache(maxsize=100):
try:
result = cache[key]
hits += 1
+ return result
except KeyError:
- result = user_function(*args, **kwds)
- cache[key] = result
- misses += 1
+ pass
+ result = user_function(*args, **kwds)
+ cache[key] = result
+ misses += 1
return result
else:
- cache = OrderedDict() # ordered least recent to most recent
+ cache = OrderedDict() # ordered least recent to most recent
cache_popitem = cache.popitem
cache_renew = cache.move_to_end
@@ -176,18 +178,20 @@ def lru_cache(maxsize=100):
key = args
if kwds:
key += kwd_mark + tuple(sorted(kwds.items()))
- try:
- with lock:
+ with lock:
+ try:
result = cache[key]
- cache_renew(key) # record recent use of this key
+ cache_renew(key) # record recent use of this key
hits += 1
- except KeyError:
- result = user_function(*args, **kwds)
- with lock:
- cache[key] = result # record recent use of this key
- misses += 1
- if len(cache) > maxsize:
- cache_popitem(0) # purge least recently used cache entry
+ return result
+ except KeyError:
+ pass
+ result = user_function(*args, **kwds)
+ with lock:
+ cache[key] = result # record recent use of this key
+ misses += 1
+ if len(cache) > maxsize:
+ cache_popitem(0) # purge least recently used cache entry
return result
def cache_info():