Commit 48f3dcc9 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer

- Changed shlex.split() method to have more useful and

  meaningful parameters.
parent cf146d31
...@@ -25,12 +25,11 @@ Python applications) or for parsing quoted strings. ...@@ -25,12 +25,11 @@ Python applications) or for parsing quoted strings.
The \module{shlex} module defines the following functions: The \module{shlex} module defines the following functions:
\begin{funcdesc}{split}{s\optional{, posix=\code{True}\optional{, \begin{funcdesc}{split}{s\optional{, comments=\code{False}}}
spaces=\code{True}}}} Split the string \var{s} using shell-like syntax. If \var{comments} is
Split the string \var{s} using shell-like syntax. If \var{posix} is \code{False}, the parsing of comments in the given string will be
\code{True}, operate in \POSIX{} mode. If \var{spaces} is \code{True}, it disabled (setting the \member{commenters} member of the \class{shlex}
will only split words in whitespaces (setting the instance to the empty string). This function operates in \POSIX{} mode.
\member{whitespace_split} member of the \class{shlex} instance).
\versionadded{2.3} \versionadded{2.3}
\end{funcdesc} \end{funcdesc}
......
...@@ -271,9 +271,11 @@ class shlex: ...@@ -271,9 +271,11 @@ class shlex:
raise StopIteration raise StopIteration
return token return token
def split(s, posix=True, spaces=True): def split(s, comments=False):
lex = shlex(s, posix=posix) lex = shlex(s, posix=True)
lex.whitespace_split = spaces lex.whitespace_split = True
if not comments:
lex.commenters = ''
return list(lex) return list(lex)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -151,9 +151,9 @@ class ShlexTest(unittest.TestCase): ...@@ -151,9 +151,9 @@ class ShlexTest(unittest.TestCase):
for item in self.posix_data: for item in self.posix_data:
item[0] = item[0].replace(r"\n", "\n") item[0] = item[0].replace(r"\n", "\n")
def splitTest(self, data, posix, spaces): def splitTest(self, data, comments):
for i in range(len(data)): for i in range(len(data)):
l = shlex.split(data[i][0], posix=posix, spaces=spaces) l = shlex.split(data[i][0], comments=comments)
self.assertEqual(l, data[i][1:], self.assertEqual(l, data[i][1:],
"%s: %s != %s" % "%s: %s != %s" %
(data[i][0], l, data[i][1:])) (data[i][0], l, data[i][1:]))
...@@ -167,13 +167,9 @@ class ShlexTest(unittest.TestCase): ...@@ -167,13 +167,9 @@ class ShlexTest(unittest.TestCase):
tok = lex.get_token() tok = lex.get_token()
return ret return ret
def testSplit(self):
"""Test data splitting with non-posix parser"""
self.splitTest(self.data, posix=0, spaces=0)
def testSplitPosix(self): def testSplitPosix(self):
"""Test data splitting with posix parser""" """Test data splitting with posix parser"""
self.splitTest(self.posix_data, posix=1, spaces=1) self.splitTest(self.posix_data, comments=True)
def testCompat(self): def testCompat(self):
"""Test compatibility interface""" """Test compatibility interface"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment