提交 a0b7f353 编写于 作者: C Cleber Rosa

String utilities: add checks for data type (text or binary)

To lay some common ground across Python versions, let's define
the criteria we have for binary or text data.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 510ca7f7
......@@ -27,6 +27,7 @@ And not notice until their code starts failing.
import itertools
import re
import sys
import string
from six import string_types, PY3
......@@ -278,3 +279,28 @@ def string_to_safe_path(input_str):
for bad_chr in FS_UNSAFE_CHARS:
input_str = input_str.replace(bad_chr, "_")
return input_str
def is_bytes(data):
"""
Checks if the data given is a sequence of bytes
And not a "text" type, that can be of multi-byte characters.
Also, this does NOT mean a bytearray type.
:param data: the instance to be checked if it falls under the definition
of an array of bytes.
"""
return isinstance(data, bytes)
def is_text(data):
"""
Checks if the data given is a suitable for holding text
That is, if it can hold text that requires more than one byte for
each character.
"""
if sys.version_info[0] < 3:
return isinstance(data, unicode)
return isinstance(data, str)
......@@ -77,6 +77,35 @@ class AstringTest(unittest.TestCase):
self.assertEqual(astring.string_to_safe_path(avocado),
"%s__" % avocado[:-2])
def test_is_bytes(self):
"""
Verifies what bytes means, basically that they are the same
thing accross Python 2 and 3 and can be decoded into "text"
"""
binary = b''
text = u''
self.assertTrue(astring.is_bytes(binary))
self.assertFalse(astring.is_bytes(text))
self.assertTrue(hasattr(binary, 'decode'))
self.assertTrue(astring.is_text(binary.decode()))
# on Python 2, each str member is also a single byte char
if sys.version_info[0] < 3:
self.assertTrue(astring.is_bytes(str('')))
else:
self.assertFalse(astring.is_bytes(str('')))
def test_is_text(self):
"""
Verifies what text means, basically that they can represent
extended set of characters and can be encoded into "bytes"
"""
binary = b''
text = u''
self.assertTrue(astring.is_text(text))
self.assertFalse(astring.is_text(binary))
self.assertTrue(hasattr(text, 'encode'))
self.assertTrue(astring.is_bytes(text.encode()))
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册