Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
import types
|
|
|
|
import urllib
|
2009-05-08 17:51:05 +08:00
|
|
|
import locale
|
2007-09-22 21:21:54 +08:00
|
|
|
import datetime
|
2009-05-08 17:51:05 +08:00
|
|
|
import codecs
|
2009-02-12 04:13:17 +08:00
|
|
|
|
2007-04-04 14:34:19 +08:00
|
|
|
from django.utils.functional import Promise
|
|
|
|
|
2009-02-12 04:13:17 +08:00
|
|
|
try:
|
|
|
|
from decimal import Decimal
|
|
|
|
except ImportError:
|
|
|
|
from django.utils._decimal import Decimal # Python 2.3 fallback
|
|
|
|
|
|
|
|
|
2007-11-04 11:37:04 +08:00
|
|
|
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))
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
class StrAndUnicode(object):
|
|
|
|
"""
|
|
|
|
A class whose __str__ returns its __unicode__ as a UTF-8 bytestring.
|
|
|
|
|
|
|
|
Useful as a mix-in.
|
|
|
|
"""
|
|
|
|
def __str__(self):
|
|
|
|
return self.__unicode__().encode('utf-8')
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2007-04-04 14:34:19 +08:00
|
|
|
if isinstance(s, Promise):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
# The input is the result of a gettext_lazy() call.
|
|
|
|
return s
|
|
|
|
return force_unicode(s, encoding, strings_only, errors)
|
|
|
|
|
2009-04-13 20:35:49 +08:00
|
|
|
def is_protected_type(obj):
|
|
|
|
"""Determine if the object instance is of a protected type.
|
|
|
|
|
|
|
|
Objects of protected types are preserved as-is when passed to
|
|
|
|
force_unicode(strings_only=True).
|
|
|
|
"""
|
|
|
|
return isinstance(obj, (
|
|
|
|
types.NoneType,
|
|
|
|
int, long,
|
|
|
|
datetime.datetime, datetime.date, datetime.time,
|
|
|
|
float, Decimal)
|
|
|
|
)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
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.
|
|
|
|
"""
|
2009-04-13 20:35:49 +08:00
|
|
|
if strings_only and is_protected_type(s):
|
2007-04-04 14:34:19 +08:00
|
|
|
return s
|
2007-11-04 11:37:04 +08:00
|
|
|
try:
|
|
|
|
if not isinstance(s, basestring,):
|
|
|
|
if hasattr(s, '__unicode__'):
|
|
|
|
s = unicode(s)
|
|
|
|
else:
|
2008-08-27 02:53:51 +08:00
|
|
|
try:
|
|
|
|
s = unicode(str(s), encoding, errors)
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
if not isinstance(s, Exception):
|
|
|
|
raise
|
|
|
|
# If we get to here, the caller has passed in an Exception
|
|
|
|
# subclass populated with non-ASCII data without special
|
|
|
|
# handling to display as a string. We need to handle this
|
|
|
|
# without raising a further exception. We do an
|
|
|
|
# approximation to what the Exception's standard str()
|
|
|
|
# output should be.
|
|
|
|
s = ' '.join([force_unicode(arg, encoding, strings_only,
|
|
|
|
errors) for arg in s])
|
2007-11-04 11:37:04 +08:00
|
|
|
elif not isinstance(s, unicode):
|
2007-11-14 20:58:53 +08:00
|
|
|
# Note: We use .decode() here, instead of unicode(s, encoding,
|
|
|
|
# errors), so that if s is a SafeString, it ends up being a
|
|
|
|
# SafeUnicode at the end.
|
|
|
|
s = s.decode(encoding, errors)
|
2007-11-04 11:37:04 +08:00
|
|
|
except UnicodeDecodeError, e:
|
2010-02-28 23:18:03 +08:00
|
|
|
if not isinstance(s, Exception):
|
|
|
|
raise DjangoUnicodeDecodeError(s, *e.args)
|
|
|
|
else:
|
|
|
|
# If we get to here, the caller has passed in an Exception
|
|
|
|
# subclass populated with non-ASCII bytestring data without a
|
|
|
|
# working unicode method. Try to handle this without raising a
|
|
|
|
# further exception by individually forcing the exception args
|
|
|
|
# to unicode.
|
|
|
|
s = ' '.join([force_unicode(arg, encoding, strings_only,
|
|
|
|
errors) for arg in s])
|
2007-04-04 14:34:19 +08:00
|
|
|
return s
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
|
2007-04-04 14:34:19 +08:00
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Returns a bytestring version of 's', encoded as specified in 'encoding'.
|
2007-04-04 14:34:19 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
If strings_only is True, don't convert (some) non-string-like objects.
|
2007-04-04 14:34:19 +08:00
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if strings_only and isinstance(s, (types.NoneType, int)):
|
|
|
|
return s
|
|
|
|
if isinstance(s, Promise):
|
|
|
|
return unicode(s).encode(encoding, errors)
|
|
|
|
elif not isinstance(s, basestring):
|
|
|
|
try:
|
|
|
|
return str(s)
|
|
|
|
except UnicodeEncodeError:
|
2008-08-27 02:53:51 +08:00
|
|
|
if isinstance(s, Exception):
|
|
|
|
# An Exception subclass containing non-ASCII data that doesn't
|
|
|
|
# know how to print itself properly. We shouldn't raise a
|
|
|
|
# further exception.
|
|
|
|
return ' '.join([smart_str(arg, encoding, strings_only,
|
|
|
|
errors) for arg in s])
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return unicode(s).encode(encoding, errors)
|
|
|
|
elif isinstance(s, unicode):
|
|
|
|
return s.encode(encoding, errors)
|
|
|
|
elif s and encoding != 'utf-8':
|
|
|
|
return s.decode('utf-8', errors).encode(encoding, errors)
|
|
|
|
else:
|
|
|
|
return s
|
|
|
|
|
|
|
|
def iri_to_uri(iri):
|
|
|
|
"""
|
|
|
|
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
|
|
|
portion that is suitable for inclusion in a URL.
|
|
|
|
|
|
|
|
This is the algorithm from section 3.1 of RFC 3987. However, since we are
|
|
|
|
assuming input is either UTF-8 or unicode already, we can simplify things a
|
|
|
|
little from the full method.
|
|
|
|
|
|
|
|
Returns an ASCII string containing the encoded result.
|
|
|
|
"""
|
2010-01-04 02:06:27 +08:00
|
|
|
# The list of safe characters here is constructed from the "reserved" and
|
|
|
|
# "unreserved" characters specified in sections 2.2 and 2.3 of RFC 3986:
|
|
|
|
# reserved = gen-delims / sub-delims
|
|
|
|
# gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
|
|
|
|
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
|
|
|
# / "*" / "+" / "," / ";" / "="
|
|
|
|
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
|
|
|
# Of the unreserved characters, urllib.quote already considers all but
|
|
|
|
# the ~ safe.
|
|
|
|
# The % character is also added to the list of safe characters here, as the
|
|
|
|
# end of section 3.1 of RFC 3987 specifically mentions that % must not be
|
|
|
|
# converted.
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if iri is None:
|
|
|
|
return iri
|
2010-01-04 02:06:27 +08:00
|
|
|
return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~")
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2009-05-08 17:51:05 +08:00
|
|
|
|
|
|
|
# The encoding of the default system locale but falls back to the
|
|
|
|
# given fallback encoding if the encoding is unsupported by python or could
|
|
|
|
# not be determined. See tickets #10335 and #5846
|
|
|
|
try:
|
|
|
|
DEFAULT_LOCALE_ENCODING = locale.getdefaultlocale()[1] or 'ascii'
|
|
|
|
codecs.lookup(DEFAULT_LOCALE_ENCODING)
|
|
|
|
except:
|
|
|
|
DEFAULT_LOCALE_ENCODING = 'ascii'
|