Ticket #3012: locmem-cache-exception-fix.patch

File locmem-cache-exception-fix.patch, 2.2 KB (added by Sundance, 8 years ago)
  • django/core/cache/backends/locmem.py

     
    22
    33from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
    44from django.utils.synch import RWLock
    5 import copy, time
     5import time
     6try:
     7    import cPickle as pickle
     8except ImportError:
     9    import pickle
    610
    711class CacheClass(SimpleCacheClass):
    812    def __init__(self, host, params):
     
    2024            elif exp < now:
    2125                should_delete = True
    2226            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
    2431        finally:
    2532            self._lock.reader_leaves()
    2633        if should_delete:
     
    3542    def set(self, key, value, timeout=None):
    3643        self._lock.writer_enters()
    3744        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
    3949        finally:
    4050            self._lock.writer_leaves()
    4151
  • tests/regressiontests/cache/tests.py

     
    6767        time.sleep(2)
    6868        self.assertEqual(cache.get("expire"), None)
    6969
     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
    7079if __name__ == '__main__':
    7180    unittest.main()
     81 No newline at end of file
Back to Top