Prevent modify mutable cached value
The cached values are copied when set and copied again when returned so the stored values can never be modified. Closes #9558
This commit is contained in:
1 parent
c76a4f85d1
commit
f26aa8863a
2 files changed
+12
-2
No files matched your search
@@ -7,6 +7,7 @@ import os
|
||||
import selectors
|
||||
import threading
|
||||
from collections import OrderedDict, defaultdict
|
||||
from copy import deepcopy
|
||||
from weakref import WeakKeyDictionary
|
||||
|
||||
from sql import Table
|
||||
@@ -190,7 +191,7 @@ class MemoryCache(BaseCache):
|
||||
return default
|
||||
cache[key] = (expire, result)
|
||||
self.hit += 1
|
||||
return result
|
||||
return deepcopy(result)
|
||||
except (KeyError, TypeError):
|
||||
self.miss += 1
|
||||
return default
|
||||
@@ -203,7 +204,7 @@ class MemoryCache(BaseCache):
|
||||
else:
|
||||
expire = None
|
||||
try:
|
||||
cache[key] = (expire, value)
|
||||
cache[key] = (expire, deepcopy(value))
|
||||
except TypeError:
|
||||
pass
|
||||
return value
|
||||
|
||||
@@ -121,6 +121,15 @@ class MemoryCacheTestCase(unittest.TestCase):
|
||||
|
||||
self.assertEqual(cache.get('foo'), 'bar')
|
||||
|
||||
@with_transaction()
|
||||
def test_memory_cache_mutable(self):
|
||||
"Test MemoryCache with mutable value"
|
||||
value = ['bar']
|
||||
cache.set('foo', value)
|
||||
value.remove('bar')
|
||||
|
||||
self.assertEqual(cache.get('foo'), ['bar'])
|
||||
|
||||
@with_transaction()
|
||||
def test_memory_cache_drop(self):
|
||||
"Test MemoryCache drop"
|
||||
|
||||
Reference in new issue
Block a user