[Python-checkins] cpython (3.5): Closes #21999: Handled empty strings correctly when in POSIX mode.

vinay.sajip python-checkins at python.org
Tue Aug 9 10:07:16 EDT 2016


https://hg.python.org/cpython/rev/4f02ad46a0a8
changeset:   102579:4f02ad46a0a8
branch:      3.5
parent:      102572:87e3a58ed3c3
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Tue Aug 09 15:04:49 2016 +0100
summary:
  Closes #21999: Handled empty strings correctly when in POSIX mode.

files:
  Lib/shlex.py           |   2 +-
  Lib/test/test_shlex.py |  12 ++++++++++++
  2 files changed, 13 insertions(+), 1 deletions(-)


diff --git a/Lib/shlex.py b/Lib/shlex.py
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -224,7 +224,7 @@
                     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
--- a/Lib/test/test_shlex.py
+++ b/Lib/test/test_shlex.py
@@ -173,6 +173,18 @@
                              "%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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list