From: Vinay Sajip Date: Tue, 9 Aug 2016 14:04:49 +0000 (+0100) Subject: Closes #21999: Handled empty strings correctly when in POSIX mode. X-Git-Tag: v3.6.0a4~45^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=085e806798f4f70e70a9c8cba31f240d8fd668b3;p=python Closes #21999: Handled empty strings correctly when in POSIX mode. --- diff --git a/Lib/shlex.py b/Lib/shlex.py index f08391800b..ecd2efd022 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -224,7 +224,7 @@ class shlex: if self.debug >= 2: print("shlex: I see punctuation in word state") self.state = ' ' - if self.token: + if self.token or (self.posix and quoted): break # emit current token else: continue diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index 4fafdd4f2e..55b533d5ab 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -173,6 +173,18 @@ class ShlexTest(unittest.TestCase): "%s: %s != %s" % (self.data[i][0], l, self.data[i][1:])) + def testEmptyStringHandling(self): + """Test that parsing of empty strings is correctly handled.""" + # see Issue #21999 + expected = ['', ')', 'abc'] + + s = shlex.shlex("'')abc", posix=True) + slist = list(s) + self.assertEqual(slist, expected) + expected = ["''", ')', 'abc'] + s = shlex.shlex("'')abc") + self.assertEqual(list(s), expected) + def testQuote(self): safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./' unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s