summaryrefslogtreecommitdiff
path: root/django/utils/encoding.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-08-26 18:53:51 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-08-26 18:53:51 +0000
commitc6a2bd9b962af1cdf46f964589e6023046cfa8ec (patch)
treeb2923bc5accdfb1945ae096d16bb80af396ff4de /django/utils/encoding.py
parent5dd68fa74ae6f3f401649c73e0f450b35d291d9c (diff)
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. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8588 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/encoding.py')
-rw-r--r--django/utils/encoding.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index c54e67610f..dbd2c7a211 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -48,7 +48,19 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
if hasattr(s, '__unicode__'):
s = unicode(s)
else:
- s = unicode(str(s), encoding, errors)
+ try:
+ s = unicode(str(s), encoding, errors)
+ except UnicodeEncodeError:
+ if not isinstance(s, Exception):
+ raise
+ # If we get to here, the caller has passed in an Exception
+ # subclass populated with non-ASCII data without special
+ # handling to display as a string. We need to handle this
+ # without raising a further exception. We do an
+ # approximation to what the Exception's standard str()
+ # output should be.
+ s = ' '.join([force_unicode(arg, encoding, strings_only,
+ errors) for arg in s])
elif not isinstance(s, unicode):
# Note: We use .decode() here, instead of unicode(s, encoding,
# errors), so that if s is a SafeString, it ends up being a
@@ -72,6 +84,12 @@ def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
try:
return str(s)
except UnicodeEncodeError:
+ if isinstance(s, Exception):
+ # An Exception subclass containing non-ASCII data that doesn't
+ # know how to print itself properly. We shouldn't raise a
+ # further exception.
+ return ' '.join([smart_str(arg, encoding, strings_only,
+ errors) for arg in s])
return unicode(s).encode(encoding, errors)
elif isinstance(s, unicode):
return s.encode(encoding, errors)