Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mog_commons/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.9'
__version__ = '0.1.10'
21 changes: 15 additions & 6 deletions src/mog_commons/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import errno
import subprocess
import six
from mog_commons.string import is_unicode
from mog_commons.functional import oget

Expand All @@ -12,17 +13,25 @@
# Process operations
#
def __convert_args(args, shell, cmd_encoding):
xs = []
# Note: workaround for http://bugs.python.org/issue8513
workaround = shell and sys.version_info[:2] == (3, 2) and not sys.platform == 'win32'

if isinstance(args, six.string_types):
# input as string
s = args.encode(cmd_encoding)
if workaround:
return ['/bin/sh', '-c', s], False
else:
return args, shell

# input as list
xs = ['/bin/sh', '-c'] if workaround else []
if shell:
args = [subprocess.list2cmdline(args)]
if shell and sys.version_info[:2] == (3, 2) and not sys.platform == 'win32':
# Note: workaround for http://bugs.python.org/issue8513
xs = ['/bin/sh', '-c']
shell = False
for a in args:
assert is_unicode(a), 'cmd must be unicode string, not %s' % type(a).__name__
xs.append(a.encode(cmd_encoding))
return xs, shell
return xs, shell and not workaround


def execute_command(args, shell=False, cwd=None, env=None, stdin=None, stdout=None, stderr=None, cmd_encoding='utf-8'):
Expand Down
2 changes: 2 additions & 0 deletions tests/mog_commons/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
class TestCommand(unittest.TestCase):
def test_execute_command(self):
self.assertEqual(execute_command(['exit', '2'], shell=True), 2)
self.assertEqual(execute_command('exit 3', shell=True), 3)
self.assertEqual(execute_command(['/bin/sh', '-c', 'exit 4'], shell=False), 4)

def test_capture_command(self):
self.assertEqual(capture_command(['echo', 'あいう'], shell=True), (0, 'あいう\n'.encode('utf-8'), b''))
Expand Down