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
|
23 | 23 | from django.db.models.loading import register_models, get_model |
24 | 24 | from django.utils.translation import ugettext_lazy as _ |
25 | 25 | from django.utils.functional import curry |
26 | | from django.utils.encoding import smart_str, force_text |
| 26 | from django.utils.encoding import smart_text, force_text |
27 | 27 | from django.utils import six |
28 | 28 | from django.utils.text import get_text_list, capfirst |
29 | 29 | |
… |
… |
class Model(six.with_metaclass(ModelBase, object)):
|
404 | 404 | u = six.text_type(self) |
405 | 405 | except (UnicodeEncodeError, UnicodeDecodeError): |
406 | 406 | 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)) |
408 | 410 | |
409 | 411 | def __str__(self): |
410 | 412 | if not six.PY3 and hasattr(self, '__unicode__'): |
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 | |
1 | 3 | from __future__ import absolute_import, unicode_literals |
2 | 4 | |
3 | 5 | import datetime |
… |
… |
class ModelTests(TestCase):
|
146 | 148 | b = BrokenUnicodeMethod.objects.create(name="Jerry") |
147 | 149 | self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>") |
148 | 150 | |
| 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 | |
149 | 159 | @skipUnlessDBFeature("supports_timezones") |
150 | 160 | def test_timezones(self): |
151 | 161 | # Saving an updating with timezone-aware datetime Python objects. |