Skip to content

Commit 059f61b

Browse files
committed
Do not break when calling format_environment with unicode values
Signed-off-by: Joffrey F <[email protected]>
1 parent 008730c commit 059f61b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

docker/utils/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,9 @@ def format_environment(environment):
10011001
def format_env(key, value):
10021002
if value is None:
10031003
return key
1004+
if isinstance(value, six.binary_type):
1005+
value = value.decode('utf-8')
1006+
10041007
return u'{key}={value}'.format(key=key, value=value)
10051008
return [format_env(*var) for var in six.iteritems(environment)]
10061009

tests/unit/utils_test.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file,
2121
exclude_paths, convert_volume_binds, decode_json_header, tar,
2222
split_command, create_ipam_config, create_ipam_pool, parse_devices,
23-
update_headers,
23+
update_headers
2424
)
2525

2626
from docker.utils.ports import build_port_bindings, split_port
27-
from docker.utils.utils import create_endpoint_config
27+
from docker.utils.utils import create_endpoint_config, format_environment
2828

2929
from .. import base
3030
from ..helpers import make_tree
@@ -1042,3 +1042,18 @@ def test_tar_with_directory_symlinks(self):
10421042
self.assertEqual(
10431043
sorted(tar_data.getnames()), ['bar', 'bar/foo', 'foo']
10441044
)
1045+
1046+
1047+
class FormatEnvironmentTest(base.BaseTestCase):
1048+
def test_format_env_binary_unicode_value(self):
1049+
env_dict = {
1050+
'ARTIST_NAME': b'\xec\x86\xa1\xec\xa7\x80\xec\x9d\x80'
1051+
}
1052+
assert format_environment(env_dict) == [u'ARTIST_NAME=송지은']
1053+
1054+
def test_format_env_no_value(self):
1055+
env_dict = {
1056+
'FOO': None,
1057+
'BAR': '',
1058+
}
1059+
assert sorted(format_environment(env_dict)) == ['BAR=', 'FOO']

0 commit comments

Comments
 (0)