Ticket #18063: 18063-no-unicode-repr.diff

File 18063-no-unicode-repr.diff, 2.5 KB (added by Tai Lee, 5 years ago)
  • django/db/models/base.py

    From 1b6ea4f0baa064e45e0a119bb1946a6ad5823b0d Mon Sep 17 00:00:00 2001
    From: Tai Lee <tai.lee@3030.com.au>
    Date: Mon, 20 Aug 2012 14:07:59 +1000
    Subject: [PATCH] Fixed #18063 -- Encode `__repr__()` as ASCII in 2.x,
     replacing errors.
    
    ---
     django/db/models/base.py                     |  6 ++++--
     tests/regressiontests/model_regress/tests.py | 10 ++++++++++
     2 files changed, 14 insertions(+), 2 deletions(-)
    
    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index fd7250c..5e3b81d 100644
    a b from django.db.models import signals 
    2323from django.db.models.loading import register_models, get_model
    2424from django.utils.translation import ugettext_lazy as _
    2525from django.utils.functional import curry
    26 from django.utils.encoding import smart_str, force_text
     26from django.utils.encoding import smart_text, force_text
    2727from django.utils import six
    2828from django.utils.text import get_text_list, capfirst
    2929
    class Model(six.with_metaclass(ModelBase, object)): 
    404404            u = six.text_type(self)
    405405        except (UnicodeEncodeError, UnicodeDecodeError):
    406406            u = '[Bad Unicode data]'
    407         return smart_str('<%s: %s>' % (self.__class__.__name__, u))
     407        if not six.PY3:
     408            u = u.encode('ascii', 'replace')
     409        return smart_text('<%s: %s>' % (self.__class__.__name__, u))
    408410
    409411    def __str__(self):
    410412        if not six.PY3 and hasattr(self, '__unicode__'):
  • tests/regressiontests/model_regress/tests.py

    diff --git a/tests/regressiontests/model_regress/tests.py b/tests/regressiontests/model_regress/tests.py
    index 6a45a83..6a03b86 100644
    a b  
     1# coding: utf-8
     2
    13from __future__ import absolute_import, unicode_literals
    24
    35import datetime
    class ModelTests(TestCase): 
    146148        b = BrokenUnicodeMethod.objects.create(name="Jerry")
    147149        self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>")
    148150
     151    def test_no_unicode_in_repr(self):
     152        a = Article.objects.create(
     153            headline="Watch for umlauts: üöä", pub_date=datetime.datetime.now())
     154        if six.PY3:
     155            self.assertEqual(repr(a), '<Article: Watch for umlauts: üöä>')
     156        else:
     157            self.assertEqual(repr(a), '<Article: Watch for umlauts: ???>')
     158
    149159    @skipUnlessDBFeature("supports_timezones")
    150160    def test_timezones(self):
    151161        # Saving an updating with timezone-aware datetime Python objects.
Back to Top