summaryrefslogtreecommitdiff
path: root/django/utils/encoding.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-06-01 07:30:40 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-06-01 07:30:40 +0000
commit554f4e7aa8a3ffe65cac3b6698906c06e7db4fe6 (patch)
treec014732f8ef6dddcaa198e04d8701c9f4afababe /django/utils/encoding.py
parent7a30b4e03ae8848792bd114741bc2fb175d280a4 (diff)
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
Diffstat (limited to 'django/utils/encoding.py')
-rw-r--r--django/utils/encoding.py12
1 files changed, 8 insertions, 4 deletions
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__'):