Return Bytes from Split

Allows split to return a list of bytes, given a byte input string.
This commit is contained in:
Hassan Abouelela 2020-10-12 09:51:30 +03:00
parent ab48481bd8
commit a00ce0134b
No known key found for this signature in database
GPG Key ID: 1B5B5D1B8BB0BC18
2 changed files with 7 additions and 3 deletions

View File

@ -316,7 +316,11 @@ def split(s, comments=False, posix=True):
lex.whitespace_split = True
if not comments:
lex.commenters = ''
return list(lex)
if isinstance(s, bytes):
return [i.encode("ascii") for i in lex]
else:
return list(lex)
def join(split_command):

View File

@ -171,9 +171,9 @@ class ShlexTest(unittest.TestCase):
"""Test data splitting with posix parser"""
self.splitTest(self.posix_data, comments=True)
def splitBytesTest(self):
def testSplitBytes(self):
"""Test byte objects splitting"""
self.assertEqual(shlex.split(b"split words"), [b"hello", b"world"])
self.assertEqual(shlex.split(b"split words"), [b"split", b"words"])
def testCompat(self):
"""Test compatibility interface"""