[py3] Ported django.utils.encoding.

* Renamed smart_unicode to smart_text (but kept the old name under
  Python 2 for backwards compatibility).
* Renamed smart_str to smart_bytes.
* Re-introduced smart_str as an alias for smart_text under Python 3
  and smart_bytes under Python 2 (which is backwards compatible).
  Thus smart_str always returns a str objects.
* Used the new smart_str in a few places where both Python 2 and 3
  want a str.
This commit is contained in:
Aymeric Augustin 2012-07-21 10:00:10 +02:00
parent ee191715ea
commit c5ef65bcf3
125 changed files with 629 additions and 583 deletions

View file

@ -178,33 +178,53 @@ The functions defined in this module share the following properties:
.. class:: StrAndUnicode
A class whose ``__str__`` returns its ``__unicode__`` as a UTF-8
bytestring. Useful as a mix-in.
A class that derives ``__str__`` from ``__unicode__``.
.. function:: smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
On Python 2, ``__str__`` returns the output of ``__unicode__`` encoded as
a UTF-8 bytestring. On Python 3, ``__str__`` returns the output of
``__unicode__``.
Returns a ``unicode`` object representing ``s``. Treats bytestrings using
the 'encoding' codec.
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__``.
.. function:: smart_text(s, encoding='utf-8', strings_only=False, errors='strict')
.. versionadded:: 1.5
Returns a text object representing ``s`` -- ``unicode`` on Python 2 and
``str`` on Python 3. Treats bytestrings using the ``encoding`` codec.
If ``strings_only`` is ``True``, don't convert (some) non-string-like
objects.
.. function:: smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
Historical name of :func:`smart_text`. Only available under Python 2.
.. function:: 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)``.
``force_text(strings_only=True)``.
.. function:: force_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
.. function:: force_text(s, encoding='utf-8', strings_only=False, errors='strict')
Similar to ``smart_unicode``, except that lazy instances are resolved to
.. versionadded:: 1.5
Similar to ``smart_text``, 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.
.. function:: smart_str(s, encoding='utf-8', strings_only=False, errors='strict')
.. function:: force_unicode(s, encoding='utf-8', strings_only=False, errors='strict')
Historical name of :func:`force_text`. Only available under Python 2.
.. function:: smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')
.. versionadded:: 1.5
Returns a bytestring version of ``s``, encoded as specified in
``encoding``.
@ -212,6 +232,14 @@ The functions defined in this module share the following properties:
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')
Alias of :func:`smart_bytes` on Python 2 and :func:`smart_text` on Python
3. This function always returns a :class:`str`.
For instance, this is suitable for writing to :attr:`sys.stdout` on
Python 2 and 3.
.. function:: iri_to_uri(iri)
Convert an Internationalized Resource Identifier (IRI) portion to a URI
@ -406,7 +434,7 @@ escaping HTML.
Returns the given text with ampersands, quotes and angle brackets encoded
for use in HTML. The input is first passed through
:func:`~django.utils.encoding.force_unicode` and the output has
:func:`~django.utils.encoding.force_text` and the output has
:func:`~django.utils.safestring.mark_safe` applied.
.. function:: conditional_escape(text)
@ -448,7 +476,7 @@ escaping HTML.
interpolation, some of the formatting options provided by `str.format`_
(e.g. number formatting) will not work, since all arguments are passed
through :func:`conditional_escape` which (ultimately) calls
:func:`~django.utils.encoding.force_unicode` on the values.
:func:`~django.utils.encoding.force_text` on the values.
.. _str.format: http://docs.python.org/library/stdtypes.html#str.format