From da9cf53cb536511a8abaade9f20e1dd8c4a02c7d Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 5 May 2014 20:39:26 +0200 Subject: [PATCH] Fixed #22564 -- Prevented unneeded bytestrings in migrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some cases, this could lead to migrations written with Python 2 being incompatible with Python 3. Thanks Tim Graham for the report and Loïc Bistuer for the advices. --- django/db/migrations/state.py | 3 ++- django/db/migrations/writer.py | 2 ++ django/db/models/fields/__init__.py | 2 +- tests/field_deconstruction/tests.py | 9 ++++++--- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 92c7a52ef4..a5ea1cf37c 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -6,6 +6,7 @@ from django.db import models from django.db.models.options import DEFAULT_NAMES, normalize_together from django.db.models.fields.related import do_pending_lookups from django.utils import six +from django.utils.encoding import force_text from django.utils.module_loading import import_string @@ -132,7 +133,7 @@ class ModelState(object): def __init__(self, app_label, name, fields, options=None, bases=None): self.app_label = app_label - self.name = name + self.name = force_text(name) self.fields = fields self.options = options or {} self.bases = bases or (models.Model, ) diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 7696aaf339..0e2eeef85a 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -124,6 +124,8 @@ class MigrationWriter(object): dependencies.append(" migrations.swappable_dependency(settings.%s)," % dependency[1]) imports.add("from django.conf import settings") else: + # No need to output bytestrings for dependencies + dependency = tuple([force_text(s) for s in dependency]) dependencies.append(" %s," % self.serialize(dependency)[0]) items["dependencies"] = "\n".join(dependencies) + "\n" if dependencies else "" diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index caf0311cc3..77e233f7e8 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -370,7 +370,7 @@ class Field(RegisterLookupMixin): path = path.replace("django.db.models.fields", "django.db.models") # Return basic info - other fields should override this. return ( - self.name, + force_text(self.name, strings_only=True), path, [], keywords, diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py index 10cf342529..2aaaad988b 100644 --- a/tests/field_deconstruction/tests.py +++ b/tests/field_deconstruction/tests.py @@ -1,6 +1,8 @@ import warnings -from django.test import TestCase, override_settings + from django.db import models +from django.test import TestCase, override_settings +from django.utils import six class FieldDeconstructionTests(TestCase): @@ -15,14 +17,15 @@ class FieldDeconstructionTests(TestCase): # First try using a "normal" field field = models.CharField(max_length=65) name, path, args, kwargs = field.deconstruct() - self.assertEqual(name, None) + self.assertIsNone(name) field.set_attributes_from_name("is_awesome_test") name, path, args, kwargs = field.deconstruct() self.assertEqual(name, "is_awesome_test") + self.assertIsInstance(name, six.text_type) # Now try with a ForeignKey field = models.ForeignKey("some_fake.ModelName") name, path, args, kwargs = field.deconstruct() - self.assertEqual(name, None) + self.assertIsNone(name) field.set_attributes_from_name("author") name, path, args, kwargs = field.deconstruct() self.assertEqual(name, "author")