Commit 589dc49e authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Fixed #21198 -- Prevented invalid use of @python_2_unicode_compatible.

Thanks jpic for the report and chmodas for working on a patch.

Reverts 2ea80b94. Refs #19362.
parent 570d9c26
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -450,11 +450,6 @@ class Model(six.with_metaclass(ModelBase)):

    def __str__(self):
        if six.PY2 and hasattr(self, '__unicode__'):
            if type(self).__unicode__ == Model.__str__:
                klass_name = type(self).__name__
                raise RuntimeError("%s.__unicode__ is aliased to __str__. Did"
                                   " you apply @python_2_unicode_compatible"
                                   " without defining __str__?" % klass_name)
            return force_text(self).encode('utf-8')
        return '%s object' % self.__class__.__name__

+4 −0
Original line number Diff line number Diff line
@@ -28,6 +28,10 @@ def python_2_unicode_compatible(klass):
    returning text and apply this decorator to the class.
    """
    if six.PY2:
        if '__str__' not in klass.__dict__:
            raise ValueError("@python_2_unicode_compatible cannot be applied "
                             "to %s because it doesn't define __str__()." %
                             klass.__name__)
        klass.__unicode__ = klass.__str__
        klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
    return klass
+0 −3
Original line number Diff line number Diff line
from django.db import models
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class Comment(models.Model):
    pass


@python_2_unicode_compatible
class Book(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    comments = models.ManyToManyField(Comment)
+0 −8
Original line number Diff line number Diff line
@@ -27,14 +27,6 @@ class Article(models.Model):
        # in ASCII.
        return self.headline

@python_2_unicode_compatible
class BrokenArticle(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateTimeField()

    def __unicode__(self):      # instead of __str__
        return self.headline

@python_2_unicode_compatible
class InternationalArticle(models.Model):
    headline = models.CharField(max_length=100)
+1 −11
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ from unittest import skipIf
from django.test import TestCase
from django.utils import six

from .models import Article, BrokenArticle, InternationalArticle
from .models import Article, InternationalArticle


class SimpleTests(TestCase):
@@ -21,16 +21,6 @@ class SimpleTests(TestCase):
        self.assertEqual(str(a), str('Area man programs in Python'))
        self.assertEqual(repr(a), str('<Article: Area man programs in Python>'))

    @skipIf(six.PY3, "tests Model's default __str__ method under Python 2")
    def test_broken(self):
        # Regression test for #19362.
        a = BrokenArticle.objects.create(
            headline='Girl wins €12.500 in lottery',
            pub_date=datetime.datetime(2005, 7, 28)
        )
        six.assertRaisesRegex(self, RuntimeError, "Did you apply "
            "@python_2_unicode_compatible without defining __str__\?", str, a)

    def test_international(self):
        a = InternationalArticle.objects.create(
            headline='Girl wins €12.500 in lottery',
Loading