Ticket #18541: LocMem.diff

File LocMem.diff, 2.1 KB (added by zmsmith, 12 years ago)

Patch with improved test

  • 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..07d0a66 100644
    a b class C:  
    4242        return 24
    4343
    4444
     45class PicklingSideEffect(object):
     46
     47    def __init__(self, cache):
     48        self.cache = cache
     49        self.locked = False
     50
     51    def __getstate__(self):
     52        if self.cache._lock.active_writers:
     53            self.locked = True
     54        return {}
     55
     56
    4557class DummyCacheTests(unittest.TestCase):
    4658    # The Dummy cache backend doesn't really behave like a test backend,
    4759    # so it has different test requirements.
    class LocMemCacheTests(unittest.TestCase, BaseCacheTests):  
    919931        self.assertEqual(mirror_cache.get('value1'), 42)
    920932        self.assertEqual(other_cache.get('value1'), None)
    921933
     934    def test_locking_on_pickle(self):
     935        cache = self.cache
     936        bad_obj = PicklingSideEffect(cache)
     937        cache.set('bar', bad_obj)
     938        assert bad_obj.locked == False, "Cache was locked during pickling"
     939
    922940    def test_incr_decr_timeout(self):
    923941        """incr/decr does not modify expiry time (matches memcached behavior)"""
    924942        key = 'value'
Back to Top