1 import types |
1 import types |
2 import urllib |
2 import urllib |
3 import datetime |
3 import datetime |
4 |
|
5 from django.utils.functional import Promise |
4 from django.utils.functional import Promise |
6 from django.utils.safestring import SafeData, mark_safe |
|
7 |
5 |
8 class DjangoUnicodeDecodeError(UnicodeDecodeError): |
6 class DjangoUnicodeDecodeError(UnicodeDecodeError): |
9 def __init__(self, obj, *args): |
7 def __init__(self, obj, *args): |
10 self.obj = obj |
8 self.obj = obj |
11 UnicodeDecodeError.__init__(self, *args) |
9 UnicodeDecodeError.__init__(self, *args) |
48 try: |
46 try: |
49 if not isinstance(s, basestring,): |
47 if not isinstance(s, basestring,): |
50 if hasattr(s, '__unicode__'): |
48 if hasattr(s, '__unicode__'): |
51 s = unicode(s) |
49 s = unicode(s) |
52 else: |
50 else: |
53 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]) |
54 elif not isinstance(s, unicode): |
64 elif not isinstance(s, unicode): |
55 # Note: We use .decode() here, instead of unicode(s, encoding, |
65 # Note: We use .decode() here, instead of unicode(s, encoding, |
56 # errors), so that if s is a SafeString, it ends up being a |
66 # errors), so that if s is a SafeString, it ends up being a |
57 # SafeUnicode at the end. |
67 # SafeUnicode at the end. |
58 s = s.decode(encoding, errors) |
68 s = s.decode(encoding, errors) |
72 return unicode(s).encode(encoding, errors) |
82 return unicode(s).encode(encoding, errors) |
73 elif not isinstance(s, basestring): |
83 elif not isinstance(s, basestring): |
74 try: |
84 try: |
75 return str(s) |
85 return str(s) |
76 except UnicodeEncodeError: |
86 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]) |
77 return unicode(s).encode(encoding, errors) |
93 return unicode(s).encode(encoding, errors) |
78 elif isinstance(s, unicode): |
94 elif isinstance(s, unicode): |
79 return s.encode(encoding, errors) |
95 return s.encode(encoding, errors) |
80 elif s and encoding != 'utf-8': |
96 elif s and encoding != 'utf-8': |
81 return s.decode('utf-8', errors).encode(encoding, errors) |
97 return s.decode('utf-8', errors).encode(encoding, errors) |