Django

Code

Changeset 6649

Show
Ignore:
Timestamp:
11/03/07 22:37:04 (8 months ago)
Author:
mtredinnick
Message:

Fixed #5640 -- Added some extra error reporting when smart_unicode() or
force_unicode() raise a UnicodeDecodeError?. This should at least help people
identify which is the bad piece of data they passed in. About the best we can
do here.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/encoding.py

    r6567 r6649  
    22import urllib 
    33import datetime 
     4 
    45from django.utils.functional import Promise 
     6 
     7class DjangoUnicodeDecodeError(UnicodeDecodeError): 
     8    def __init__(self, obj, *args): 
     9        self.obj = obj 
     10        UnicodeDecodeError.__init__(self, *args) 
     11 
     12    def __str__(self): 
     13        original = UnicodeDecodeError.__str__(self) 
     14        return '%s. You passed in %r (%s)' % (original, self.obj, 
     15                type(self.obj)) 
    516 
    617class StrAndUnicode(object): 
     
    3445    if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.date, datetime.time, float)): 
    3546        return s 
    36     if not isinstance(s, basestring,): 
    37         if hasattr(s, '__unicode__'): 
    38             s = unicode(s) 
    39         else: 
    40             s = unicode(str(s), encoding, errors) 
    41     elif not isinstance(s, unicode): 
    42         s = unicode(s, encoding, errors) 
     47    try: 
     48        if not isinstance(s, basestring,): 
     49            if hasattr(s, '__unicode__'): 
     50                s = unicode(s) 
     51            else: 
     52                s = unicode(str(s), encoding, errors) 
     53        elif not isinstance(s, unicode): 
     54            s = unicode(s, encoding, errors) 
     55    except UnicodeDecodeError, e: 
     56        raise DjangoUnicodeDecodeError(s, *e.args) 
    4357    return s 
    4458