diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-08-18 16:36:55 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-08-18 16:38:49 +0200 |
| commit | a120fac65a17137bc8ac710477478474e3f9973e (patch) | |
| tree | 338c9ad149e43accbf8432f12eda66aa2ce73fba /django | |
| parent | f04bb6d798b07aa5e7c1d99d700fa6ddc7d39e62 (diff) | |
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.
Diffstat (limited to 'django')
| -rw-r--r-- | django/utils/encoding.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 3ac2c508b6..7027b82a61 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -141,6 +141,19 @@ def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): 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 isinstance(s, bytes): if encoding == 'utf-8': return s @@ -169,8 +182,10 @@ def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): if six.PY3: smart_str = smart_text + force_str = force_text else: smart_str = smart_bytes + force_str = force_bytes # backwards compatibility for Python 2 smart_unicode = smart_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). """ +force_str.__doc__ = """\ +Apply force_text in Python 3 and force_bytes in Python 2. +""" + def iri_to_uri(iri): """ Convert an Internationalized Resource Identifier (IRI) portion to a URI |
