summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCyril Roelandt <cyril.roelandt@enovance.com>2014-03-04 22:24:40 +0100
committerCyril Roelandt <cyril.roelandt@enovance.com>2014-03-04 22:24:40 +0100
commit5db1a8a6538511c89e87c6c2820688a86b98a0db (patch)
tree65182ca7ce0c1d86c5dbe039fb81cf09ba7613ff
parent560eff685bba9d3abec6f4d99a60bce71c33b2c1 (diff)
downloadoslo-utils-5db1a8a6538511c89e87c6c2820688a86b98a0db.tar.gz
strutils: Allow safe_{encode,decode} to take bytes as input
In Python 3, passing "b'foo'" to safe_{encode,decode} currently fails. This patch fixes this issue. Change-Id: I01a5312700be80f353725fdf13de6413c4eb86f3
-rw-r--r--openstack/common/strutils.py9
-rw-r--r--tests/unit/test_strutils.py3
2 files changed, 8 insertions, 4 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py
index 2369a5f..e7a0b17 100644
--- a/openstack/common/strutils.py
+++ b/openstack/common/strutils.py
@@ -98,7 +98,8 @@ def bool_from_string(subject, strict=False, default=False):
def safe_decode(text, incoming=None, errors='strict'):
- """Decodes incoming str using `incoming` if they're not already unicode.
+ """Decodes incoming text/bytes string using `incoming` if they're not
+ already unicode.
:param incoming: Text's current encoding
:param errors: Errors handling policy. See here for valid
@@ -107,7 +108,7 @@ def safe_decode(text, incoming=None, errors='strict'):
representation of it.
:raises TypeError: If text is not an instance of str
"""
- if not isinstance(text, six.string_types):
+ if not isinstance(text, (six.string_types, six.binary_type)):
raise TypeError("%s can't be decoded" % type(text))
if isinstance(text, six.text_type):
@@ -137,7 +138,7 @@ def safe_decode(text, incoming=None, errors='strict'):
def safe_encode(text, incoming=None,
encoding='utf-8', errors='strict'):
- """Encodes incoming str/unicode using `encoding`.
+ """Encodes incoming text/bytes string using `encoding`.
If incoming is not specified, text is expected to be encoded with
current python's default encoding. (`sys.getdefaultencoding`)
@@ -150,7 +151,7 @@ def safe_encode(text, incoming=None,
representation of it.
:raises TypeError: If text is not an instance of str
"""
- if not isinstance(text, six.string_types):
+ if not isinstance(text, (six.string_types, six.binary_type)):
raise TypeError("%s can't be encoded" % type(text))
if not incoming:
diff --git a/tests/unit/test_strutils.py b/tests/unit/test_strutils.py
index 9776e41..a4ad73a 100644
--- a/tests/unit/test_strutils.py
+++ b/tests/unit/test_strutils.py
@@ -161,6 +161,8 @@ class StrUtilsTest(test.BaseTestCase):
self.assertEqual(six.u('ni\xf1o'), safe_decode('ni\xc3\xb1o',
incoming='ascii'))
+ self.assertEqual(six.u('foo'), safe_decode(b'foo'))
+
def test_safe_encode(self):
safe_encode = strutils.safe_encode
self.assertRaises(TypeError, safe_encode, True)
@@ -175,6 +177,7 @@ class StrUtilsTest(test.BaseTestCase):
# 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'))
def test_slugify(self):
to_slug = strutils.to_slug