Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,30 @@ string data in all Python versions.
:meth:`py3:str.encode`


.. function:: str_to_binary(s, encoding='utf-8', errors='strict')

Encodes s to from ``str`` to :data:`binary_type` in python 3 only. No-op in python 2.
*encoding*, *errors* are same as :meth:`py3.str.encode`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, I'd mention the type of the argument (it's possible to infer it from the rest but it took me a while). And do this for all functions.



.. function:: binary_to_str(s, encoding='utf-8', errors='strict')

Decodes s from :data:`binary_type` to ``str`` in python 3 only. No-op in python 2.
*encoding*, *errors* are same as :meth:`py3.str.encode`


.. function:: str_to_text(s, encoding='utf-8', errors='strict')

Decodes s from ``str`` to :data:`text_type` in python 2 only. No-op in python 3.
*encoding*, *errors* are same as :meth:`py3.str.encode`


.. function:: text_to_str(s, encoding='utf-8', errors='strict')

Encodes s from :data:`text_type` to ``str`` in python 2 only. No-op in python 3.
*encoding*, *errors* are same as :meth:`py3.str.encode`


.. data:: StringIO

This is a fake file object for textual data. It's an alias for
Expand Down
25 changes: 25 additions & 0 deletions six.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,31 @@ def ensure_text(s, encoding='utf-8', errors='strict'):
raise TypeError("not expecting type '%s'" % type(s))


if PY3:
def binary_to_str(s, encoding='utf-8', errors='strict'):
return s.decode(encoding, errors)

def str_to_binary(s, encoding='utf-8', errors='strict'):
return s.encode(encoding, errors)

def text_to_str(s, encoding='utf-8', errors='strict'):
return s

def str_to_text(s, encoding='utf-8', errors='strict'):
return s
else:
def binary_to_str(s, encoding='utf-8', errors='strict'):
return s

def str_to_binary(s, encoding='utf-8', errors='strict'):
return s

def text_to_str(s, encoding='utf-8', errors='strict'):
return s.encode(encoding, errors)

def str_to_text(s, encoding='utf-8', errors='strict'):
return s.decode(encoding, errors)


def python_2_unicode_compatible(klass):
"""
Expand Down
19 changes: 15 additions & 4 deletions test_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,12 +954,23 @@ def __bytes__(self):
assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello")


class EnsureTests:
# grinning face emoji
UNICODE_EMOJI = six.u("\U0001F600")
BINARY_EMOJI = b"\xf0\x9f\x98\x80"
if six.PY2:
STR_EMOJI = BINARY_EMOJI
else:
STR_EMOJI = UNICODE_EMOJI


# grinning face emoji
UNICODE_EMOJI = six.u("\U0001F600")
BINARY_EMOJI = b"\xf0\x9f\x98\x80"
def test_str_conversions():
assert six.binary_to_str(BINARY_EMOJI) == STR_EMOJI
assert six.str_to_binary(STR_EMOJI) == BINARY_EMOJI
assert six.text_to_str(UNICODE_EMOJI) == STR_EMOJI
assert six.str_to_text(STR_EMOJI) == UNICODE_EMOJI


class EnsureTests:
def test_ensure_binary_raise_type_error(self):
with py.test.raises(TypeError):
six.ensure_str(8)
Expand Down