From a13a47e447c925ad39f0c3a4bd5c2cddfc74d3d3 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 15 Feb 2007 04:13:02 +0000 Subject: [PATCH] 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 --- django/newforms/util.py | 5 ++++- tests/regressiontests/forms/tests.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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__":