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
This commit is contained in:
Adrian Holovaty 2007-02-15 04:13:02 +00:00
parent 6245816dd9
commit a13a47e447
2 changed files with 27 additions and 1 deletions

View File

@ -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

View File

@ -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__":