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
4 changes: 1 addition & 3 deletions docker/api/exec_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import shlex

import six

from .. import errors
Expand All @@ -20,7 +18,7 @@ def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False,
'User-specific exec is not supported in API < 1.19'
)
if isinstance(cmd, six.string_types):
cmd = shlex.split(str(cmd))
cmd = utils.split_command(cmd)

data = {
'Container': container,
Expand Down
2 changes: 1 addition & 1 deletion docker/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mkbuildcontext, tar, exclude_paths, parse_repository_tag, parse_host,
kwargs_from_env, convert_filters, create_host_config,
create_container_config, parse_bytes, ping_registry, parse_env_file,
version_lt, version_gte, decode_json_header
version_lt, version_gte, decode_json_header, split_command,
) # flake8: noqa

from .types import Ulimit, LogConfig # flake8: noqa
Expand Down
10 changes: 8 additions & 2 deletions docker/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ def parse_env_file(env_file):
return environment


def split_command(command):
if six.PY2:
command = command.encode('utf-8')
return shlex.split(command)


def create_container_config(
version, image, command, hostname=None, user=None, detach=False,
stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None,
Expand All @@ -682,10 +688,10 @@ def create_container_config(
labels=None, volume_driver=None
):
if isinstance(command, six.string_types):
command = shlex.split(str(command))
command = split_command(command)

if isinstance(entrypoint, six.string_types):
entrypoint = shlex.split(str(entrypoint))
entrypoint = split_command(entrypoint)

if isinstance(environment, dict):
environment = [
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from docker.utils import (
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file,
exclude_paths, convert_volume_binds, decode_json_header, tar
exclude_paths, convert_volume_binds, decode_json_header, tar,
split_command,
)
from docker.utils.ports import build_port_bindings, split_port

Expand Down Expand Up @@ -389,6 +390,18 @@ def test_decode_json_header(self):
self.assertEqual(obj, decoded_data)


class SplitCommandTest(base.BaseTestCase):

@pytest.mark.skipif(six.PY2, reason="shlex doesn't support unicode in py2")
def test_split_command_with_unicode(self):
self.assertEqual(split_command('echo μ'), ['echo', 'μ'])

@pytest.mark.skipif(six.PY3, reason="shlex doesn't support unicode in py2")
def test_split_command_with_bytes(self):
expected = ['echo', u'μ'.encode('utf-8')]
self.assertEqual(split_command(u'echo μ'), expected)


class PortsTest(base.BaseTestCase):
def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
Expand Down