Skip to content

Commit d11d45a

Browse files
committed
[py3] Used six.with_metaclass wherever necessary.
1 parent 7fa51a2 commit d11d45a

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

django/contrib/admin/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from django.utils.datastructures import SortedDict
2525
from django.utils.html import escape, escapejs
2626
from django.utils.safestring import mark_safe
27+
from django.utils import six
2728
from django.utils.text import capfirst, get_text_list
2829
from django.utils.translation import ugettext as _
2930
from django.utils.translation import ungettext
@@ -57,9 +58,8 @@ class IncorrectLookupParameters(Exception):
5758

5859
csrf_protect_m = method_decorator(csrf_protect)
5960

60-
class BaseModelAdmin(object):
61+
class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
6162
"""Functionality common to both ModelAdmin and InlineAdmin."""
62-
__metaclass__ = forms.MediaDefiningClass
6363

6464
raw_id_fields = ()
6565
fields = None

django/db/models/base.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from django.utils.translation import ugettext_lazy as _
2525
from django.utils.functional import curry
2626
from django.utils.encoding import smart_str, force_unicode
27+
from django.utils import six
2728
from django.utils.text import get_text_list, capfirst
2829

2930

@@ -275,8 +276,8 @@ def __init__(self, db=None):
275276
# This impacts validation only; it has no effect on the actual save.
276277
self.adding = True
277278

278-
class Model(object):
279-
__metaclass__ = ModelBase
279+
280+
class ModelWithoutMeta(object):
280281
_deferred = False
281282

282283
def __init__(self, *args, **kwargs):
@@ -369,7 +370,7 @@ def __init__(self, *args, **kwargs):
369370
pass
370371
if kwargs:
371372
raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
372-
super(Model, self).__init__()
373+
super(ModelWithoutMeta, self).__init__()
373374
signals.post_init.send(sender=self.__class__, instance=self)
374375

375376
def __repr__(self):
@@ -401,7 +402,7 @@ def __reduce__(self):
401402
only module-level classes can be pickled by the default path.
402403
"""
403404
if not self._deferred:
404-
return super(Model, self).__reduce__()
405+
return super(ModelWithoutMeta, self).__reduce__()
405406
data = self.__dict__
406407
defers = []
407408
for field in self._meta.fields:
@@ -876,6 +877,15 @@ def clean_fields(self, exclude=None):
876877
raise ValidationError(errors)
877878

878879

880+
# For unknown reasons, six.with_metaclass doesn't work correctly for Model.
881+
# Fallback to exec'ing the appropriate syntax for each Python version.
882+
883+
if six.PY3:
884+
six.exec_("class Model(ModelWithoutMeta, metaclass=ModelBase): pass")
885+
else:
886+
six.exec_("class Model(ModelWithoutMeta): __metaclass__ = ModelBase")
887+
888+
879889
############################################
880890
# HELPER FUNCTIONS (CURRIED MODEL METHODS) #
881891
############################################

django/forms/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.utils.html import conditional_escape, format_html
1515
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
1616
from django.utils.safestring import mark_safe
17+
from django.utils import six
1718

1819

1920
__all__ = ('BaseForm', 'Form')
@@ -380,14 +381,13 @@ def visible_fields(self):
380381
"""
381382
return [field for field in self if not field.is_hidden]
382383

383-
class Form(BaseForm):
384+
class Form(six.with_metaclass(DeclarativeFieldsMetaclass, BaseForm)):
384385
"A collection of Fields, plus their associated data."
385386
# This is a separate class from BaseForm in order to abstract the way
386387
# self.fields is specified. This class (Form) is the one that does the
387388
# fancy metaclass stuff purely for the semantic sugar -- it allows one
388389
# to define a form using declarative syntax.
389390
# BaseForm itself has no way of designating self.fields.
390-
__metaclass__ = DeclarativeFieldsMetaclass
391391

392392
class BoundField(StrAndUnicode):
393393
"A Field plus data"

django/forms/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
MultipleHiddenInput, media_property)
1616
from django.utils.encoding import smart_unicode, force_unicode
1717
from django.utils.datastructures import SortedDict
18+
from django.utils import six
1819
from django.utils.text import get_text_list, capfirst
1920
from django.utils.translation import ugettext_lazy as _, ugettext
2021

@@ -365,8 +366,8 @@ def save(self, commit=True):
365366

366367
save.alters_data = True
367368

368-
class ModelForm(BaseModelForm):
369-
__metaclass__ = ModelFormMetaclass
369+
class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)):
370+
pass
370371

371372
def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
372373
formfield_callback=None, widgets=None):
@@ -401,6 +402,7 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
401402

402403
form_metaclass = ModelFormMetaclass
403404

405+
# TODO: this doesn't work under Python 3.
404406
if issubclass(form, BaseModelForm) and hasattr(form, '__metaclass__'):
405407
form_metaclass = form.__metaclass__
406408

django/forms/widgets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from django.utils.encoding import StrAndUnicode, force_unicode
1818
from django.utils.safestring import mark_safe
1919
from django.utils import datetime_safe, formats
20+
from django.utils import six
2021

2122
__all__ = (
2223
'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
@@ -153,8 +154,7 @@ def __unicode__(self):
153154
args.append(self.choices)
154155
return self.parent_widget.render(*args)
155156

156-
class Widget(object):
157-
__metaclass__ = MediaDefiningClass
157+
class Widget(six.with_metaclass(MediaDefiningClass)):
158158
is_hidden = False # Determines whether this corresponds to an <input type="hidden">.
159159
needs_multipart_form = False # Determines does this widget need multipart form
160160
is_localized = False

0 commit comments

Comments
 (0)