summaryrefslogtreecommitdiff
path: root/django/utils/encoding.py
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2007-04-06 04:39:24 +0000
committerAdrian Holovaty <adrian@holovaty.com>2007-04-06 04:39:24 +0000
commit94aeccf18eca83ce8c983695059d56f66127a0fd (patch)
tree42aae866c510e6cabc658acb445f1e41f9dbe82c /django/utils/encoding.py
parente2b49c379a56730b73d79ec05d6edc790ba08cc9 (diff)
newforms-admin: Merged to [4939]
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4940 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/encoding.py')
-rw-r--r--django/utils/encoding.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
new file mode 100644
index 0000000000..4774fb0d26
--- /dev/null
+++ b/django/utils/encoding.py
@@ -0,0 +1,32 @@
+from django.conf import settings
+from django.utils.functional import Promise
+
+def smart_unicode(s):
+ if isinstance(s, Promise):
+ # The input is the result of a gettext_lazy() call, or similar. It will
+ # already be encoded in DEFAULT_CHARSET on evaluation and we don't want
+ # to evaluate it until render time.
+ # FIXME: This isn't totally consistent, because it eventually returns a
+ # bytestring rather than a unicode object. It works wherever we use
+ # smart_unicode() at the moment. Fixing this requires work in the
+ # i18n internals.
+ return s
+ if not isinstance(s, basestring,):
+ if hasattr(s, '__unicode__'):
+ s = unicode(s)
+ else:
+ s = unicode(str(s), settings.DEFAULT_CHARSET)
+ elif not isinstance(s, unicode):
+ s = unicode(s, settings.DEFAULT_CHARSET)
+ return s
+
+class StrAndUnicode(object):
+ """
+ A class whose __str__ returns its __unicode__ as a bytestring
+ according to settings.DEFAULT_CHARSET.
+
+ Useful as a mix-in.
+ """
+ def __str__(self):
+ return self.__unicode__().encode(settings.DEFAULT_CHARSET)
+