File: cache_exception_safety.patch

package info (click to toggle)
libgpuarray 0.7.6-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,176 kB
  • sloc: ansic: 19,235; python: 4,591; makefile: 208; javascript: 71; sh: 15
file content (33 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download | duplicates (3)
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():