From 3f1ce2e60280ef42c8864e65485e4572d7740e8d Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 4 Nov 2007 03:37:04 +0000 Subject: [PATCH] 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. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6649 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/encoding.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 2bd1ef6563..4bda9caa50 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -1,8 +1,19 @@ import types import urllib import datetime + from django.utils.functional import Promise +class DjangoUnicodeDecodeError(UnicodeDecodeError): + def __init__(self, obj, *args): + self.obj = obj + UnicodeDecodeError.__init__(self, *args) + + def __str__(self): + original = UnicodeDecodeError.__str__(self) + return '%s. You passed in %r (%s)' % (original, self.obj, + type(self.obj)) + class StrAndUnicode(object): """ A class whose __str__ returns its __unicode__ as a UTF-8 bytestring. @@ -33,13 +44,16 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.date, datetime.time, float)): return s - if not isinstance(s, basestring,): - if hasattr(s, '__unicode__'): - s = unicode(s) - else: - s = unicode(str(s), encoding, errors) - elif not isinstance(s, unicode): - s = unicode(s, encoding, errors) + try: + if not isinstance(s, basestring,): + if hasattr(s, '__unicode__'): + s = unicode(s) + else: + s = unicode(str(s), encoding, errors) + elif not isinstance(s, unicode): + s = unicode(s, encoding, errors) + except UnicodeDecodeError, e: + raise DjangoUnicodeDecodeError(s, *e.args) return s def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):