diff --git a/IPython/utils/_process_common.py b/IPython/utils/_process_common.py index 0843e89..f77cf59 100644 --- a/IPython/utils/_process_common.py +++ b/IPython/utils/_process_common.py @@ -15,6 +15,7 @@ of subprocess utilities, and it contains tools that are common to all of them. # Imports #----------------------------------------------------------------------------- import subprocess +import shlex import sys from IPython.utils import py3compat @@ -143,3 +144,27 @@ def getoutputerror(cmd): return '', '' out, err = out_err return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err) + + +def arg_split(s, posix=False): + """Split a command line's arguments in a shell-like manner. + + This is a modified version of the standard library's shlex.split() + function, but with a default of posix=False for splitting, so that quotes + in inputs are respected.""" + + # Unfortunately, python's shlex module is buggy with unicode input: + # http://bugs.python.org/issue1170 + # At least encoding the input when it's unicode seems to help, but there + # may be more problems lurking. Apparently this is fixed in python3. + is_unicode = False + if (not py3compat.PY3) and isinstance(s, unicode): + is_unicode = True + s = s.encode('utf-8') + lex = shlex.shlex(s, posix=posix) + lex.whitespace_split = True + tokens = list(lex) + if is_unicode: + # Convert the tokens back to unicode. + tokens = [x.decode('utf-8') for x in tokens] + return tokens diff --git a/IPython/utils/_process_posix.py b/IPython/utils/_process_posix.py index 3147cb4..6227f35 100644 --- a/IPython/utils/_process_posix.py +++ b/IPython/utils/_process_posix.py @@ -18,13 +18,12 @@ from __future__ import print_function # Stdlib import subprocess as sp import sys -import shlex from IPython.external import pexpect # Our own from .autoattr import auto_attr -from ._process_common import getoutput +from ._process_common import getoutput, arg_split from IPython.utils import text from IPython.utils import py3compat @@ -194,28 +193,5 @@ class ProcessHandler(object): # (ls is a good example) that makes them hard. system = ProcessHandler().system -def arg_split(s, posix=False): - """Split a command line's arguments in a shell-like manner. - - This is a modified version of the standard library's shlex.split() - function, but with a default of posix=False for splitting, so that quotes - in inputs are respected.""" - - # Unfortunately, python's shlex module is buggy with unicode input: - # http://bugs.python.org/issue1170 - # At least encoding the input when it's unicode seems to help, but there - # may be more problems lurking. Apparently this is fixed in python3. - is_unicode = False - if (not py3compat.PY3) and isinstance(s, unicode): - is_unicode = True - s = s.encode('utf-8') - lex = shlex.shlex(s, posix=posix) - lex.whitespace_split = True - tokens = list(lex) - if is_unicode: - # Convert the tokens back to unicode. - tokens = [x.decode('utf-8') for x in tokens] - return tokens - diff --git a/IPython/utils/_process_win32.py b/IPython/utils/_process_win32.py index 29e74d4..9de9edf 100644 --- a/IPython/utils/_process_win32.py +++ b/IPython/utils/_process_win32.py @@ -175,27 +175,4 @@ try: retval = LocalFree(result_pointer) return result except AttributeError: - import shlex - #alternative if CommandLineToArgvW is not available - def arg_split(s, posix=False): - """Split a command line's arguments in a shell-like manner. - - This is a modified version of the standard library's shlex.split() - function, but with a default of posix=False for splitting, so that quotes - in inputs are respected.""" - - # Unfortunately, python's shlex module is buggy with unicode input: - # http://bugs.python.org/issue1170 - # At least encoding the input when it's unicode seems to help, but there - # may be more problems lurking. Apparently this is fixed in python3. - is_unicode = False - if (not py3compat.PY3) and isinstance(s, unicode): - is_unicode = True - s = s.encode('utf-8') - lex = shlex.shlex(s, posix=posix) - lex.whitespace_split = True - tokens = list(lex) - if is_unicode: - # Convert the tokens back to unicode. - tokens = [x.decode('utf-8') for x in tokens] - return tokens + from ._process_common import arg_split