Author: mtredinnick
Date: 2007-11-03 22:37:04 -0500 (Sat, 03 Nov 2007)
New Revision: 6649

Modified:
   django/trunk/django/utils/encoding.py
Log:
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.


Modified: django/trunk/django/utils/encoding.py
===================================================================
--- django/trunk/django/utils/encoding.py       2007-11-04 02:48:44 UTC (rev 
6648)
+++ django/trunk/django/utils/encoding.py       2007-11-04 03:37:04 UTC (rev 
6649)
@@ -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 @@
     """
     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'):


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to