mirror of
https://github.com/django/django.git
synced 2025-03-31 19:46:42 +00:00
Introduced force_bytes and force_str.
This is consistent with the smart_* series of functions and it's going to be used by the next commit.
This commit is contained in:
parent
f04bb6d798
commit
a120fac65a
@ -139,6 +139,19 @@ def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
|
|||||||
"""
|
"""
|
||||||
Returns a bytestring version of 's', encoded as specified in 'encoding'.
|
Returns a bytestring version of 's', encoded as specified in 'encoding'.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
If strings_only is True, don't convert (some) non-string-like objects.
|
If strings_only is True, don't convert (some) non-string-like objects.
|
||||||
"""
|
"""
|
||||||
if isinstance(s, bytes):
|
if isinstance(s, bytes):
|
||||||
@ -169,8 +182,10 @@ def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
|
|||||||
|
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
smart_str = smart_text
|
smart_str = smart_text
|
||||||
|
force_str = force_text
|
||||||
else:
|
else:
|
||||||
smart_str = smart_bytes
|
smart_str = smart_bytes
|
||||||
|
force_str = force_bytes
|
||||||
# backwards compatibility for Python 2
|
# backwards compatibility for Python 2
|
||||||
smart_unicode = smart_text
|
smart_unicode = smart_text
|
||||||
force_unicode = force_text
|
force_unicode = force_text
|
||||||
@ -181,6 +196,10 @@ Apply smart_text in Python 3 and smart_bytes in Python 2.
|
|||||||
This is suitable for writing to sys.stdout (for instance).
|
This is suitable for writing to sys.stdout (for instance).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
force_str.__doc__ = """\
|
||||||
|
Apply force_text in Python 3 and force_bytes in Python 2.
|
||||||
|
"""
|
||||||
|
|
||||||
def iri_to_uri(iri):
|
def iri_to_uri(iri):
|
||||||
"""
|
"""
|
||||||
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
||||||
|
@ -240,14 +240,29 @@ The functions defined in this module share the following properties:
|
|||||||
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
||||||
objects.
|
objects.
|
||||||
|
|
||||||
|
.. function:: force_bytes(s, encoding='utf-8', strings_only=False, errors='strict')
|
||||||
|
|
||||||
|
.. versionadded:: 1.5
|
||||||
|
|
||||||
|
Similar to ``smart_bytes``, except that lazy instances are resolved to
|
||||||
|
bytestrings, rather than kept as lazy objects.
|
||||||
|
|
||||||
|
If ``strings_only`` is ``True``, don't convert (some) non-string-like
|
||||||
|
objects.
|
||||||
|
|
||||||
.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
||||||
|
|
||||||
Alias of :func:`smart_bytes` on Python 2 and :func:`smart_text` on Python
|
Alias of :func:`smart_bytes` on Python 2 and :func:`smart_text` on Python
|
||||||
3. This function always returns a :class:`str`.
|
3. This function returns a :class:`str` or a lazy string.
|
||||||
|
|
||||||
For instance, this is suitable for writing to :attr:`sys.stdout` on
|
For instance, this is suitable for writing to :attr:`sys.stdout` on
|
||||||
Python 2 and 3.
|
Python 2 and 3.
|
||||||
|
|
||||||
|
.. function:: force_str(s, encoding='utf-8', strings_only=False, errors='strict')
|
||||||
|
|
||||||
|
Alias of :func:`force_bytes` on Python 2 and :func:`force_text` on Python
|
||||||
|
3. This function always returns a :class:`str`.
|
||||||
|
|
||||||
.. function:: iri_to_uri(iri)
|
.. function:: iri_to_uri(iri)
|
||||||
|
|
||||||
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
Convert an Internationalized Resource Identifier (IRI) portion to a URI
|
||||||
|
Loading…
x
Reference in New Issue
Block a user