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
|
2010-05-04 22:00:30 +08:00
|
|
|
from decimal import Decimal
|
2009-02-12 04:13:17 +08:00
|
|
|
|
2007-04-04 14:34:19 +08:00
|
|
|
from django.utils.functional import Promise
|
|
|
|
|
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.
|
|
|
|
"""
|
2010-09-27 23:25:08 +08:00
|
|
|
# Handle the common case first, saves 30-40% in performance when s
|
|
|
|
# is an instance of unicode. This function gets called often in that
|
|
|
|
# setting.
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
return s
|
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.
|
2011-10-12 01:36:05 +08:00
|
|
|
s = u' '.join([force_unicode(arg, encoding, strings_only,
|
2008-08-27 02:53:51 +08:00
|
|
|
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)
|
2012-04-29 00:09:37 +08:00
|
|
|
except UnicodeDecodeError as 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.
|
2011-10-12 01:36:05 +08:00
|
|
|
s = u' '.join([force_unicode(arg, encoding, strings_only,
|
2010-02-28 23:18:03 +08:00
|
|
|
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
|
2012-05-19 23:43:34 +08:00
|
|
|
return urllib.quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2011-02-04 22:43:10 +08:00
|
|
|
def filepath_to_uri(path):
|
|
|
|
"""Convert an file system path to a URI portion that is suitable for
|
|
|
|
inclusion in a URL.
|
|
|
|
|
|
|
|
We are assuming input is either UTF-8 or unicode already.
|
|
|
|
|
|
|
|
This method will encode certain chars that would normally be recognized as
|
|
|
|
special chars for URIs. Note that this method does not encode the '
|
|
|
|
character, as it is a valid character within URIs. See
|
|
|
|
encodeURIComponent() JavaScript function for more details.
|
|
|
|
|
|
|
|
Returns an ASCII string containing the encoded result.
|
|
|
|
"""
|
|
|
|
if path is None:
|
|
|
|
return path
|
|
|
|
# I know about `os.sep` and `os.altsep` but I want to leave
|
|
|
|
# some flexibility for hardcoding separators.
|
2012-05-19 23:43:34 +08:00
|
|
|
return urllib.quote(smart_str(path).replace("\\", "/"), safe=b"/~!*()'")
|
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'
|