Django

Code

Changeset 8588

Show
Ignore:
Timestamp:
08/26/08 13:53:51 (3 months ago)
Author:
mtredinnick
Message:

Fixed #6353 (again) by making force_unicode() and smart_str() a bit more robust
in the face of funky Exception instances. This is slightly symptomatic of
problems in the calling code, but we don't want to raise a secondary exception
whilst trying to display the first one. Based on a patch from Karen Tracey.

Files:

Legend:

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

    r8046 r8588  
    4949                s = unicode(s) 
    5050            else: 
    51                 s = unicode(str(s), encoding, errors) 
     51                try: 
     52                    s = unicode(str(s), encoding, errors) 
     53                except UnicodeEncodeError: 
     54                    if not isinstance(s, Exception): 
     55                        raise 
     56                    # If we get to here, the caller has passed in an Exception 
     57                    # subclass populated with non-ASCII data without special 
     58                    # handling to display as a string. We need to handle this 
     59                    # without raising a further exception. We do an 
     60                    # approximation to what the Exception's standard str() 
     61                    # output should be. 
     62                    s = ' '.join([force_unicode(arg, encoding, strings_only, 
     63                            errors) for arg in s]) 
    5264        elif not isinstance(s, unicode): 
    5365            # Note: We use .decode() here, instead of unicode(s, encoding, 
     
    7385            return str(s) 
    7486        except UnicodeEncodeError: 
     87            if isinstance(s, Exception): 
     88                # An Exception subclass containing non-ASCII data that doesn't 
     89                # know how to print itself properly. We shouldn't raise a 
     90                # further exception. 
     91                return ' '.join([smart_str(arg, encoding, strings_only, 
     92                        errors) for arg in s]) 
    7593            return unicode(s).encode(encoding, errors) 
    7694    elif isinstance(s, unicode):