diff options
author | Victor Stinner <victor.stinner@enovance.com> | 2014-04-07 12:09:24 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@enovance.com> | 2014-04-07 12:09:24 +0200 |
commit | 43a5fa4760d2a44672da6b92b56a8e34fae67ecf (patch) | |
tree | ccd2a1546e76d9c4058a07b9b28bb202cbb3f589 | |
parent | 8a19cf155c535473720862883bd3bccd0bc934c2 (diff) | |
download | oslo-utils-43a5fa4760d2a44672da6b92b56a8e34fae67ecf.tar.gz |
Fix safe_encode(): return bytes on Python 3
safe_decode() decodes bytes to text. safe_encode() should be the reverse
operation: encode text to bytes. Currently, it returns text on Python 3
which is wrong in my opinion.
The patch fixes also test_encode() on Python 3.
Change-Id: If91a866d864a22d28a352152beff4c7406a27b7b
-rw-r--r-- | openstack/common/strutils.py | 14 | ||||
-rw-r--r-- | tests/unit/test_strutils.py | 23 |
2 files changed, 17 insertions, 20 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py index e7a0b17..29cd572 100644 --- a/openstack/common/strutils.py +++ b/openstack/common/strutils.py @@ -159,19 +159,13 @@ def safe_encode(text, incoming=None, sys.getdefaultencoding()) if isinstance(text, six.text_type): - if six.PY3: - return text.encode(encoding, errors).decode(incoming) - else: - return text.encode(encoding, errors) + return text.encode(encoding, errors) elif text and encoding != incoming: # Decode text before encoding it with `encoding` text = safe_decode(text, incoming, errors) - if six.PY3: - return text.encode(encoding, errors).decode(incoming) - else: - return text.encode(encoding, errors) - - return text + return text.encode(encoding, errors) + else: + return text def string_to_bytes(text, unit_system='IEC', return_int=False): diff --git a/tests/unit/test_strutils.py b/tests/unit/test_strutils.py index a4ad73a..6f3adb7 100644 --- a/tests/unit/test_strutils.py +++ b/tests/unit/test_strutils.py @@ -166,18 +166,21 @@ class StrUtilsTest(test.BaseTestCase): def test_safe_encode(self): safe_encode = strutils.safe_encode self.assertRaises(TypeError, safe_encode, True) - self.assertEqual("ni\xc3\xb1o", safe_encode(six.u('ni\xf1o'), - encoding="utf-8")) - self.assertEqual("dGVzdA==\n", safe_encode("test", - encoding='base64')) - self.assertEqual('ni\xf1o', safe_encode("ni\xc3\xb1o", - encoding="iso-8859-1", - incoming="utf-8")) + self.assertEqual(six.b("ni\xc3\xb1o"), safe_encode(six.u('ni\xf1o'), + encoding="utf-8")) + if six.PY2: + # In Python 3, str.encode() doesn't support anymore + # text => text encodings like base64 + self.assertEqual(six.b("dGVzdA==\n"), + safe_encode("test", encoding='base64')) + self.assertEqual(six.b('ni\xf1o'), safe_encode(six.b("ni\xc3\xb1o"), + encoding="iso-8859-1", + incoming="utf-8")) # Forcing incoming to ascii so it falls back to utf-8 - self.assertEqual('ni\xc3\xb1o', safe_encode('ni\xc3\xb1o', - incoming='ascii')) - self.assertEqual('foo', safe_encode(b'foo')) + self.assertEqual(six.b('ni\xc3\xb1o'), + safe_encode(six.b('ni\xc3\xb1o'), incoming='ascii')) + self.assertEqual(six.b('foo'), safe_encode(six.u('foo'))) def test_slugify(self): to_slug = strutils.to_slug |