This following is from the django source code (Django-1.41/django/utils/encoding.py);
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 = u' '.join([force_unicode(arg, encoding, strings_only,
errors) for arg in s])
My question is: In which case will s be an instance of Exception?
when s is an instance of Exception, and s have neither str or repr attribute. Than this situation happen. Is this right?
swill be an exception if someone called the force_unicode function with a subclass of Exception and the message includes unicode characters.If the code in the
tryblock fails then nothing will be assigned tos, so s will remain what the function was initially called with.Since
Exceptioninherits fromobject, andobjecthas had the__unicode__method since Python 2.5, it might be the case that this code existed for Python 2.4 and is now obsolete.UPDATE:
After opening a pull request, this code has now been removed from the Django source: https://github.com/django/django/commit/ce1eb320e59b577a600eb84d7f423a1897be3576