Show More
@@ -15,6 +15,7 b' of subprocess utilities, and it contains tools that are common to all of them.' | |||
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | import subprocess |
|
18 | import shlex | |
|
18 | 19 | import sys |
|
19 | 20 | |
|
20 | 21 | from IPython.utils import py3compat |
@@ -143,3 +144,27 b' def getoutputerror(cmd):' | |||
|
143 | 144 | return '', '' |
|
144 | 145 | out, err = out_err |
|
145 | 146 | return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err) |
|
147 | ||
|
148 | ||
|
149 | def arg_split(s, posix=False): | |
|
150 | """Split a command line's arguments in a shell-like manner. | |
|
151 | ||
|
152 | This is a modified version of the standard library's shlex.split() | |
|
153 | function, but with a default of posix=False for splitting, so that quotes | |
|
154 | in inputs are respected.""" | |
|
155 | ||
|
156 | # Unfortunately, python's shlex module is buggy with unicode input: | |
|
157 | # http://bugs.python.org/issue1170 | |
|
158 | # At least encoding the input when it's unicode seems to help, but there | |
|
159 | # may be more problems lurking. Apparently this is fixed in python3. | |
|
160 | is_unicode = False | |
|
161 | if (not py3compat.PY3) and isinstance(s, unicode): | |
|
162 | is_unicode = True | |
|
163 | s = s.encode('utf-8') | |
|
164 | lex = shlex.shlex(s, posix=posix) | |
|
165 | lex.whitespace_split = True | |
|
166 | tokens = list(lex) | |
|
167 | if is_unicode: | |
|
168 | # Convert the tokens back to unicode. | |
|
169 | tokens = [x.decode('utf-8') for x in tokens] | |
|
170 | return tokens |
@@ -18,13 +18,12 b' from __future__ import print_function' | |||
|
18 | 18 | # Stdlib |
|
19 | 19 | import subprocess as sp |
|
20 | 20 | import sys |
|
21 | import shlex | |
|
22 | 21 | |
|
23 | 22 | from IPython.external import pexpect |
|
24 | 23 | |
|
25 | 24 | # Our own |
|
26 | 25 | from .autoattr import auto_attr |
|
27 | from ._process_common import getoutput | |
|
26 | from ._process_common import getoutput, arg_split | |
|
28 | 27 | from IPython.utils import text |
|
29 | 28 | from IPython.utils import py3compat |
|
30 | 29 | |
@@ -194,28 +193,5 b' class ProcessHandler(object):' | |||
|
194 | 193 | # (ls is a good example) that makes them hard. |
|
195 | 194 | system = ProcessHandler().system |
|
196 | 195 | |
|
197 | def arg_split(s, posix=False): | |
|
198 | """Split a command line's arguments in a shell-like manner. | |
|
199 | ||
|
200 | This is a modified version of the standard library's shlex.split() | |
|
201 | function, but with a default of posix=False for splitting, so that quotes | |
|
202 | in inputs are respected.""" | |
|
203 | ||
|
204 | # Unfortunately, python's shlex module is buggy with unicode input: | |
|
205 | # http://bugs.python.org/issue1170 | |
|
206 | # At least encoding the input when it's unicode seems to help, but there | |
|
207 | # may be more problems lurking. Apparently this is fixed in python3. | |
|
208 | is_unicode = False | |
|
209 | if (not py3compat.PY3) and isinstance(s, unicode): | |
|
210 | is_unicode = True | |
|
211 | s = s.encode('utf-8') | |
|
212 | lex = shlex.shlex(s, posix=posix) | |
|
213 | lex.whitespace_split = True | |
|
214 | tokens = list(lex) | |
|
215 | if is_unicode: | |
|
216 | # Convert the tokens back to unicode. | |
|
217 | tokens = [x.decode('utf-8') for x in tokens] | |
|
218 | return tokens | |
|
219 | ||
|
220 | 196 | |
|
221 | 197 |
@@ -175,27 +175,4 b' try:' | |||
|
175 | 175 | retval = LocalFree(result_pointer) |
|
176 | 176 | return result |
|
177 | 177 | except AttributeError: |
|
178 | import shlex | |
|
179 | #alternative if CommandLineToArgvW is not available | |
|
180 | def arg_split(s, posix=False): | |
|
181 | """Split a command line's arguments in a shell-like manner. | |
|
182 | ||
|
183 | This is a modified version of the standard library's shlex.split() | |
|
184 | function, but with a default of posix=False for splitting, so that quotes | |
|
185 | in inputs are respected.""" | |
|
186 | ||
|
187 | # Unfortunately, python's shlex module is buggy with unicode input: | |
|
188 | # http://bugs.python.org/issue1170 | |
|
189 | # At least encoding the input when it's unicode seems to help, but there | |
|
190 | # may be more problems lurking. Apparently this is fixed in python3. | |
|
191 | is_unicode = False | |
|
192 | if (not py3compat.PY3) and isinstance(s, unicode): | |
|
193 | is_unicode = True | |
|
194 | s = s.encode('utf-8') | |
|
195 | lex = shlex.shlex(s, posix=posix) | |
|
196 | lex.whitespace_split = True | |
|
197 | tokens = list(lex) | |
|
198 | if is_unicode: | |
|
199 | # Convert the tokens back to unicode. | |
|
200 | tokens = [x.decode('utf-8') for x in tokens] | |
|
201 | return tokens | |
|
178 | from ._process_common import arg_split |
General Comments 0
You need to be logged in to leave comments.
Login now