Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed a bunch of Python 2.4 workarounds now that we don't support i…

…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...
commit a87be3554fc73a567b926ea51c13ea844b5113f8 1 parent 18ef901
Adrian Holovaty authored March 28, 2011
11  django/contrib/admin/options.py
... ...
@@ -1,3 +1,4 @@
  1
+from functools import update_wrapper, partial
1 2
 from django import forms, template
2 3
 from django.forms.formsets import all_valid
3 4
 from django.forms.models import (modelform_factory, modelformset_factory,
@@ -17,10 +18,8 @@
17 18
 from django.shortcuts import get_object_or_404, render_to_response
18 19
 from django.utils.decorators import method_decorator
19 20
 from django.utils.datastructures import SortedDict
20  
-from django.utils.functional import update_wrapper
21 21
 from django.utils.html import escape, escapejs
22 22
 from django.utils.safestring import mark_safe
23  
-from django.utils.functional import curry
24 23
 from django.utils.text import capfirst, get_text_list
25 24
 from django.utils.translation import ugettext as _
26 25
 from django.utils.translation import ungettext
@@ -426,7 +425,7 @@ def get_form(self, request, obj=None, **kwargs):
426 425
             "form": self.form,
427 426
             "fields": fields,
428 427
             "exclude": exclude,
429  
-            "formfield_callback": curry(self.formfield_for_dbfield, request=request),
  428
+            "formfield_callback": partial(self.formfield_for_dbfield, request=request),
430 429
         }
431 430
         defaults.update(kwargs)
432 431
         return modelform_factory(self.model, **defaults)
@@ -457,7 +456,7 @@ def get_changelist_form(self, request, **kwargs):
457 456
         Returns a Form class for use in the Formset on the changelist page.
458 457
         """
459 458
         defaults = {
460  
-            "formfield_callback": curry(self.formfield_for_dbfield, request=request),
  459
+            "formfield_callback": partial(self.formfield_for_dbfield, request=request),
461 460
         }
462 461
         defaults.update(kwargs)
463 462
         return modelform_factory(self.model, **defaults)
@@ -468,7 +467,7 @@ def get_changelist_formset(self, request, **kwargs):
468 467
         is used.
469 468
         """
470 469
         defaults = {
471  
-            "formfield_callback": curry(self.formfield_for_dbfield, request=request),
  470
+            "formfield_callback": partial(self.formfield_for_dbfield, request=request),
472 471
         }
473 472
         defaults.update(kwargs)
474 473
         return modelformset_factory(self.model,
@@ -1327,7 +1326,7 @@ def get_formset(self, request, obj=None, **kwargs):
1327 1326
             "fk_name": self.fk_name,
1328 1327
             "fields": fields,
1329 1328
             "exclude": exclude,
1330  
-            "formfield_callback": curry(self.formfield_for_dbfield, request=request),
  1329
+            "formfield_callback": partial(self.formfield_for_dbfield, request=request),
1331 1330
             "extra": self.extra,
1332 1331
             "max_num": self.max_num,
1333 1332
             "can_delete": self.can_delete,
2  django/contrib/admin/sites.py
... ...
@@ -1,4 +1,5 @@
1 1
 import re
  2
+from functools import update_wrapper
2 3
 from django import http, template
3 4
 from django.contrib.admin import ModelAdmin, actions
4 5
 from django.contrib.admin.forms import AdminAuthenticationForm
@@ -9,7 +10,6 @@
9 10
 from django.core.exceptions import ImproperlyConfigured
10 11
 from django.core.urlresolvers import reverse
11 12
 from django.shortcuts import render_to_response
12  
-from django.utils.functional import update_wrapper
13 13
 from django.utils.safestring import mark_safe
14 14
 from django.utils.text import capfirst
15 15
 from django.utils.translation import ugettext as _
6  django/contrib/admin/views/decorators.py
... ...
@@ -1,8 +1,4 @@
1  
-try:
2  
-    from functools import wraps
3  
-except ImportError:
4  
-    from django.utils.functional import wraps  # Python 2.4 fallback.
5  
-
  1
+from functools import wraps
6 2
 from django.utils.translation import ugettext as _
7 3
 from django.contrib.admin.forms import AdminAuthenticationForm
8 4
 from django.contrib.auth.views import login
3  django/contrib/admin/widgets.py
@@ -2,8 +2,7 @@
2 2
 Form Widget classes specific to the Django admin site.
3 3
 """
4 4
 
5  
-import django.utils.copycompat as copy
6  
-
  5
+import copy
7 6
 from django import forms
8 7
 from django.forms.widgets import RadioFieldRenderer
9 8
 from django.forms.util import flatatt
6  django/contrib/auth/decorators.py
... ...
@@ -1,9 +1,5 @@
1 1
 import urlparse
2  
-try:
3  
-    from functools import wraps
4  
-except ImportError:
5  
-    from django.utils.functional import wraps  # Python 2.4 fallback.
6  
-
  2
+from functools import wraps
7 3
 from django.conf import settings
8 4
 from django.contrib.auth import REDIRECT_FIELD_NAME
9 5
 from django.utils.decorators import available_attrs
6  django/contrib/auth/models.py
... ...
@@ -1,4 +1,5 @@
1 1
 import datetime
  2
+import hashlib
2 3
 import urllib
3 4
 
4 5
 from django.contrib import auth
@@ -8,7 +9,6 @@
8 9
 from django.db.models.manager import EmptyManager
9 10
 from django.contrib.contenttypes.models import ContentType
10 11
 from django.utils.encoding import smart_str
11  
-from django.utils.hashcompat import md5_constructor, sha_constructor
12 12
 from django.utils.translation import ugettext_lazy as _
13 13
 from django.utils.crypto import constant_time_compare
14 14
 
@@ -29,9 +29,9 @@ def get_hexdigest(algorithm, salt, raw_password):
29 29
         return crypt.crypt(raw_password, salt)
30 30
 
31 31
     if algorithm == 'md5':
32  
-        return md5_constructor(salt + raw_password).hexdigest()
  32
+        return hashlib.md5(salt + raw_password).hexdigest()
33 33
     elif algorithm == 'sha1':
34  
-        return sha_constructor(salt + raw_password).hexdigest()
  34
+        return hashlib.sha1(salt + raw_password).hexdigest()
35 35
     raise ValueError("Got unknown password algorithm type in password.")
36 36
 
37 37
 def check_password(raw_password, enc_password):
8  django/contrib/auth/tests/tokens.py
@@ -58,14 +58,14 @@ def test_django12_hash(self):
58 58
         # Hard code in the Django 1.2 algorithm (not the result, as it is time
59 59
         # dependent)
60 60
         def _make_token(user):
61  
-            from django.utils.hashcompat import sha_constructor
  61
+            import hashlib
62 62
             from django.utils.http import int_to_base36
63 63
 
64 64
             timestamp = (date.today() - date(2001,1,1)).days
65 65
             ts_b36 = int_to_base36(timestamp)
66  
-            hash = sha_constructor(settings.SECRET_KEY + unicode(user.id) +
67  
-                                   user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
68  
-                                   unicode(timestamp)).hexdigest()[::2]
  66
+            hash = hashlib.sha1(settings.SECRET_KEY + unicode(user.id) +
  67
+                               user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
  68
+                               unicode(timestamp)).hexdigest()[::2]
69 69
             return "%s-%s" % (ts_b36, hash)
70 70
 
71 71
         user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
9  django/contrib/auth/tokens.py
... ...
@@ -1,7 +1,6 @@
1 1
 from datetime import date
2  
-
  2
+import hashlib
3 3
 from django.conf import settings
4  
-from django.utils.hashcompat import sha_constructor
5 4
 from django.utils.http import int_to_base36, base36_to_int
6 5
 from django.utils.crypto import constant_time_compare, salted_hmac
7 6
 
@@ -67,9 +66,9 @@ def _make_token_with_timestamp(self, user, timestamp):
67 66
     def _make_token_with_timestamp_old(self, user, timestamp):
68 67
         # The Django 1.2 method
69 68
         ts_b36 = int_to_base36(timestamp)
70  
-        hash = sha_constructor(settings.SECRET_KEY + unicode(user.id) +
71  
-                               user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
72  
-                               unicode(timestamp)).hexdigest()[::2]
  69
+        hash = hashlib.sha1(settings.SECRET_KEY + unicode(user.id) +
  70
+                           user.password + user.last_login.strftime('%Y-%m-%d %H:%M:%S') +
  71
+                           unicode(timestamp)).hexdigest()[::2]
73 72
         return "%s-%s" % (ts_b36, hash)
74 73
 
75 74
     def _num_days(self, dt):
7  django/contrib/comments/forms.py
... ...
@@ -1,6 +1,6 @@
1  
-import time
2 1
 import datetime
3  
-
  2
+import hashlib
  3
+import time
4 4
 from django import forms
5 5
 from django.forms.util import ErrorDict
6 6
 from django.conf import settings
@@ -8,7 +8,6 @@
8 8
 from models import Comment
9 9
 from django.utils.crypto import salted_hmac, constant_time_compare
10 10
 from django.utils.encoding import force_unicode
11  
-from django.utils.hashcompat import sha_constructor
12 11
 from django.utils.text import get_text_list
13 12
 from django.utils.translation import ungettext, ugettext_lazy as _
14 13
 
@@ -100,7 +99,7 @@ def _generate_security_hash_old(self, content_type, object_pk, timestamp):
100 99
         """Generate a (SHA1) security hash from the provided info."""
101 100
         # Django 1.2 compatibility
102 101
         info = (content_type, object_pk, timestamp, settings.SECRET_KEY)
103  
-        return sha_constructor("".join(info)).hexdigest()
  102
+        return hashlib.sha1("".join(info)).hexdigest()
104 103
 
105 104
 class CommentDetailsForm(CommentSecurityForm):
106 105
     """
8  django/contrib/contenttypes/generic.py
@@ -2,6 +2,7 @@
2 2
 Classes allowing "generic" relations through ContentType and object-id fields.
3 3
 """
4 4
 
  5
+from functools import partial
5 6
 from django.core.exceptions import ObjectDoesNotExist
6 7
 from django.db import connection
7 8
 from django.db.models import signals
@@ -11,11 +12,8 @@
11 12
 from django.forms import ModelForm
12 13
 from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance
13 14
 from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
14  
-from django.utils.encoding import smart_unicode
15  
-from django.utils.functional import curry
16  
-
17 15
 from django.contrib.contenttypes.models import ContentType
18  
-
  16
+from django.utils.encoding import smart_unicode
19 17
 
20 18
 class GenericForeignKey(object):
21 19
     """
@@ -414,7 +412,7 @@ def get_formset(self, request, obj=None):
414 412
             "ct_field": self.ct_field,
415 413
             "fk_field": self.ct_fk_field,
416 414
             "form": self.form,
417  
-            "formfield_callback": curry(self.formfield_for_dbfield, request=request),
  415
+            "formfield_callback": partial(self.formfield_for_dbfield, request=request),
418 416
             "formset": self.formset,
419 417
             "extra": self.extra,
420 418
             "can_delete": self.can_delete,
6  django/contrib/formtools/preview.py
@@ -2,13 +2,15 @@
2 2
 Formtools Preview application.
3 3
 """
4 4
 
5  
-import cPickle as pickle
  5
+try:
  6
+    import cPickle as pickle
  7
+except ImportError:
  8
+    import pickle
6 9
 
7 10
 from django.conf import settings
8 11
 from django.http import Http404
9 12
 from django.shortcuts import render_to_response
10 13
 from django.template.context import RequestContext
11  
-from django.utils.hashcompat import md5_constructor
12 14
 from django.utils.crypto import constant_time_compare
13 15
 from django.contrib.formtools.utils import security_hash
14 16
 
4  django/contrib/formtools/utils.py
@@ -3,10 +3,10 @@
3 3
 except ImportError:
4 4
     import pickle
5 5
 
  6
+import hashlib
6 7
 from django.conf import settings
7 8
 from django.forms import BooleanField
8 9
 from django.utils.crypto import salted_hmac
9  
-from django.utils.hashcompat import md5_constructor
10 10
 
11 11
 
12 12
 def security_hash(request, form, *args):
@@ -39,7 +39,7 @@ def security_hash(request, form, *args):
39 39
     # Python 2.3, but Django requires 2.4 anyway, so that's OK.
40 40
     pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
41 41
 
42  
-    return md5_constructor(pickled).hexdigest()
  42
+    return hashlib.md5(pickled).hexdigest()
43 43
 
44 44
 
45 45
 def form_hmac(form):
6  django/contrib/formtools/wizard.py
@@ -4,7 +4,10 @@
4 4
 stored on the server side.
5 5
 """
6 6
 
7  
-import cPickle as pickle
  7
+try:
  8
+    import cPickle as pickle
  9
+except ImportError:
  10
+    import pickle
8 11
 
9 12
 from django import forms
10 13
 from django.conf import settings
@@ -13,7 +16,6 @@
13 16
 from django.shortcuts import render_to_response
14 17
 from django.template.context import RequestContext
15 18
 from django.utils.crypto import constant_time_compare
16  
-from django.utils.hashcompat import md5_constructor
17 19
 from django.utils.translation import ugettext_lazy as _
18 20
 from django.utils.decorators import method_decorator
19 21
 from django.views.decorators.csrf import csrf_protect
2  django/contrib/gis/geos/tests/test_geos.py
@@ -820,7 +820,7 @@ def test21_test_gdal(self):
820 820
 
821 821
     def test22_copy(self):
822 822
         "Testing use with the Python `copy` module."
823  
-        import django.utils.copycompat as copy
  823
+        import copy
824 824
         poly = GEOSGeometry('POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))')
825 825
         cpy1 = copy.copy(poly)
826 826
         cpy2 = copy.deepcopy(poly)
3  django/contrib/gis/geos/tests/test_geos_mutation.py
@@ -2,8 +2,7 @@
2 2
 # Modified from original contribution by Aryeh Leib Taurog, which was
3 3
 # released under the New BSD license.
4 4
 
5  
-import django.utils.copycompat as copy
6  
-
  5
+import copy
7 6
 from django.contrib.gis.geos import *
8 7
 from django.contrib.gis.geos.error import GEOSIndexError
9 8
 from django.utils import unittest
2  django/contrib/gis/tests/layermap/tests.py
... ...
@@ -1,7 +1,7 @@
  1
+import copy
1 2
 import os
2 3
 from decimal import Decimal
3 4
 
4  
-from django.utils.copycompat import copy
5 5
 from django.utils.unittest import TestCase
6 6
 
7 7
 from django.contrib.gis.gdal import DataSource, OGRException
6  django/contrib/sessions/backends/base.py
... ...
@@ -1,4 +1,5 @@
1 1
 import base64
  2
+import hashlib
2 3
 import os
3 4
 import random
4 5
 import sys
@@ -11,7 +12,6 @@
11 12
 
12 13
 from django.conf import settings
13 14
 from django.core.exceptions import SuspiciousOperation
14  
-from django.utils.hashcompat import md5_constructor
15 15
 from django.utils.crypto import constant_time_compare, salted_hmac
16 16
 
17 17
 # Use the system (hardware-based) random number generator if it exists.
@@ -119,7 +119,7 @@ def decode(self, session_data):
119 119
     def _decode_old(self, session_data):
120 120
         encoded_data = base64.decodestring(session_data)
121 121
         pickled, tamper_check = encoded_data[:-32], encoded_data[-32:]
122  
-        if not constant_time_compare(md5_constructor(pickled + settings.SECRET_KEY).hexdigest(),
  122
+        if not constant_time_compare(hashlib.md5(pickled + settings.SECRET_KEY).hexdigest(),
123 123
                                      tamper_check):
124 124
             raise SuspiciousOperation("User tampered with session cookie.")
125 125
         return pickle.loads(pickled)
@@ -161,7 +161,7 @@ def _get_new_session_key(self):
161 161
             # No getpid() in Jython, for example
162 162
             pid = 1
163 163
         while 1:
164  
-            session_key = md5_constructor("%s%s%s%s"
  164
+            session_key = hashlib.md5("%s%s%s%s"
165 165
                     % (randrange(0, MAX_SESSION_KEY), pid, time.time(),
166 166
                        settings.SECRET_KEY)).hexdigest()
167 167
             if not self.exists(session_key):
4  django/contrib/sessions/tests.py
... ...
@@ -1,5 +1,6 @@
1 1
 import base64
2 2
 from datetime import datetime, timedelta
  3
+import hashlib
3 4
 import pickle
4 5
 import shutil
5 6
 import tempfile
@@ -15,7 +16,6 @@
15 16
 from django.http import HttpResponse
16 17
 from django.test import TestCase, RequestFactory
17 18
 from django.utils import unittest
18  
-from django.utils.hashcompat import md5_constructor
19 19
 
20 20
 
21 21
 class SessionTestsMixin(object):
@@ -257,7 +257,7 @@ def test_decode_django12(self):
257 257
         # Hard code the Django 1.2 method here:
258 258
         def encode(session_dict):
259 259
             pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
260  
-            pickled_md5 = md5_constructor(pickled + settings.SECRET_KEY).hexdigest()
  260
+            pickled_md5 = hashlib.md5(pickled + settings.SECRET_KEY).hexdigest()
261 261
             return base64.encodestring(pickled + pickled_md5)
262 262
 
263 263
         data = {'a test key': 'a test value'}
6  django/core/cache/backends/filebased.py
... ...
@@ -1,15 +1,15 @@
1 1
 "File-based cache backend"
2 2
 
  3
+import hashlib
3 4
 import os
4  
-import time
5 5
 import shutil
  6
+import time
6 7
 try:
7 8
     import cPickle as pickle
8 9
 except ImportError:
9 10
     import pickle
10 11
 
11 12
 from django.core.cache.backends.base import BaseCache
12  
-from django.utils.hashcompat import md5_constructor
13 13
 
14 14
 class FileBasedCache(BaseCache):
15 15
     def __init__(self, dir, params):
@@ -145,7 +145,7 @@ def _key_to_file(self, key):
145 145
         Thus, a cache key of "foo" gets turnned into a file named
146 146
         ``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``.
147 147
         """
148  
-        path = md5_constructor(key).hexdigest()
  148
+        path = hashlib.md5(key).hexdigest()
149 149
         path = os.path.join(path[:2], path[2:4], path[4:])
150 150
         return os.path.join(self._dir, path)
151 151
 
8  django/core/cache/backends/locmem.py
@@ -77,12 +77,10 @@ def set(self, key, value, timeout=None, version=None):
77 77
         key = self.make_key(key, version=version)
78 78
         self.validate_key(key)
79 79
         self._lock.writer_enters()
80  
-        # Python 2.4 doesn't allow combined try-except-finally blocks.
81 80
         try:
82  
-            try:
83  
-                self._set(key, pickle.dumps(value), timeout)
84  
-            except pickle.PickleError:
85  
-                pass
  81
+            self._set(key, pickle.dumps(value), timeout)
  82
+        except pickle.PickleError:
  83
+            pass
86 84
         finally:
87 85
             self._lock.writer_leaves()
88 86
 

0 notes on commit a87be35

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