Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit a13a47e447c925ad39f0c3a4bd5c2cddfc74d3d3 1 parent 6245816
Adrian Holovaty authored February 15, 2007
5  django/newforms/util.py
@@ -7,7 +7,10 @@
7 7
 
8 8
 def smart_unicode(s):
9 9
     if not isinstance(s, basestring):
10  
-        s = unicode(str(s))
  10
+        if hasattr(s, '__unicode__'):
  11
+            s = unicode(s)
  12
+        else:
  13
+            s = unicode(str(s), settings.DEFAULT_CHARSET)
11 14
     elif not isinstance(s, unicode):
12 15
         s = unicode(s, settings.DEFAULT_CHARSET)
13 16
     return s
23  tests/regressiontests/forms/tests.py
@@ -3265,6 +3265,29 @@
3265 3265
 u''
3266 3266
 >>> f.clean('')
3267 3267
 u''
  3268
+
  3269
+#################################
  3270
+# Tests of underlying functions #
  3271
+#################################
  3272
+
  3273
+# smart_unicode tests
  3274
+>>> from django.newforms.util import smart_unicode
  3275
+>>> class Test:
  3276
+...     def __str__(self):
  3277
+...        return 'ŠĐĆŽćžšđ'
  3278
+>>> class TestU:
  3279
+...     def __str__(self):
  3280
+...        return 'Foo'
  3281
+...     def __unicode__(self):
  3282
+...        return u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
  3283
+>>> smart_unicode(Test())
  3284
+u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
  3285
+>>> smart_unicode(TestU())
  3286
+u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111'
  3287
+>>> smart_unicode(1)
  3288
+u'1'
  3289
+>>> smart_unicode('foo')
  3290
+u'foo'
3268 3291
 """
3269 3292
 
3270 3293
 if __name__ == "__main__":

0 notes on commit a13a47e

Please sign in to comment.
Something went wrong with that request. Please try again.