Ticket #18541: LocMemlocking.diff

File LocMemlocking.diff, 1.9 KB (added by zmsmith, 12 years ago)

LocMemlocking.diff

  • django/core/cache/backends/locmem.py

    diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
    index 9196b3b..0054d22 100644
    a b class LocMemCache(BaseCache):  
    6969    def set(self, key, value, timeout=None, version=None):
    7070        key = self.make_key(key, version=version)
    7171        self.validate_key(key)
     72        try:
     73            pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
     74        except pickle.PickleError:
     75            return
    7276        with self._lock.writer():
    73             try:
    74                 pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
    75                 self._set(key, pickled, timeout)
    76             except pickle.PickleError:
    77                 pass
     77            self._set(key, pickled, timeout)
    7878
    7979    def incr(self, key, delta=1, version=None):
    8080        value = self.get(key, version=version)
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index 264ef74..dca7d00 100644
    a b class C:  
    4242        return 24
    4343
    4444
     45class PicklingSideEffect(object):
     46    def __getstate__(self):
     47        self.cache.get('foo')
     48        return {}
     49
     50
    4551class DummyCacheTests(unittest.TestCase):
    4652    # The Dummy cache backend doesn't really behave like a test backend,
    4753    # so it has different test requirements.
    class LocMemCacheTests(unittest.TestCase, BaseCacheTests):  
    919925        self.assertEqual(mirror_cache.get('value1'), 42)
    920926        self.assertEqual(other_cache.get('value1'), None)
    921927
     928    def test_locking_on_pickle(self):
     929
     930        cache = self.cache
     931
     932        bad_obj = PicklingSideEffect()
     933        bad_obj.cache = cache
     934        cache.set('bar', bad_obj)
     935
    922936    def test_incr_decr_timeout(self):
    923937        """incr/decr does not modify expiry time (matches memcached behavior)"""
    924938        key = 'value'
Back to Top