-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 3 compatibility #60
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bcf072d
Initial py3 compatibility.
menno-tgho dec8003
Py3 compatibility
menno-tgho 8e7ba8d
Python 3 compatibility changes
menno-tgho f3cc408
Py3 compatibility: Fixed up imports based on feedback.
menno-tgho 6590de1
Py3 compatibility: found a few more locations where compatibility was…
menno-tgho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from django.utils.six.moves import cPickle as pickle | ||
import datetime | ||
import six | ||
|
||
from django.conf import settings | ||
from django.db.backends import ( | ||
|
@@ -11,8 +11,16 @@ | |
BaseDatabaseIntrospection) | ||
from django.db.utils import DatabaseError | ||
from django.utils.functional import Promise | ||
from django.utils.safestring import EscapeString, EscapeUnicode, SafeString, \ | ||
SafeUnicode | ||
from django.utils.safestring import EscapeString, SafeString | ||
|
||
if six.PY3: | ||
import pickle | ||
EscapeUnicode = EscapeString | ||
SafeUnicode = SafeString | ||
StandardError = Exception | ||
else: | ||
import cPickle as pickle | ||
from django.utils.safestring import EscapeUnicode, SafeUnicode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not continue to import pickle from django.utils.six.moves? |
||
from django.utils import timezone | ||
|
||
from .creation import NonrelDatabaseCreation | ||
|
@@ -435,7 +443,7 @@ def _value_for_db_collection(self, value, field, field_kind, db_type, | |
value = ( | ||
(key, self._value_for_db(subvalue, subfield, | ||
subkind, db_subtype, lookup)) | ||
for key, subvalue in value.iteritems()) | ||
for key, subvalue in six.iteritems(value)) | ||
|
||
# Return just a dict, a once-flattened list; | ||
if db_type == 'dict': | ||
|
@@ -492,7 +500,7 @@ def _value_from_db_collection(self, value, field, field_kind, db_type): | |
if db_type == 'list': | ||
value = zip(value[::2], value[1::2]) | ||
else: | ||
value = value.iteritems() | ||
value = six.iteritems(value) | ||
|
||
# DictField needs to hold a dict. | ||
return dict( | ||
|
@@ -551,7 +559,7 @@ def _value_for_db_model(self, value, field, field_kind, db_type, lookup): | |
value = ( | ||
(subfield.column, self._value_for_db( | ||
subvalue, lookup=lookup, *self._convert_as(subfield, lookup))) | ||
for subfield, subvalue in value.iteritems()) | ||
for subfield, subvalue in six.iteritems(value)) | ||
|
||
# Cast to a dict, interleave columns with values on a list, | ||
# serialize, or return a generator. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from django.db.models.fields.subclassing import Creator | ||
from django.db.utils import IntegrityError | ||
from django.db.models.fields.related import add_lazy_relation | ||
from django.utils import six | ||
|
||
|
||
__all__ = ('RawField', 'ListField', 'SetField', 'DictField', | ||
|
@@ -81,11 +82,11 @@ def contribute_to_class(self, cls, name): | |
super(AbstractIterableField, self).contribute_to_class(cls, name) | ||
|
||
# If items' field uses SubfieldBase we also need to. | ||
item_metaclass = getattr(self.item_field, '__metaclass__', None) | ||
if item_metaclass and issubclass(item_metaclass, models.SubfieldBase): | ||
item_metaclass = type(self.item_field) | ||
if issubclass(item_metaclass, models.SubfieldBase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the |
||
setattr(cls, self.name, Creator(self)) | ||
|
||
if isinstance(self.item_field, models.ForeignKey) and isinstance(self.item_field.rel.to, basestring): | ||
if isinstance(self.item_field, models.ForeignKey) and isinstance(self.item_field.rel.to, six.string_types): | ||
""" | ||
If rel.to is a string because the actual class is not yet defined, look up the | ||
actual class later. Refer to django.models.fields.related.RelatedField.contribute_to_class. | ||
|
@@ -225,15 +226,15 @@ def get_internal_type(self): | |
|
||
def _map(self, function, iterable, *args, **kwargs): | ||
return self._type((key, function(value, *args, **kwargs)) | ||
for key, value in iterable.iteritems()) | ||
for key, value in six.iteritems(iterable)) | ||
|
||
def validate(self, values, model_instance): | ||
if not isinstance(values, dict): | ||
raise ValidationError("Value is of type %r. Should be a dict." % | ||
type(values)) | ||
|
||
|
||
class EmbeddedModelField(models.Field): | ||
class EmbeddedModelField(six.with_metaclass(models.SubfieldBase, models.Field)): | ||
""" | ||
Field that allows you to embed a model instance. | ||
|
||
|
@@ -245,8 +246,6 @@ class EmbeddedModelField(models.Field): | |
the embedded instance (not just pre_save, get_db_prep_* and | ||
to_python). | ||
""" | ||
__metaclass__ = models.SubfieldBase | ||
|
||
def __init__(self, embedded_model=None, *args, **kwargs): | ||
self.embedded_model = embedded_model | ||
kwargs.setdefault('default', None) | ||
|
@@ -271,7 +270,7 @@ def _set_model(self, model): | |
our "model" attribute in its contribute_to_class method). | ||
""" | ||
self._model = model | ||
if model is not None and isinstance(self.embedded_model, basestring): | ||
if model is not None and isinstance(self.embedded_model, six.string_types): | ||
|
||
def _resolve_lookup(self_, resolved_model, model): | ||
self.embedded_model = resolved_model | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from django.utils import six
?