1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
Description: Don't corrupt lru_cache on exceptions
If user_function raises an exception, avoid adding a key to
last_use and not cache, as such keys trigger KeyError when purged.
Author: Rebecca N. Palmer <rebecca_palmer@zoho.com>
Forwarded: https://github.com/Theano/libgpuarray/pull/584
--- a/pygpu/tools.py
+++ b/pygpu/tools.py
@@ -177,7 +177,6 @@ def lru_cache(maxsize=20):
@functools.wraps(user_function)
def wrapper(*key):
time[0] += 1
- last_use[key] = time[0]
try:
result = cache[key]
@@ -189,11 +188,12 @@ def lru_cache(maxsize=20):
# purge least recently used cache entries
if len(cache) > wrapper.maxsize:
- for key, _ in nsmallest(wrapper.maxsize // 10,
+ for key0, _ in nsmallest(wrapper.maxsize // 10,
six.iteritems(last_use),
key=itemgetter(1)):
- del cache[key], last_use[key]
+ del cache[key0], last_use[key0]
+ last_use[key] = time[0]
return result
def clear():
|