mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Make salt.utils.stringutils.to_binary work for bytestrings
Also make sure we're testing bytestring equivalents to each of the strings in the test.
This commit is contained in:
parent
1024000369
commit
5d98a8bedd
2 changed files with 38 additions and 5 deletions
|
@ -184,19 +184,27 @@ def is_binary(data):
|
|||
'''
|
||||
Detects if the passed string of data is binary or text
|
||||
'''
|
||||
if not data or not isinstance(data, six.string_types):
|
||||
if not data or not isinstance(data, (six.string_types, six.binary_type)):
|
||||
return False
|
||||
if str('\0') in data:
|
||||
|
||||
if isinstance(data, six.binary_type):
|
||||
if b'\0' in data:
|
||||
return True
|
||||
elif str('\0') in data:
|
||||
return True
|
||||
|
||||
text_characters = ''.join([chr(x) for x in range(32, 127)] + list('\n\r\t\b'))
|
||||
# Get the non-text characters (map each character to itself then use the
|
||||
# 'remove' option to get rid of the text characters.)
|
||||
if six.PY3:
|
||||
trans = ''.maketrans('', '', text_characters)
|
||||
nontext = data.translate(trans)
|
||||
if isinstance(data, six.binary_type):
|
||||
import salt.utils.data
|
||||
nontext = data.translate(None, salt.utils.data.encode(text_characters))
|
||||
else:
|
||||
trans = ''.maketrans('', '', text_characters)
|
||||
nontext = data.translate(trans)
|
||||
else:
|
||||
if isinstance(data, unicode): # pylint: disable=incompatible-py3-code
|
||||
if isinstance(data, six.text_type):
|
||||
trans_args = ({ord(x): None for x in text_characters},)
|
||||
else:
|
||||
trans_args = (None, str(text_characters)) # future lint: blacklisted-function
|
||||
|
|
|
@ -37,19 +37,44 @@ class StringutilsTestCase(TestCase):
|
|||
|
||||
def test_is_binary(self):
|
||||
self.assertFalse(salt.utils.stringutils.is_binary(LOREM_IPSUM))
|
||||
# Also test bytestring
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.is_binary(
|
||||
salt.utils.stringutils.is_binary(LOREM_IPSUM)
|
||||
)
|
||||
)
|
||||
|
||||
zero_str = '{0}{1}'.format(LOREM_IPSUM, '\0')
|
||||
self.assertTrue(salt.utils.stringutils.is_binary(zero_str))
|
||||
# Also test bytestring
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.is_binary(
|
||||
salt.utils.stringutils.to_bytes(zero_str)
|
||||
)
|
||||
)
|
||||
|
||||
# To to ensure safe exit if str passed doesn't evaluate to True
|
||||
self.assertFalse(salt.utils.stringutils.is_binary(''))
|
||||
self.assertFalse(salt.utils.stringutils.is_binary(b''))
|
||||
|
||||
nontext = 3 * (''.join([chr(x) for x in range(1, 32) if x not in (8, 9, 10, 12, 13)]))
|
||||
almost_bin_str = '{0}{1}'.format(LOREM_IPSUM[:100], nontext[:42])
|
||||
self.assertFalse(salt.utils.stringutils.is_binary(almost_bin_str))
|
||||
# Also test bytestring
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.is_binary(
|
||||
salt.utils.stringutils.to_bytes(almost_bin_str)
|
||||
)
|
||||
)
|
||||
|
||||
bin_str = almost_bin_str + '\x01'
|
||||
self.assertTrue(salt.utils.stringutils.is_binary(bin_str))
|
||||
# Also test bytestring
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.is_binary(
|
||||
salt.utils.stringutils.to_bytes(bin_str)
|
||||
)
|
||||
)
|
||||
|
||||
def test_to_str(self):
|
||||
for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
|
||||
|
|
Loading…
Add table
Reference in a new issue