]> granicus.if.org Git - python/commitdiff
Issue #21999: Handled empty strings correctly when in POSIX mode.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Tue, 9 Aug 2016 13:57:03 +0000 (14:57 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Tue, 9 Aug 2016 13:57:03 +0000 (14:57 +0100)
Lib/shlex.py
Lib/test/test_shlex.py

index e7c8accd427590b035bdf47f82d005daec879c63..7bef0ddc6c986836aecdad297b2f388fb10eb207 100644 (file)
@@ -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
index 6c35f491b5bbf99a0baf7075e21223341d52e542..ed741722af1822ec98c2662f3db9e5d43d1edd91 100644 (file)
@@ -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):