Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 61eda72

Browse files
committed
Fixed python#29132: Updated shlex to work better with punctuation chars in POSIX mode.
Thanks to Evan_ for the report and patch.
1 parent 2e1b6ea commit 61eda72

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Lib/shlex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,6 @@ def read_token(self):
232232
break # emit current token
233233
else:
234234
continue
235-
elif self.posix and nextchar in self.quotes:
236-
self.state = nextchar
237-
elif self.posix and nextchar in self.escape:
238-
escapedstate = 'a'
239-
self.state = nextchar
240235
elif self.state == 'c':
241236
if nextchar in self.punctuation_chars:
242237
self.token += nextchar
@@ -245,6 +240,11 @@ def read_token(self):
245240
self._pushback_chars.append(nextchar)
246241
self.state = ' '
247242
break
243+
elif self.posix and nextchar in self.quotes:
244+
self.state = nextchar
245+
elif self.posix and nextchar in self.escape:
246+
escapedstate = 'a'
247+
self.state = nextchar
248248
elif (nextchar in self.wordchars or nextchar in self.quotes
249249
or self.whitespace_split):
250250
self.token += nextchar

Lib/test/test_shlex.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ def testPunctuationWithWhitespaceSplit(self):
273273
# white space
274274
self.assertEqual(list(s), ['a', '&&', 'b', '||', 'c'])
275275

276+
def testPunctuationWithPosix(self):
277+
"""Test that punctuation_chars and posix behave correctly together."""
278+
# see Issue #29132
279+
s = shlex.shlex('f >"abc"', posix=True, punctuation_chars=True)
280+
self.assertEqual(list(s), ['f', '>', 'abc'])
281+
s = shlex.shlex('f >\\"abc\\"', posix=True, punctuation_chars=True)
282+
self.assertEqual(list(s), ['f', '>', '"abc"'])
283+
276284
def testEmptyStringHandling(self):
277285
"""Test that parsing of empty strings is correctly handled."""
278286
# see Issue #21999

0 commit comments

Comments
 (0)