Ticket #4845: dj-unicode-cache.diff
File dj-unicode-cache.diff, 4.9 KB (added by , 17 years ago) |
---|
-
django/core/cache/backends/filebased.py
1 1 "File-based cache backend" 2 2 3 3 from django.core.cache.backends.simple import CacheClass as SimpleCacheClass 4 import os, time, urllib 4 from django.utils.http import urlquote_plus 5 import os, time 5 6 try: 6 7 import cPickle as pickle 7 8 except ImportError: … … 77 78 raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir 78 79 79 80 def _key_to_file(self, key): 80 return os.path.join(self._dir, url lib.quote_plus(key))81 return os.path.join(self._dir, urlquote_plus(key)) -
django/core/cache/backends/memcached.py
1 1 "Memcached cache backend" 2 2 3 3 from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError 4 from django.utils.encoding import smart_unicode, smart_str 5 import types 4 6 5 7 try: 6 8 import cmemcache as memcache … … 16 18 self._cache = memcache.Client(server.split(';')) 17 19 18 20 def get(self, key, default=None): 19 val = self._cache.get( key)21 val = self._cache.get(smart_str(key)) 20 22 if val is None: 21 23 return default 22 24 else: 23 return val 25 if isinstance(val, basestring): 26 return smart_unicode(val) 27 else: 28 return val 24 29 25 30 def set(self, key, value, timeout=0): 26 self._cache.set(key, value, timeout or self.default_timeout) 31 if isinstance(value, types.UnicodeType): 32 value = smart_str(value) 33 self._cache.set(smart_str(key), value, timeout or self.default_timeout) 27 34 28 35 def delete(self, key): 29 self._cache.delete( key)36 self._cache.delete(smart_str(key)) 30 37 31 38 def get_many(self, keys): 32 return self._cache.get_multi( keys)39 return self._cache.get_multi(map(smart_str,keys)) -
tests/regressiontests/cache/tests.py
1 # -*- coding: utf-8 -*- 2 1 3 # Unit tests for cache framework 2 4 # Uses whatever cache backend is set in the test settings file. 3 5 … … 19 21 20 22 def test_non_existent(self): 21 23 # get with non-existent keys 22 self.assertEqual(cache.get("does notexist"), None)23 self.assertEqual(cache.get("does notexist", "bang!"), "bang!")24 self.assertEqual(cache.get("does_not_exist"), None) 25 self.assertEqual(cache.get("does_not_exist", "bang!"), "bang!") 24 26 25 27 def test_get_many(self): 26 28 # get_many … … 31 33 self.assertEqual(cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'}) 32 34 self.assertEqual(cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'}) 33 35 34 def test_delete(self): 36 def test_delete(self): 35 37 # delete 36 38 cache.set("key1", "spam") 37 39 cache.set("key2", "eggs") … … 42 44 43 45 def test_has_key(self): 44 46 # has_key 45 cache.set("hello ", "goodbye")46 self.assertEqual(cache.has_key("hello "), True)47 self.assertEqual(cache.has_key("goodbye "), False)47 cache.set("hello1", "goodbye1") 48 self.assertEqual(cache.has_key("hello1"), True) 49 self.assertEqual(cache.has_key("goodbye1"), False) 48 50 49 51 def test_in(self): 50 cache.set("hello ", "goodbye")51 self.assertEqual("hello " in cache, True)52 self.assertEqual("goodbye " in cache, False)52 cache.set("hello2", "goodbye2") 53 self.assertEqual("hello2" in cache, True) 54 self.assertEqual("goodbye2" in cache, False) 53 55 54 56 def test_data_types(self): 55 57 stuff = { … … 61 63 'function' : f, 62 64 'class' : C, 63 65 } 64 for (key, value) in stuff.items():65 cache.set(key, value)66 self.assertEqual(cache.get(key), value)67 66 68 67 def test_expiration(self): 69 68 # expiration … … 71 70 time.sleep(2) 72 71 self.assertEqual(cache.get("expire"), None) 73 72 73 def test_unicode(self): 74 stuff = { 75 u'ascii': u'ascii_value', 76 u'unicode_ascii': u'Iñtërnâtiônàlizætiøn1', 77 u'Iñtërnâtiônàlizætiøn': u'Iñtërnâtiônàlizætiøn2', 78 u'ascii': {u'x' : 1 } 79 } 80 for (key, value) in stuff.items(): 81 cache.set(key, value) 82 self.assertEqual(cache.get(key), value) 83 84 74 85 if __name__ == '__main__': 75 unittest.main() 76 No newline at end of file 86 unittest.main()