Skip to content

Commit 929e046

Browse files
committed
Merge pull request #824 from dnephin/fix_unicode_commands
Support unicode commands
2 parents 0234dde + 417c800 commit 929e046

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

docker/api/exec_api.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import shlex
2-
31
import six
42

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

2523
data = {
2624
'Container': container,

docker/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mkbuildcontext, tar, exclude_paths, parse_repository_tag, parse_host,
44
kwargs_from_env, convert_filters, create_host_config,
55
create_container_config, parse_bytes, ping_registry, parse_env_file,
6-
version_lt, version_gte, decode_json_header
6+
version_lt, version_gte, decode_json_header, split_command,
77
) # flake8: noqa
88

99
from .types import Ulimit, LogConfig # flake8: noqa

docker/utils/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ def parse_env_file(env_file):
673673
return environment
674674

675675

676+
def split_command(command):
677+
if six.PY2:
678+
command = command.encode('utf-8')
679+
return shlex.split(command)
680+
681+
676682
def create_container_config(
677683
version, image, command, hostname=None, user=None, detach=False,
678684
stdin_open=False, tty=False, mem_limit=None, ports=None, environment=None,
@@ -682,10 +688,10 @@ def create_container_config(
682688
labels=None, volume_driver=None
683689
):
684690
if isinstance(command, six.string_types):
685-
command = shlex.split(str(command))
691+
command = split_command(command)
686692

687693
if isinstance(entrypoint, six.string_types):
688-
entrypoint = shlex.split(str(entrypoint))
694+
entrypoint = split_command(entrypoint)
689695

690696
if isinstance(environment, dict):
691697
environment = [

tests/unit/utils_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from docker.utils import (
1818
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
1919
create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file,
20-
exclude_paths, convert_volume_binds, decode_json_header, tar
20+
exclude_paths, convert_volume_binds, decode_json_header, tar,
21+
split_command,
2122
)
2223
from docker.utils.ports import build_port_bindings, split_port
2324

@@ -389,6 +390,18 @@ def test_decode_json_header(self):
389390
self.assertEqual(obj, decoded_data)
390391

391392

393+
class SplitCommandTest(base.BaseTestCase):
394+
395+
@pytest.mark.skipif(six.PY2, reason="shlex doesn't support unicode in py2")
396+
def test_split_command_with_unicode(self):
397+
self.assertEqual(split_command('echo μ'), ['echo', 'μ'])
398+
399+
@pytest.mark.skipif(six.PY3, reason="shlex doesn't support unicode in py2")
400+
def test_split_command_with_bytes(self):
401+
expected = ['echo', u'μ'.encode('utf-8')]
402+
self.assertEqual(split_command(u'echo μ'), expected)
403+
404+
392405
class PortsTest(base.BaseTestCase):
393406
def test_split_port_with_host_ip(self):
394407
internal_port, external_port = split_port("127.0.0.1:1000:2000")

0 commit comments

Comments
 (0)