##// END OF EJS Templates
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
Jörgen Stenarson -
Show More
@@ -151,26 +151,51 b' def getoutput(cmd):'
151 out = ''
151 out = ''
152 return out
152 return out
153
153
154
154 try:
155 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
155 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
156 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
156 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
157 CommandLineToArgvW.res_types = [POINTER(LPCWSTR)]
157 CommandLineToArgvW.res_types = [POINTER(LPCWSTR)]
158 LocalFree = ctypes.windll.kernel32.LocalFree
158 LocalFree = ctypes.windll.kernel32.LocalFree
159 LocalFree.res_type = HLOCAL
159 LocalFree.res_type = HLOCAL
160 LocalFree.arg_types = [HLOCAL]
160 LocalFree.arg_types = [HLOCAL]
161
161
162 def arg_split(commandline, posix=False):
162 def arg_split(commandline, posix=False):
163 """Split a command line's arguments in a shell-like manner.
163 """Split a command line's arguments in a shell-like manner.
164
164
165 This is a special version for windows that use a ctypes call to CommandLineToArgvW
165 This is a special version for windows that use a ctypes call to CommandLineToArgvW
166 to do the argv splitting. The posix paramter is ignored.
166 to do the argv splitting. The posix paramter is ignored.
167 """
167 """
168 #CommandLineToArgvW returns path to executable if called with empty string.
168 #CommandLineToArgvW returns path to executable if called with empty string.
169 if commandline.strip() == "":
169 if commandline.strip() == "":
170 return []
170 return []
171 argvn = c_int()
171 argvn = c_int()
172 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
172 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
173 result_array_type = LPCWSTR * argvn.value
173 result_array_type = LPCWSTR * argvn.value
174 result = [arg for arg in result_array_type.from_address(result_pointer)]
174 result = [arg for arg in result_array_type.from_address(result_pointer)]
175 retval = LocalFree(result_pointer)
175 retval = LocalFree(result_pointer)
176 return result
176 return result
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
General Comments 0
You need to be logged in to leave comments. Login now