summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorGary Wilson Jr <gary.wilson@gmail.com>2008-08-02 05:56:57 +0000
committerGary Wilson Jr <gary.wilson@gmail.com>2008-08-02 05:56:57 +0000
commitc85c8f88914cdba05813cc5768377841340bd09d (patch)
tree58f61ee33072b70256a448f7df7333109adf1936 /django/utils
parent8a58f2216cb2897db53ad166d71393fef1181c1a (diff)
Fixed #7919 -- md5 and sha modules are deprecated since Python 2.5, use hashlib module when available. Patch from Karen Tracey.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8193 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/cache.py6
-rw-r--r--django/utils/hashcompat.py16
2 files changed, 19 insertions, 3 deletions
diff --git a/django/utils/cache.py b/django/utils/cache.py
index 603199cc7e..78063c6b11 100644
--- a/django/utils/cache.py
+++ b/django/utils/cache.py
@@ -17,7 +17,6 @@ An example: i18n middleware would need to distinguish caches by the
"Accept-language" header.
"""
-import md5
import re
import time
try:
@@ -29,6 +28,7 @@ from django.conf import settings
from django.core.cache import cache
from django.utils.encoding import smart_str, iri_to_uri
from django.utils.http import http_date
+from django.utils.hashcompat import md5_constructor
cc_delim_re = re.compile(r'\s*,\s*')
@@ -104,7 +104,7 @@ def patch_response_headers(response, cache_timeout=None):
if cache_timeout < 0:
cache_timeout = 0 # Can't have max-age negative
if not response.has_header('ETag'):
- response['ETag'] = '"%s"' % md5.new(response.content).hexdigest()
+ response['ETag'] = '"%s"' % md5_constructor(response.content).hexdigest()
if not response.has_header('Last-Modified'):
response['Last-Modified'] = http_date()
if not response.has_header('Expires'):
@@ -138,7 +138,7 @@ def patch_vary_headers(response, newheaders):
def _generate_cache_key(request, headerlist, key_prefix):
"""Returns a cache key from the headers given in the header list."""
- ctx = md5.new()
+ ctx = md5_constructor()
for header in headerlist:
value = request.META.get(header, None)
if value is not None:
diff --git a/django/utils/hashcompat.py b/django/utils/hashcompat.py
new file mode 100644
index 0000000000..8880d92646
--- /dev/null
+++ b/django/utils/hashcompat.py
@@ -0,0 +1,16 @@
+"""
+The md5 and sha modules are deprecated since Python 2.5, replaced by the
+hashlib module containing both hash algorithms. Here, we provide a common
+interface to the md5 and sha constructors, preferring the hashlib module when
+available.
+"""
+
+try:
+ import hashlib
+ md5_constructor = hashlib.md5
+ sha_constructor = hashlib.sha1
+except ImportError:
+ import md5
+ md5_constructor = md5.new
+ import sha
+ sha_constructor = sha.new