Skip to content

Commit 9a3d882

Browse files
author
piterpunk
committed
Small changes to salt.utils.stringutils.is_binary
- Define `text_characters` only when it will be used - Adds commentaries about the `text_characters` definition - Fix a corner case if a utf-8 multibyte character is truncated and more than 30% of the byte array have values between 1 and 31.
1 parent fcd18ff commit 9a3d882

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

salt/utils/stringutils.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,6 @@ def is_binary(data):
182182
Detects if the passed string of data is binary or text
183183
"""
184184

185-
def int2byte(x):
186-
return bytes((x,))
187-
188-
text_characters = b"".join(int2byte(i) for i in range(32, 127)) + b"\n\r\t\f\b"
189-
190185
if not data or not isinstance(data, ((str,), bytes)):
191186
return False
192187

@@ -196,15 +191,24 @@ def int2byte(x):
196191
if b"\0" in data:
197192
return True
198193

194+
def int2byte(x):
195+
return bytes((x,))
196+
197+
# Defines the text characters as being the ASCII characters plus a fell
198+
# control characters (newline, carriage return, tab, formfeed and backspace)
199+
text_characters = b"".join(int2byte(i) for i in range(32, 127)) + b"\n\r\t\f\b"
200+
199201
try:
202+
# Defines the range of values used by UTF-8 multibyte characters
203+
mb_utf8_characters = b"".join(int2byte(i) for i in range(128, 255))
204+
200205
data.decode(__salt_system_encoding__)
201-
text_characters = text_characters + b"".join(
202-
int2byte(i) for i in range(128, 255)
203-
)
206+
207+
text_characters = text_characters + mb_utf8_characters
204208
except UnicodeDecodeError as err:
205209
# This handles truncated characters at end of string
206210
if err.reason == "unexpected end of data":
207-
return False
211+
text_characters = text_characters + mb_utf8_characters
208212

209213
nontext = data.translate(None, text_characters)
210214

0 commit comments

Comments
 (0)