2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2009-05-08 17:51:05 +08:00
|
|
|
import codecs
|
2012-07-20 21:36:52 +08:00
|
|
|
import datetime
|
2010-05-04 22:00:30 +08:00
|
|
|
from decimal import Decimal
|
2012-07-20 21:36:52 +08:00
|
|
|
import locale
|
2012-08-12 20:39:48 +08:00
|
|
|
import warnings
|
2009-02-12 04:13:17 +08:00
|
|
|
|
2007-04-04 14:34:19 +08:00
|
|
|
from django.utils.functional import Promise
|
2012-07-20 18:45:19 +08:00
|
|
|
from django.utils import six
|
2013-09-06 03:38:59 +08:00
|
|
|
from django.utils.six.moves.urllib.parse import quote
|
2007-04-04 14:34:19 +08:00
|
|
|
|
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):
|
|
|
|
"""
|
2012-07-21 16:00:10 +08:00
|
|
|
A class that derives __str__ from __unicode__.
|
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
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
On Python 2, __str__ returns the output of __unicode__ encoded as a UTF-8
|
|
|
|
bytestring. On Python 3, __str__ returns the output of __unicode__.
|
|
|
|
|
|
|
|
Useful as a mix-in. If you support Python 2 and 3 with a single code base,
|
|
|
|
you can inherit this mix-in and just define __unicode__.
|
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
|
|
|
"""
|
2012-08-12 20:39:48 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn("StrAndUnicode is deprecated. Define a __str__ method "
|
|
|
|
"and apply the @python_2_unicode_compatible decorator "
|
2012-12-26 04:07:27 +08:00
|
|
|
"instead.", DeprecationWarning, stacklevel=2)
|
2012-08-12 20:39:48 +08:00
|
|
|
super(StrAndUnicode, self).__init__(*args, **kwargs)
|
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
if six.PY3:
|
|
|
|
def __str__(self):
|
|
|
|
return self.__unicode__()
|
|
|
|
else:
|
|
|
|
def __str__(self):
|
|
|
|
return self.__unicode__().encode('utf-8')
|
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
|
|
|
|
2012-08-11 21:34:51 +08:00
|
|
|
def python_2_unicode_compatible(klass):
|
|
|
|
"""
|
|
|
|
A decorator that defines __unicode__ and __str__ methods under Python 2.
|
|
|
|
Under Python 3 it does nothing.
|
|
|
|
|
|
|
|
To support Python 2 and 3 with a single code base, define a __str__ method
|
|
|
|
returning text and apply this decorator to the class.
|
|
|
|
"""
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2:
|
2013-10-14 00:06:58 +08:00
|
|
|
if '__str__' not in klass.__dict__:
|
|
|
|
raise ValueError("@python_2_unicode_compatible cannot be applied "
|
|
|
|
"to %s because it doesn't define __str__()." %
|
|
|
|
klass.__name__)
|
2012-08-11 21:34:51 +08:00
|
|
|
klass.__unicode__ = klass.__str__
|
|
|
|
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
|
|
|
|
return klass
|
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
def smart_text(s, encoding='utf-8', strings_only=False, errors='strict'):
|
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
|
|
|
"""
|
2012-07-21 16:00:10 +08:00
|
|
|
Returns a text object representing 's' -- unicode on Python 2 and str on
|
|
|
|
Python 3. Treats bytestrings using the 'encoding' codec.
|
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
|
|
|
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
|
2012-07-21 16:00:10 +08:00
|
|
|
return force_text(s, encoding, strings_only, errors)
|
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
|
|
|
|
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
|
2012-07-21 16:00:10 +08:00
|
|
|
force_text(strings_only=True).
|
2009-04-13 20:35:49 +08:00
|
|
|
"""
|
2012-07-20 18:45:19 +08:00
|
|
|
return isinstance(obj, six.integer_types + (type(None), float, Decimal,
|
|
|
|
datetime.datetime, datetime.date, datetime.time))
|
2009-04-13 20:35:49 +08:00
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
|
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
|
|
|
"""
|
2012-07-21 16:00:10 +08:00
|
|
|
Similar to smart_text, except that lazy instances are resolved to
|
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
|
|
|
strings, rather than kept as lazy objects.
|
|
|
|
|
|
|
|
If strings_only is True, don't convert (some) non-string-like objects.
|
|
|
|
"""
|
2012-07-21 16:00:10 +08:00
|
|
|
# Handle the common case first, saves 30-40% when s is an instance of
|
|
|
|
# six.text_type. This function gets called often in that setting.
|
2012-07-20 20:48:51 +08:00
|
|
|
if isinstance(s, six.text_type):
|
2010-09-27 23:25:08 +08:00
|
|
|
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:
|
2012-07-20 20:48:51 +08:00
|
|
|
if not isinstance(s, six.string_types):
|
2007-11-04 11:37:04 +08:00
|
|
|
if hasattr(s, '__unicode__'):
|
2012-07-20 20:48:51 +08:00
|
|
|
s = s.__unicode__()
|
2007-11-04 11:37:04 +08:00
|
|
|
else:
|
2012-10-25 06:53:00 +08:00
|
|
|
if six.PY3:
|
|
|
|
if isinstance(s, bytes):
|
|
|
|
s = six.text_type(s, encoding, errors)
|
2012-07-20 20:48:51 +08:00
|
|
|
else:
|
2012-10-25 06:53:00 +08:00
|
|
|
s = six.text_type(s)
|
|
|
|
else:
|
|
|
|
s = six.text_type(bytes(s), encoding, errors)
|
2012-07-24 06:28:29 +08:00
|
|
|
else:
|
2012-07-20 20:48:51 +08:00
|
|
|
# Note: We use .decode() here, instead of six.text_type(s, encoding,
|
2012-08-18 22:04:06 +08:00
|
|
|
# errors), so that if s is a SafeBytes, it ends up being a
|
|
|
|
# SafeText at the end.
|
2007-11-14 20:58:53 +08:00
|
|
|
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.
|
2012-07-21 16:00:10 +08:00
|
|
|
s = ' '.join([force_text(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
|
|
|
|
|
2012-07-21 16:00:10 +08:00
|
|
|
def smart_bytes(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
|
|
|
|
2012-08-18 22:36:55 +08:00
|
|
|
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_bytes(s, encoding, strings_only, errors)
|
|
|
|
|
|
|
|
|
|
|
|
def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
|
|
|
|
"""
|
|
|
|
Similar to smart_bytes, except that lazy instances are resolved to
|
|
|
|
strings, rather than kept as lazy objects.
|
|
|
|
|
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
|
|
|
"""
|
2012-12-15 07:26:08 +08:00
|
|
|
if isinstance(s, six.memoryview):
|
|
|
|
s = bytes(s)
|
2012-07-21 16:00:10 +08:00
|
|
|
if isinstance(s, bytes):
|
|
|
|
if encoding == 'utf-8':
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return s.decode('utf-8', errors).encode(encoding, errors)
|
2012-05-23 18:20:03 +08:00
|
|
|
if strings_only and (s is None or isinstance(s, int)):
|
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 s
|
|
|
|
if isinstance(s, Promise):
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(s).encode(encoding, errors)
|
2012-07-21 16:00:10 +08:00
|
|
|
if not isinstance(s, six.string_types):
|
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
|
|
|
try:
|
2012-07-20 20:48:51 +08:00
|
|
|
if six.PY3:
|
|
|
|
return six.text_type(s).encode(encoding)
|
|
|
|
else:
|
|
|
|
return bytes(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
|
|
|
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.
|
2012-09-04 15:24:39 +08:00
|
|
|
return b' '.join([force_bytes(arg, encoding, strings_only,
|
2008-08-27 02:53:51 +08:00
|
|
|
errors) for arg in s])
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(s).encode(encoding, errors)
|
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
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
return s.encode(encoding, errors)
|
|
|
|
|
|
|
|
if six.PY3:
|
|
|
|
smart_str = smart_text
|
2012-08-18 22:36:55 +08:00
|
|
|
force_str = force_text
|
2012-07-21 16:00:10 +08:00
|
|
|
else:
|
|
|
|
smart_str = smart_bytes
|
2012-08-18 22:36:55 +08:00
|
|
|
force_str = force_bytes
|
2012-07-21 16:00:10 +08:00
|
|
|
# backwards compatibility for Python 2
|
|
|
|
smart_unicode = smart_text
|
|
|
|
force_unicode = force_text
|
|
|
|
|
|
|
|
smart_str.__doc__ = """\
|
|
|
|
Apply smart_text in Python 3 and smart_bytes in Python 2.
|
|
|
|
|
|
|
|
This is suitable for writing to sys.stdout (for instance).
|
|
|
|
"""
|
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
|
|
|
|
2012-08-18 22:36:55 +08:00
|
|
|
force_str.__doc__ = """\
|
|
|
|
Apply force_text in Python 3 and force_bytes in Python 2.
|
|
|
|
"""
|
|
|
|
|
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 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-08-29 02:59:56 +08:00
|
|
|
return quote(force_bytes(iri), safe=b"/#%[]=:;$&()+,!?*@'~")
|
2007-04-04 14:34:19 +08:00
|
|
|
|
2011-02-04 22:43:10 +08:00
|
|
|
def filepath_to_uri(path):
|
2012-12-17 03:34:41 +08:00
|
|
|
"""Convert a file system path to a URI portion that is suitable for
|
2011-02-04 22:43:10 +08:00
|
|
|
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.
|
2013-03-23 00:55:12 +08:00
|
|
|
return quote(force_bytes(path).replace(b"\\", b"/"), safe=b"/~!*()'")
|
2009-05-08 17:51:05 +08:00
|
|
|
|
2013-03-03 05:41:08 +08:00
|
|
|
def get_system_encoding():
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
encoding = locale.getdefaultlocale()[1] or 'ascii'
|
|
|
|
codecs.lookup(encoding)
|
|
|
|
except Exception:
|
|
|
|
encoding = 'ascii'
|
|
|
|
return encoding
|
|
|
|
|
|
|
|
DEFAULT_LOCALE_ENCODING = get_system_encoding()
|