Skip to content

Commit

Permalink
Removed a bunch of Python 2.4 workarounds now that we don't support i…
Browse files Browse the repository at this point in the history
…t. Refs #15702 -- thanks to jonash for the patch. Splitting this over muliple commits to make it more manageable.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15926 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Mar 28, 2011
1 parent 18ef901 commit a87be35
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 63 deletions.
11 changes: 5 additions & 6 deletions django/contrib/admin/options.py
@@ -1,3 +1,4 @@
from functools import update_wrapper, partial
from django import forms, template
from django.forms.formsets import all_valid
from django.forms.models import (modelform_factory, modelformset_factory,
Expand All @@ -17,10 +18,8 @@
from django.shortcuts import get_object_or_404, render_to_response
from django.utils.decorators import method_decorator
from django.utils.datastructures import SortedDict
from django.utils.functional import update_wrapper
from django.utils.html import escape, escapejs
from django.utils.safestring import mark_safe
from django.utils.functional import curry
from django.utils.text import capfirst, get_text_list
from django.utils.translation import ugettext as _
from django.utils.translation import ungettext
Expand Down Expand Up @@ -426,7 +425,7 @@ def get_form(self, request, obj=None, **kwargs):
"form": self.form,
"fields": fields,
"exclude": exclude,
"formfield_callback": curry(self.formfield_for_dbfield, request=request),
"formfield_callback": partial(self.formfield_for_dbfield, request=request),
}
defaults.update(kwargs)
return modelform_factory(self.model, **defaults)
Expand Down Expand Up @@ -457,7 +456,7 @@ def get_changelist_form(self, request, **kwargs):
Returns a Form class for use in the Formset on the changelist page.
"""
defaults = {
"formfield_callback": curry(self.formfield_for_dbfield, request=request),
"formfield_callback": partial(self.formfield_for_dbfield, request=request),
}
defaults.update(kwargs)
return modelform_factory(self.model, **defaults)
Expand All @@ -468,7 +467,7 @@ def get_changelist_formset(self, request, **kwargs):
is used.
"""
defaults = {
"formfield_callback": curry(self.formfield_for_dbfield, request=request),
"formfield_callback": partial(self.formfield_for_dbfield, request=request),
}
defaults.update(kwargs)
return modelformset_factory(self.model,
Expand Down Expand Up @@ -1327,7 +1326,7 @@ def get_formset(self, request, obj=None, **kwargs):
"fk_name": self.fk_name,
"fields": fields,
"exclude": exclude,
"formfield_callback": curry(self.formfield_for_dbfield, request=request),
"formfield_callback": partial(self.formfield_for_dbfield, request=request),
"extra": self.extra,
"max_num": self.max_num,
"can_delete": self.can_delete,
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/admin/sites.py
@@ -1,4 +1,5 @@
import re
from functools import update_wrapper
from django import http, template
from django.contrib.admin import ModelAdmin, actions
from django.contrib.admin.forms import AdminAuthenticationForm
Expand All @@ -9,7 +10,6 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.utils.functional import update_wrapper
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
Expand Down
6 changes: 1 addition & 5 deletions django/contrib/admin/views/decorators.py
@@ -1,8 +1,4 @@
try:
from functools import wraps
except ImportError:
from django.utils.functional import wraps # Python 2.4 fallback.

from functools import wraps
from django.utils.translation import ugettext as _
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth.views import login
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/admin/widgets.py
Expand Up @@ -2,8 +2,7 @@
Form Widget classes specific to the Django admin site.
"""

import django.utils.copycompat as copy

import copy
from django import forms
from django.forms.widgets import RadioFieldRenderer
from django.forms.util import flatatt
Expand Down
6 changes: 1 addition & 5 deletions django/contrib/auth/decorators.py
@@ -1,9 +1,5 @@
import urlparse
try:
from functools import wraps
except ImportError:
from django.utils.functional import wraps # Python 2.4 fallback.

from functools import wraps
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.utils.decorators import available_attrs
Expand Down
6 changes: 3 additions & 3 deletions django/contrib/auth/models.py
@@ -1,4 +1,5 @@
import datetime
import hashlib
import urllib

from django.contrib import auth
Expand All @@ -8,7 +9,6 @@
from django.db.models.manager import EmptyManager
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_str
from django.utils.hashcompat import md5_constructor, sha_constructor
from django.utils.translation import ugettext_lazy as _
from django.utils.crypto import constant_time_compare

Expand All @@ -29,9 +29,9 @@ def get_hexdigest(algorithm, salt, raw_password):
return crypt.crypt(raw_password, salt)

if algorithm == 'md5':
return md5_constructor(salt + raw_password).hexdigest()
return hashlib.md5(salt + raw_password).hexdigest()
elif algorithm == 'sha1':
return sha_constructor(salt + raw_password).hexdigest()
return hashlib.sha1(salt + raw_password).hexdigest()
raise ValueError("Got unknown password algorithm type in password.")

def check_password(raw_password, enc_password):
Expand Down
8 changes: 4 additions & 4 deletions django/contrib/auth/tests/tokens.py
Expand Up @@ -58,14 +58,14 @@ def test_django12_hash(self):
# Hard code in the Django 1.2 algorithm (not the result, as it is time
# dependent)
def _make_token(user):
from django.utils.hashcompat import sha_constructor
import hashlib
from django.utils.http import int_to_base36

timestamp = (date.today() - date(2001,1,1)).days
ts_b36 = int_to_base36(timestamp)
hash = sha_constructor(settings.SECRET_KEY + unicode(user.id) +
user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
unicode(timestamp)).hexdigest()[::2]
hash = hashlib.sha1(settings.SECRET_KEY + unicode(user.id) +
user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
unicode(timestamp)).hexdigest()[::2]
return "%s-%s" % (ts_b36, hash)

user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
Expand Down
9 changes: 4 additions & 5 deletions django/contrib/auth/tokens.py
@@ -1,7 +1,6 @@
from datetime import date

import hashlib
from django.conf import settings
from django.utils.hashcompat import sha_constructor
from django.utils.http import int_to_base36, base36_to_int
from django.utils.crypto import constant_time_compare, salted_hmac

Expand Down Expand Up @@ -67,9 +66,9 @@ def _make_token_with_timestamp(self, user, timestamp):
def _make_token_with_timestamp_old(self, user, timestamp):
# The Django 1.2 method
ts_b36 = int_to_base36(timestamp)
hash = sha_constructor(settings.SECRET_KEY + unicode(user.id) +
user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
unicode(timestamp)).hexdigest()[::2]
hash = hashlib.sha1(settings.SECRET_KEY + unicode(user.id) +
user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
unicode(timestamp)).hexdigest()[::2]
return "%s-%s" % (ts_b36, hash)

def _num_days(self, dt):
Expand Down
7 changes: 3 additions & 4 deletions django/contrib/comments/forms.py
@@ -1,14 +1,13 @@
import time
import datetime

import hashlib
import time
from django import forms
from django.forms.util import ErrorDict
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from models import Comment
from django.utils.crypto import salted_hmac, constant_time_compare
from django.utils.encoding import force_unicode
from django.utils.hashcompat import sha_constructor
from django.utils.text import get_text_list
from django.utils.translation import ungettext, ugettext_lazy as _

Expand Down Expand Up @@ -100,7 +99,7 @@ def _generate_security_hash_old(self, content_type, object_pk, timestamp):
"""Generate a (SHA1) security hash from the provided info."""
# Django 1.2 compatibility
info = (content_type, object_pk, timestamp, settings.SECRET_KEY)
return sha_constructor("".join(info)).hexdigest()
return hashlib.sha1("".join(info)).hexdigest()

class CommentDetailsForm(CommentSecurityForm):
"""
Expand Down
8 changes: 3 additions & 5 deletions django/contrib/contenttypes/generic.py
Expand Up @@ -2,6 +2,7 @@
Classes allowing "generic" relations through ContentType and object-id fields.
"""

from functools import partial
from django.core.exceptions import ObjectDoesNotExist
from django.db import connection
from django.db.models import signals
Expand All @@ -11,11 +12,8 @@
from django.forms import ModelForm
from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance
from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
from django.utils.encoding import smart_unicode
from django.utils.functional import curry

from django.contrib.contenttypes.models import ContentType

from django.utils.encoding import smart_unicode

class GenericForeignKey(object):
"""
Expand Down Expand Up @@ -414,7 +412,7 @@ def get_formset(self, request, obj=None):
"ct_field": self.ct_field,
"fk_field": self.ct_fk_field,
"form": self.form,
"formfield_callback": curry(self.formfield_for_dbfield, request=request),
"formfield_callback": partial(self.formfield_for_dbfield, request=request),
"formset": self.formset,
"extra": self.extra,
"can_delete": self.can_delete,
Expand Down
6 changes: 4 additions & 2 deletions django/contrib/formtools/preview.py
Expand Up @@ -2,13 +2,15 @@
Formtools Preview application.
"""

import cPickle as pickle
try:
import cPickle as pickle
except ImportError:
import pickle

from django.conf import settings
from django.http import Http404
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.utils.hashcompat import md5_constructor
from django.utils.crypto import constant_time_compare
from django.contrib.formtools.utils import security_hash

Expand Down
4 changes: 2 additions & 2 deletions django/contrib/formtools/utils.py
Expand Up @@ -3,10 +3,10 @@
except ImportError:
import pickle

import hashlib
from django.conf import settings
from django.forms import BooleanField
from django.utils.crypto import salted_hmac
from django.utils.hashcompat import md5_constructor


def security_hash(request, form, *args):
Expand Down Expand Up @@ -39,7 +39,7 @@ def security_hash(request, form, *args):
# Python 2.3, but Django requires 2.4 anyway, so that's OK.
pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)

return md5_constructor(pickled).hexdigest()
return hashlib.md5(pickled).hexdigest()


def form_hmac(form):
Expand Down
6 changes: 4 additions & 2 deletions django/contrib/formtools/wizard.py
Expand Up @@ -4,7 +4,10 @@
stored on the server side.
"""

import cPickle as pickle
try:
import cPickle as pickle
except ImportError:
import pickle

from django import forms
from django.conf import settings
Expand All @@ -13,7 +16,6 @@
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.utils.crypto import constant_time_compare
from django.utils.hashcompat import md5_constructor
from django.utils.translation import ugettext_lazy as _
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/gis/geos/tests/test_geos.py
Expand Up @@ -820,7 +820,7 @@ def test21_test_gdal(self):

def test22_copy(self):
"Testing use with the Python `copy` module."
import django.utils.copycompat as copy
import copy
poly = GEOSGeometry('POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))')
cpy1 = copy.copy(poly)
cpy2 = copy.deepcopy(poly)
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/gis/geos/tests/test_geos_mutation.py
Expand Up @@ -2,8 +2,7 @@
# Modified from original contribution by Aryeh Leib Taurog, which was
# released under the New BSD license.

import django.utils.copycompat as copy

import copy
from django.contrib.gis.geos import *
from django.contrib.gis.geos.error import GEOSIndexError
from django.utils import unittest
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/gis/tests/layermap/tests.py
@@ -1,7 +1,7 @@
import copy
import os
from decimal import Decimal

from django.utils.copycompat import copy
from django.utils.unittest import TestCase

from django.contrib.gis.gdal import DataSource, OGRException
Expand Down
6 changes: 3 additions & 3 deletions django/contrib/sessions/backends/base.py
@@ -1,4 +1,5 @@
import base64
import hashlib
import os
import random
import sys
Expand All @@ -11,7 +12,6 @@

from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.utils.hashcompat import md5_constructor
from django.utils.crypto import constant_time_compare, salted_hmac

# Use the system (hardware-based) random number generator if it exists.
Expand Down Expand Up @@ -119,7 +119,7 @@ def decode(self, session_data):
def _decode_old(self, session_data):
encoded_data = base64.decodestring(session_data)
pickled, tamper_check = encoded_data[:-32], encoded_data[-32:]
if not constant_time_compare(md5_constructor(pickled + settings.SECRET_KEY).hexdigest(),
if not constant_time_compare(hashlib.md5(pickled + settings.SECRET_KEY).hexdigest(),
tamper_check):
raise SuspiciousOperation("User tampered with session cookie.")
return pickle.loads(pickled)
Expand Down Expand Up @@ -161,7 +161,7 @@ def _get_new_session_key(self):
# No getpid() in Jython, for example
pid = 1
while 1:
session_key = md5_constructor("%s%s%s%s"
session_key = hashlib.md5("%s%s%s%s"
% (randrange(0, MAX_SESSION_KEY), pid, time.time(),
settings.SECRET_KEY)).hexdigest()
if not self.exists(session_key):
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/sessions/tests.py
@@ -1,5 +1,6 @@
import base64
from datetime import datetime, timedelta
import hashlib
import pickle
import shutil
import tempfile
Expand All @@ -15,7 +16,6 @@
from django.http import HttpResponse
from django.test import TestCase, RequestFactory
from django.utils import unittest
from django.utils.hashcompat import md5_constructor


class SessionTestsMixin(object):
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_decode_django12(self):
# Hard code the Django 1.2 method here:
def encode(session_dict):
pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
pickled_md5 = md5_constructor(pickled + settings.SECRET_KEY).hexdigest()
pickled_md5 = hashlib.md5(pickled + settings.SECRET_KEY).hexdigest()
return base64.encodestring(pickled + pickled_md5)

data = {'a test key': 'a test value'}
Expand Down
6 changes: 3 additions & 3 deletions django/core/cache/backends/filebased.py
@@ -1,15 +1,15 @@
"File-based cache backend"

import hashlib
import os
import time
import shutil
import time
try:
import cPickle as pickle
except ImportError:
import pickle

from django.core.cache.backends.base import BaseCache
from django.utils.hashcompat import md5_constructor

class FileBasedCache(BaseCache):
def __init__(self, dir, params):
Expand Down Expand Up @@ -145,7 +145,7 @@ def _key_to_file(self, key):
Thus, a cache key of "foo" gets turnned into a file named
``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``.
"""
path = md5_constructor(key).hexdigest()
path = hashlib.md5(key).hexdigest()
path = os.path.join(path[:2], path[2:4], path[4:])
return os.path.join(self._dir, path)

Expand Down

0 comments on commit a87be35

Please sign in to comment.