Ticket #21200: locmem_django_issue_21200_with_tests.patch

File locmem_django_issue_21200_with_tests.patch, 3.5 KB (added by Thomas C, 11 years ago)

Same as above, with tests

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

    diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
    index cab690b..73b02a5 100644
    a b class LocMemCache(BaseCache):  
    2727    def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
    2828        key = self.make_key(key, version=version)
    2929        self.validate_key(key)
    30         try:
    31             pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
    32         except pickle.PickleError:
    33             return False
     30        pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
    3431        with self._lock.writer():
    3532            exp = self._expire_info.get(key)
    3633            if exp is None or exp <= time.time():
    class LocMemCache(BaseCache):  
    6966    def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
    7067        key = self.make_key(key, version=version)
    7168        self.validate_key(key)
    72         try:
    73             pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
    74         except pickle.PickleError:
    75             pass
    76         else:
    77             with self._lock.writer():
    78                 self._set(key, pickled, timeout)
     69        pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
     70        with self._lock.writer():
     71            self._set(key, pickled, timeout)
    7972
    8073    def incr(self, key, delta=1, version=None):
    8174        value = self.get(key, version=version)
    class LocMemCache(BaseCache):  
    8376            raise ValueError("Key '%s' not found" % key)
    8477        new_value = value + delta
    8578        key = self.make_key(key, version=version)
    86         try:
    87             pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
    88         except pickle.PickleError:
    89             pass
    90         else:
    91             with self._lock.writer():
    92                 self._cache[key] = pickled
     79        pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
     80        with self._lock.writer():
     81            self._cache[key] = pickled
    9382        return new_value
    9483
    9584    def has_key(self, key, version=None):
  • tests/cache/tests.py

    diff --git a/tests/cache/tests.py b/tests/cache/tests.py
    index 89c53da..c66d0f8 100644
    a b from __future__ import unicode_literals  
    66
    77import hashlib
    88import os
    9 import pickle
     9try:    # Use the same idiom as in cache backends
     10    from django.utils.six.moves import cPickle as pickle
     11except ImportError:
     12    import pickle
    1013import random
    1114import re
    1215import string
    class C:  
    4851    def m(n):
    4952        return 24
    5053
     54class Unpickable(object):
     55    def __getstate__(self):
     56        raise pickle.PickleError()
     57
    5158
    5259class DummyCacheTests(unittest.TestCase):
    5360    # The Dummy cache backend doesn't really behave like a test backend,
    class BaseCacheTests(object):  
    823830        self.assertEqual(get_cache_data.content, content.encode('utf-8'))
    824831        self.assertEqual(get_cache_data.cookies, response.cookies)
    825832
     833    def test_add_fail_on_pickleerror(self):
     834        """See https://code.djangoproject.com/ticket/21200"""
     835        with self.assertRaises(pickle.PickleError):
     836            self.cache.add('unpickable', Unpickable())
     837
     838    def test_set_fail_on_pickleerror(self):
     839        """See https://code.djangoproject.com/ticket/21200"""
     840        with self.assertRaises(pickle.PickleError):
     841            self.cache.set('unpickable', Unpickable())
     842
    826843def custom_key_func(key, key_prefix, version):
    827844    "A customized cache key function"
    828845    return 'CUSTOM-' + '-'.join([key_prefix, str(version), key])
Back to Top