Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[py3] Fixed encoding issues in cache key generation

  • Loading branch information...
commit 45baaabafb6cf911afad9ec63c86753b284f7269 1 parent d774ad7
Claude Paroz authored August 13, 2012
6  django/core/cache/backends/base.py
... ...
@@ -1,9 +1,9 @@
1 1
 "Base Cache class."
  2
+from __future__ import unicode_literals
2 3
 
3 4
 import warnings
4 5
 
5 6
 from django.core.exceptions import ImproperlyConfigured, DjangoRuntimeWarning
6  
-from django.utils.encoding import smart_bytes
7 7
 from django.utils.importlib import import_module
8 8
 
9 9
 class InvalidCacheBackendError(ImproperlyConfigured):
@@ -23,7 +23,7 @@ def default_key_func(key, key_prefix, version):
23 23
     the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
24 24
     function with custom key making behavior.
25 25
     """
26  
-    return ':'.join([key_prefix, str(version), smart_bytes(key)])
  26
+    return ':'.join([key_prefix, str(version), key])
27 27
 
28 28
 def get_key_func(key_func):
29 29
     """
@@ -62,7 +62,7 @@ def __init__(self, params):
62 62
         except (ValueError, TypeError):
63 63
             self._cull_frequency = 3
64 64
 
65  
-        self.key_prefix = smart_bytes(params.get('KEY_PREFIX', ''))
  65
+        self.key_prefix = params.get('KEY_PREFIX', '')
66 66
         self.version = params.get('VERSION', 1)
67 67
         self.key_func = get_key_func(params.get('KEY_FUNCTION', None))
68 68
 
3  django/utils/cache.py
@@ -16,6 +16,7 @@
16 16
 An example: i18n middleware would need to distinguish caches by the
17 17
 "Accept-language" header.
18 18
 """
  19
+from __future__ import unicode_literals
19 20
 
20 21
 import hashlib
21 22
 import re
@@ -170,7 +171,7 @@ def _i18n_cache_key_suffix(request, cache_key):
170 171
         # User-defined tzinfo classes may return absolutely anything.
171 172
         # Hence this paranoid conversion to create a valid cache key.
172 173
         tz_name = force_text(get_current_timezone_name(), errors='ignore')
173  
-        cache_key += '.%s' % tz_name.encode('ascii', 'ignore').replace(' ', '_')
  174
+        cache_key += '.%s' % tz_name.encode('ascii', 'ignore').decode('ascii').replace(' ', '_')
174 175
     return cache_key
175 176
 
176 177
 def _generate_cache_key(request, method, headerlist, key_prefix):
4  tests/regressiontests/cache/tests.py
@@ -1308,7 +1308,7 @@ def test_cache_key_i18n_timezone(self):
1308 1308
         # This is tightly coupled to the implementation,
1309 1309
         # but it's the most straightforward way to test the key.
1310 1310
         tz = force_text(timezone.get_current_timezone_name(), errors='ignore')
1311  
-        tz = tz.encode('ascii', 'ignore').replace(' ', '_')
  1311
+        tz = tz.encode('ascii', 'ignore').decode('ascii').replace(' ', '_')
1312 1312
         response = HttpResponse()
1313 1313
         key = learn_cache_key(request, response)
1314 1314
         self.assertIn(tz, key, "Cache keys should include the time zone name when time zones are active")
@@ -1320,7 +1320,7 @@ def test_cache_key_no_i18n (self):
1320 1320
         request = self._get_request()
1321 1321
         lang = translation.get_language()
1322 1322
         tz = force_text(timezone.get_current_timezone_name(), errors='ignore')
1323  
-        tz = tz.encode('ascii', 'ignore').replace(' ', '_')
  1323
+        tz = tz.encode('ascii', 'ignore').decode('ascii').replace(' ', '_')
1324 1324
         response = HttpResponse()
1325 1325
         key = learn_cache_key(request, response)
1326 1326
         self.assertNotIn(lang, key, "Cache keys shouldn't include the language name when i18n isn't active")

0 notes on commit 45baaab

Please sign in to comment.
Something went wrong with that request. Please try again.