Django

Code

Changeset 4522

Show
Ignore:
Timestamp:
02/14/07 22:13:02 (2 years ago)
Author:
adrian
Message:

Fixed #3314 -- Fixed a bug in newforms smart_unicode. Thanks for the patch, nesh

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/util.py

    r4370 r4522  
    88def smart_unicode(s): 
    99    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) 
    1114    elif not isinstance(s, unicode): 
    1215        s = unicode(s, settings.DEFAULT_CHARSET) 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4521 r4522  
    32663266>>> f.clean('') 
    32673267u'' 
     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()) 
     3284u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' 
     3285>>> smart_unicode(TestU()) 
     3286u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' 
     3287>>> smart_unicode(1) 
     3288u'1' 
     3289>>> smart_unicode('foo') 
     3290u'foo' 
    32683291""" 
    32693292