Skip to content

Commit

Permalink
Moving posix version of arg_split to _process_common.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jörgen Stenarson committed Nov 30, 2011
1 parent 2381b09 commit c72bbc7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
25 changes: 25 additions & 0 deletions IPython/utils/_process_common.py
Expand Up @@ -15,6 +15,7 @@
# Imports
#-----------------------------------------------------------------------------
import subprocess
import shlex
import sys

from IPython.utils import py3compat
Expand Down Expand Up @@ -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
26 changes: 1 addition & 25 deletions IPython/utils/_process_posix.py
Expand Up @@ -18,13 +18,12 @@
# 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

Expand Down Expand Up @@ -194,28 +193,5 @@ def system(self, cmd):
# (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



25 changes: 1 addition & 24 deletions IPython/utils/_process_win32.py
Expand Up @@ -175,27 +175,4 @@ def arg_split(commandline, posix=False):
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

0 comments on commit c72bbc7

Please sign in to comment.