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

index f08391800b14f62dfeae12ff721ad4ba1b418d52..ecd2efd022502bf2ac99d7769d504ac0e24c7293 100644 (file)
@@ -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
index 4fafdd4f2e4588b3a90d3c6ec8c402e7e19b4df3..55b533d5ab77d12dd05d45f88e1c84cea3cc73a3 100644 (file)
@@ -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