summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2007-02-15 04:13:02 +0000
committerAdrian Holovaty <adrian@holovaty.com>2007-02-15 04:13:02 +0000
commita13a47e447c925ad39f0c3a4bd5c2cddfc74d3d3 (patch)
treeeda7c03164d02f2ce1e5e56487e96a31dfeae625
parent6245816dd90b8b0b9b8739f1fc292547a1a55639 (diff)
Fixed #3314 -- Fixed a bug in newforms smart_unicode. Thanks for the patch, nesh
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4522 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/newforms/util.py5
-rw-r--r--tests/regressiontests/forms/tests.py23
2 files changed, 27 insertions, 1 deletions
diff --git a/django/newforms/util.py b/django/newforms/util.py
index 2fec9e4e2e..51a8efdde2 100644
--- a/django/newforms/util.py
+++ b/django/newforms/util.py
@@ -7,7 +7,10 @@ flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs
def smart_unicode(s):
if not isinstance(s, basestring):
- s = unicode(str(s))
+ if hasattr(s, '__unicode__'):
+ s = unicode(s)
+ else:
+ s = unicode(str(s), settings.DEFAULT_CHARSET)
elif not isinstance(s, unicode):
s = unicode(s, settings.DEFAULT_CHARSET)
return s
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index 41890e86ef..644f922d28 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -3265,6 +3265,29 @@ ValidationError: [u'Enter a zip code in the format XXXXX or XXXXX-XXXX.']
u''
>>> f.clean('')
u''
+
+#################################
+# Tests of underlying functions #
+#################################
+
+# smart_unicode tests
+>>> from django.newforms.util import smart_unicode
+>>> class Test:
+... def __str__(self):
+... return 'ŠĐĆŽćžšđ'
+>>> class TestU:
+... def __str__(self):
+... return 'Foo'
+... def __unicode__(self):
+... return u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
+>>> smart_unicode(Test())
+u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
+>>> smart_unicode(TestU())
+u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
+>>> smart_unicode(1)
+u'1'
+>>> smart_unicode('foo')
+u'foo'
"""
if __name__ == "__main__":