From 554f4e7aa8a3ffe65cac3b6698906c06e7db4fe6 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 1 Jun 2007 07:30:40 +0000 Subject: unicode: Reverted [5388] and fixed the problem in a different way. Checked every occurrence of smart_unicode() and force_unicode() that was not previously a str() call, so hopefully the problems will not reoccur. Fixed #4447. Refs #4435, #4430. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5400 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/encoding.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'django/utils/encoding.py') diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 2fe6a2d970..7515d0c41b 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -12,22 +12,26 @@ class StrAndUnicode(object): def __str__(self): return self.__unicode__().encode('utf-8') -def smart_unicode(s, encoding='utf-8', errors='strict'): +def smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ Returns a unicode object representing 's'. Treats bytestrings using the 'encoding' codec. + + If strings_only is True, don't convert (some) non-string-like objects. """ if isinstance(s, Promise): # The input is the result of a gettext_lazy() call. return s - return force_unicode(s, encoding, errors) + return force_unicode(s, encoding, strings_only, errors) -def force_unicode(s, encoding='utf-8', errors='strict'): +def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ Similar to smart_unicode, except that lazy instances are resolved to strings, rather than kept as lazy objects. + + If strings_only is True, don't convert (some) non-string-like objects. """ - if s is None: + if strings_only and isinstance(s, (types.NoneType, int)): return s if not isinstance(s, basestring,): if hasattr(s, '__unicode__'): -- cgit v1.3