Ticket #3012: locmem-cache-exception-fix.patch
File locmem-cache-exception-fix.patch, 2.2 KB (added by , 18 years ago) |
---|
-
django/core/cache/backends/locmem.py
2 2 3 3 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass 4 4 from django.utils.synch import RWLock 5 import copy, time 5 import time 6 try: 7 import cPickle as pickle 8 except ImportError: 9 import pickle 6 10 7 11 class CacheClass(SimpleCacheClass): 8 12 def __init__(self, host, params): … … 20 24 elif exp < now: 21 25 should_delete = True 22 26 else: 23 return copy.deepcopy(self._cache[key]) 27 try: 28 return pickle.loads(self._cache[key]) 29 except (pickle.PickleError, TypeError): 30 return default 24 31 finally: 25 32 self._lock.reader_leaves() 26 33 if should_delete: … … 35 42 def set(self, key, value, timeout=None): 36 43 self._lock.writer_enters() 37 44 try: 38 SimpleCacheClass.set(self, key, value, timeout) 45 try: 46 SimpleCacheClass.set(self, key, pickle.dumps(value), timeout) 47 except (pickle.PickleError, TypeError): 48 pass 39 49 finally: 40 50 self._lock.writer_leaves() 41 51 -
tests/regressiontests/cache/tests.py
67 67 time.sleep(2) 68 68 self.assertEqual(cache.get("expire"), None) 69 69 70 def test_unserializable_data(self): 71 # Ensure that the cache backend doesn't choke on unserializable 72 # data. (see bug #3012) 73 listiterator = iter([1, 2, 3, 4]) 74 defaultvalue = "This is a default value." 75 cache.set("testiterator", listiterator) 76 cached_data = cache.get("testiterator", default=defaultvalue) 77 self.assert_(cached_data in (defaultvalue, listiterator)) 78 70 79 if __name__ == '__main__': 71 80 unittest.main() 81 No newline at end of file