[py3] Always fed hashlib with bytes.

This commit is contained in:
Łukasz Langa 2013-02-27 04:26:15 +01:00 committed by Aymeric Augustin
parent a28e2e7c4d
commit 40b9f4fb8b
4 changed files with 16 additions and 15 deletions

View File

@ -6,6 +6,7 @@ to load templates from them in order, caching the result.
import hashlib import hashlib
from django.template.base import TemplateDoesNotExist from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader, get_template_from_string, find_template_loader, make_origin from django.template.loader import BaseLoader, get_template_from_string, find_template_loader, make_origin
from django.utils.encoding import force_bytes
class Loader(BaseLoader): class Loader(BaseLoader):
is_usable = True is_usable = True
@ -40,7 +41,7 @@ class Loader(BaseLoader):
key = template_name key = template_name
if template_dirs: if template_dirs:
# If template directories were specified, use a hash to differentiate # If template directories were specified, use a hash to differentiate
key = '-'.join([template_name, hashlib.sha1('|'.join(template_dirs)).hexdigest()]) key = '-'.join([template_name, hashlib.sha1(force_bytes('|'.join(template_dirs))).hexdigest()])
if key not in self.template_cache: if key not in self.template_cache:
template, origin = self.find_template(template_name, template_dirs) template, origin = self.find_template(template_name, template_dirs)

View File

@ -181,7 +181,7 @@ def _generate_cache_key(request, method, headerlist, key_prefix):
for header in headerlist: for header in headerlist:
value = request.META.get(header, None) value = request.META.get(header, None)
if value is not None: if value is not None:
ctx.update(value) ctx.update(force_bytes(value))
path = hashlib.md5(force_bytes(iri_to_uri(request.get_full_path()))) path = hashlib.md5(force_bytes(iri_to_uri(request.get_full_path())))
cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % ( cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
key_prefix, method, path.hexdigest(), ctx.hexdigest()) key_prefix, method, path.hexdigest(), ctx.hexdigest())

View File

@ -72,10 +72,10 @@ def get_random_string(length=12,
# is better than absolute predictability. # is better than absolute predictability.
random.seed( random.seed(
hashlib.sha256( hashlib.sha256(
"%s%s%s" % ( ("%s%s%s" % (
random.getstate(), random.getstate(),
time.time(), time.time(),
settings.SECRET_KEY) settings.SECRET_KEY)).encode('utf-8')
).digest()) ).digest())
return ''.join([random.choice(allowed_chars) for i in range(length)]) return ''.join([random.choice(allowed_chars) for i in range(length)])

22
tests/cache/tests.py vendored
View File

@ -1327,7 +1327,7 @@ class CacheI18nTest(TestCase):
def check_accept_language_vary(self, accept_language, vary, reference_key): def check_accept_language_vary(self, accept_language, vary, reference_key):
request = self._get_request() request = self._get_request()
request.META['HTTP_ACCEPT_LANGUAGE'] = accept_language request.META['HTTP_ACCEPT_LANGUAGE'] = accept_language
request.META['HTTP_ACCEPT_ENCODING'] = b'gzip;q=1.0, identity; q=0.5, *;q=0' request.META['HTTP_ACCEPT_ENCODING'] = 'gzip;q=1.0, identity; q=0.5, *;q=0'
response = HttpResponse() response = HttpResponse()
response['Vary'] = vary response['Vary'] = vary
key = learn_cache_key(request, response) key = learn_cache_key(request, response)
@ -1340,53 +1340,53 @@ class CacheI18nTest(TestCase):
lang = translation.get_language() lang = translation.get_language()
self.assertEqual(lang, 'en') self.assertEqual(lang, 'en')
request = self._get_request() request = self._get_request()
request.META['HTTP_ACCEPT_ENCODING'] = b'gzip;q=1.0, identity; q=0.5, *;q=0' request.META['HTTP_ACCEPT_ENCODING'] = 'gzip;q=1.0, identity; q=0.5, *;q=0'
response = HttpResponse() response = HttpResponse()
response['Vary'] = 'accept-encoding' response['Vary'] = 'accept-encoding'
key = learn_cache_key(request, response) key = learn_cache_key(request, response)
self.assertIn(lang, key, "Cache keys should include the language name when translation is active") self.assertIn(lang, key, "Cache keys should include the language name when translation is active")
self.check_accept_language_vary( self.check_accept_language_vary(
b'en-us', 'en-us',
'cookie, accept-language, accept-encoding', 'cookie, accept-language, accept-encoding',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'en-US', 'en-US',
'cookie, accept-encoding, accept-language', 'cookie, accept-encoding, accept-language',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'en-US,en;q=0.8', 'en-US,en;q=0.8',
'accept-encoding, accept-language, cookie', 'accept-encoding, accept-language, cookie',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'en-US,en;q=0.8,ko;q=0.6', 'en-US,en;q=0.8,ko;q=0.6',
'accept-language, cookie, accept-encoding', 'accept-language, cookie, accept-encoding',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3 ', 'ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3 ',
'accept-encoding, cookie, accept-language', 'accept-encoding, cookie, accept-language',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4', 'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4',
'accept-language, accept-encoding, cookie', 'accept-language, accept-encoding, cookie',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'ko;q=1.0,en;q=0.5', 'ko;q=1.0,en;q=0.5',
'cookie, accept-language, accept-encoding', 'cookie, accept-language, accept-encoding',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'ko, en', 'ko, en',
'cookie, accept-encoding, accept-language', 'cookie, accept-encoding, accept-language',
key key
) )
self.check_accept_language_vary( self.check_accept_language_vary(
b'ko-KR, en-US', 'ko-KR, en-US',
'accept-encoding, accept-language, cookie', 'accept-encoding, accept-language, cookie',
key key
) )