[Python-checkins] cpython (3.6): Fixed #29132: Updated shlex to work better with punctuation chars in POSIX mode.

vinay.sajip python-checkins at python.org
Sun Jan 15 05:08:02 EST 2017


https://hg.python.org/cpython/rev/7e40c940df14
changeset:   106157:7e40c940df14
branch:      3.6
parent:      106155:ea0c488b9bac
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Sun Jan 15 10:06:52 2017 +0000
summary:
  Fixed #29132: Updated shlex to work better with punctuation chars in POSIX mode.

Thanks to Evan_ for the report and patch.

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


diff --git a/Lib/shlex.py b/Lib/shlex.py
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -232,11 +232,6 @@
                             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 @@
                             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
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
@@ -273,6 +273,14 @@
         # 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

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


More information about the Python-checkins mailing list