From: Vinay Sajip Date: Tue, 9 Aug 2016 13:57:03 +0000 (+0100) Subject: Issue #21999: Handled empty strings correctly when in POSIX mode. X-Git-Tag: v2.7.13rc1~226 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fd16fcaf6b0cb7f54f8cc659e943dd6578e81e86;p=python Issue #21999: Handled empty strings correctly when in POSIX mode. --- diff --git a/Lib/shlex.py b/Lib/shlex.py index e7c8accd42..7bef0ddc6c 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -230,7 +230,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 6c35f491b5..ed741722af 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -178,6 +178,19 @@ 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) + + # Allow this test to be used with old shlex.py if not getattr(shlex, "split", None): for methname in dir(ShlexTest):