From d34b27e039f2bf0f7a4258fb66eca71f4e913af6 Mon Sep 17 00:00:00 2001 From: Jared Hance Date: Thu, 6 Jun 2019 09:25:33 -0700 Subject: [PATCH 1/4] Add various str conversion functions --- six.py | 25 +++++++++++++++++++++++++ test_six.py | 19 +++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/six.py b/six.py index 901ec3985..54afcf0f3 100644 --- a/six.py +++ b/six.py @@ -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('utf-8') + + def str_to_text(s, encoding='utf-8', errors='strict'): + return s.decode('utf-8') + def python_2_unicode_compatible(klass): """ diff --git a/test_six.py b/test_six.py index 897e2322f..a72b75003 100644 --- a/test_six.py +++ b/test_six.py @@ -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) From ea0e8b8687f7879467eafdc16b5fd2c6820ce689 Mon Sep 17 00:00:00 2001 From: Jared Hance Date: Thu, 6 Jun 2019 09:29:14 -0700 Subject: [PATCH 2/4] Fix docs --- documentation/index.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/documentation/index.rst b/documentation/index.rst index 99192a21e..9ab4d6e74 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -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 :data:`binary_type` in python 3 only. No-op in python 2. + *encoding*, *errors* are same as :meth:`py3.str.encode` + + +.. function:: binary_to_str(s, encoding='utf-8', errors='strict') + + Decodes s 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 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 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 From 155c15edf7e8c11ea02774dc99300e2414623993 Mon Sep 17 00:00:00 2001 From: Jared Hance Date: Thu, 6 Jun 2019 09:36:50 -0700 Subject: [PATCH 3/4] Be less auto-pilot --- six.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/six.py b/six.py index 54afcf0f3..b8e1745e8 100644 --- a/six.py +++ b/six.py @@ -928,10 +928,10 @@ 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('utf-8') + return s.encode(encoding, errors) def str_to_text(s, encoding='utf-8', errors='strict'): - return s.decode('utf-8') + return s.decode(encoding, errors) def python_2_unicode_compatible(klass): From b92247312d5acfc1d341f205f03c32fdf578fb49 Mon Sep 17 00:00:00 2001 From: Jared Hance Date: Thu, 6 Jun 2019 10:48:03 -0700 Subject: [PATCH 4/4] complete the docs --- documentation/index.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/documentation/index.rst b/documentation/index.rst index 9ab4d6e74..3db2397cc 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -454,25 +454,25 @@ string data in all Python versions. .. function:: str_to_binary(s, encoding='utf-8', errors='strict') - Encodes s to :data:`binary_type` in python 3 only. No-op in python 2. + 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` .. function:: binary_to_str(s, encoding='utf-8', errors='strict') - Decodes s to ``str`` in python 3 only. No-op in python 2. + 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 to :data:`text_type` in python 2 only. No-op in python 3. + 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 to ``str`` in python 2 only. No-op in python 3. + 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`