From a00ce0134b5ac2ae6818304424f694739133b5e7 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela <47495861+HassanAbouelela@users.noreply.github.com> Date: Mon, 12 Oct 2020 09:51:30 +0300 Subject: [PATCH] Return Bytes from Split Allows split to return a list of bytes, given a byte input string. --- Lib/shlex.py | 6 +++++- Lib/test/test_shlex.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/shlex.py b/Lib/shlex.py index f4c4ad8e484..595ea890433 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -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): diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index ae9a6033e15..517d10aa421 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -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"""