Skip to content

Commit 17ae4af

Browse files
authored
Merge pull request #837 from HEXRD/memoize-readonly-cached-arrays
Make memoized output numpy arrays readonly
2 parents cf45859 + 0e17e2d commit 17ae4af

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

hexrd/utils/decorators.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ def wrapped(*args, **kwargs):
7575
# Remove the left item (least recently used)
7676
cache.popitem(last=False)
7777

78+
output = func(*args, **kwargs)
79+
if isinstance(output, np.ndarray):
80+
# Make the array readonly so that caller functions *cannot*
81+
# modify the cached output array. Otherwise, we run into
82+
# hard-to-track-down bugs.
83+
output.flags.writeable = False
84+
7885
# This inserts the item on the right (most recently used)
79-
cache[key] = func(*args, **kwargs)
86+
cache[key] = output
8087
misses += 1
8188
else:
8289
# Move the item to the right (most recently used)

0 commit comments

Comments
 (0)