Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 3f1ce2e60280ef42c8864e65485e4572d7740e8d 1 parent 6f7fd89
Malcolm Tredinnick malcolmt authored
Showing with 21 additions and 7 deletions.
  1. +21 −7 django/utils/encoding.py
28 django/utils/encoding.py
View
@@ -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'):
Please sign in to comment.
Something went wrong with that request. Please try again.