Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48f3dcc

Browse files
committed
- Changed shlex.split() method to have more useful and
meaningful parameters.
1 parent cf146d3 commit 48f3dcc

3 files changed

Lines changed: 13 additions & 16 deletions

File tree

Doc/lib/libshlex.tex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ \subsection{Module Contents}
2525

2626
The \module{shlex} module defines the following functions:
2727

28-
\begin{funcdesc}{split}{s\optional{, posix=\code{True}\optional{,
29-
spaces=\code{True}}}}
30-
Split the string \var{s} using shell-like syntax. If \var{posix} is
31-
\code{True}, operate in \POSIX{} mode. If \var{spaces} is \code{True}, it
32-
will only split words in whitespaces (setting the
33-
\member{whitespace_split} member of the \class{shlex} instance).
28+
\begin{funcdesc}{split}{s\optional{, comments=\code{False}}}
29+
Split the string \var{s} using shell-like syntax. If \var{comments} is
30+
\code{False}, the parsing of comments in the given string will be
31+
disabled (setting the \member{commenters} member of the \class{shlex}
32+
instance to the empty string). This function operates in \POSIX{} mode.
3433
\versionadded{2.3}
3534
\end{funcdesc}
3635

Lib/shlex.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,11 @@ def next(self):
271271
raise StopIteration
272272
return token
273273

274-
def split(s, posix=True, spaces=True):
275-
lex = shlex(s, posix=posix)
276-
lex.whitespace_split = spaces
274+
def split(s, comments=False):
275+
lex = shlex(s, posix=True)
276+
lex.whitespace_split = True
277+
if not comments:
278+
lex.commenters = ''
277279
return list(lex)
278280

279281
if __name__ == '__main__':

Lib/test/test_shlex.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ def setUp(self):
151151
for item in self.posix_data:
152152
item[0] = item[0].replace(r"\n", "\n")
153153

154-
def splitTest(self, data, posix, spaces):
154+
def splitTest(self, data, comments):
155155
for i in range(len(data)):
156-
l = shlex.split(data[i][0], posix=posix, spaces=spaces)
156+
l = shlex.split(data[i][0], comments=comments)
157157
self.assertEqual(l, data[i][1:],
158158
"%s: %s != %s" %
159159
(data[i][0], l, data[i][1:]))
@@ -167,13 +167,9 @@ def oldSplit(self, s):
167167
tok = lex.get_token()
168168
return ret
169169

170-
def testSplit(self):
171-
"""Test data splitting with non-posix parser"""
172-
self.splitTest(self.data, posix=0, spaces=0)
173-
174170
def testSplitPosix(self):
175171
"""Test data splitting with posix parser"""
176-
self.splitTest(self.posix_data, posix=1, spaces=1)
172+
self.splitTest(self.posix_data, comments=True)
177173

178174
def testCompat(self):
179175
"""Test compatibility interface"""

0 commit comments

Comments
 (0)