]> granicus.if.org Git - python/commitdiff
Fixed #29132: Updated shlex to work better with punctuation chars in POSIX mode.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Sun, 15 Jan 2017 10:06:52 +0000 (10:06 +0000)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Sun, 15 Jan 2017 10:06:52 +0000 (10:06 +0000)
Thanks to Evan_ for the report and patch.

Lib/shlex.py
Lib/test/test_shlex.py

index e87266f8dd0701128a29df59ebd1c65f7776c11e..2c9786c517a350f2bd766b539e41ac860f9dd435 100644 (file)
@@ -232,11 +232,6 @@ class shlex:
                             break   # emit current token
                         else:
                             continue
-                elif self.posix and nextchar in self.quotes:
-                    self.state = nextchar
-                elif self.posix and nextchar in self.escape:
-                    escapedstate = 'a'
-                    self.state = nextchar
                 elif self.state == 'c':
                     if nextchar in self.punctuation_chars:
                         self.token += nextchar
@@ -245,6 +240,11 @@ class shlex:
                             self._pushback_chars.append(nextchar)
                         self.state = ' '
                         break
+                elif self.posix and nextchar in self.quotes:
+                    self.state = nextchar
+                elif self.posix and nextchar in self.escape:
+                    escapedstate = 'a'
+                    self.state = nextchar
                 elif (nextchar in self.wordchars or nextchar in self.quotes
                       or self.whitespace_split):
                     self.token += nextchar
index 3936c97c8b997d78ffdb9dd034c1a11c069e64a8..fd35788e81b27203b9a5825f498fff74cf7c79aa 100644 (file)
@@ -273,6 +273,14 @@ class ShlexTest(unittest.TestCase):
         # white space
         self.assertEqual(list(s), ['a', '&&', 'b', '||', 'c'])
 
+    def testPunctuationWithPosix(self):
+        """Test that punctuation_chars and posix behave correctly together."""
+        # see Issue #29132
+        s = shlex.shlex('f >"abc"', posix=True, punctuation_chars=True)
+        self.assertEqual(list(s), ['f', '>', 'abc'])
+        s = shlex.shlex('f >\\"abc\\"', posix=True, punctuation_chars=True)
+        self.assertEqual(list(s), ['f', '>', '"abc"'])
+
     def testEmptyStringHandling(self):
         """Test that parsing of empty strings is correctly handled."""
         # see Issue #21999