From e59558476856500b665e3eaf7aab6553ce0b91b4 Mon Sep 17 00:00:00 2001 From: Misha Wolfson Date: Mon, 9 Jul 2018 15:37:28 -0400 Subject: [PATCH 1/3] Add failing test case for unicode command in PY2 --- tests/test_common.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_common.py b/tests/test_common.py index 2b65ba6f4..ff7b0b3ef 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -163,6 +163,10 @@ def test_tmux_cmd_raises_on_not_found(): tmux_cmd('-V') +def test_tmux_cmd_unicode(): + tmux_cmd('has-session', 'юникод') + + @pytest.mark.parametrize( "session_name,raises,exc_msg_regex", [ From 7cf0551dd13346843b720487ef4ec0d0ef1c7bdb Mon Sep 17 00:00:00 2001 From: Misha Wolfson Date: Mon, 9 Jul 2018 15:37:58 -0400 Subject: [PATCH 2/3] Force tmux_command to parse UTF-8 unicode in Python 2 --- libtmux/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libtmux/common.py b/libtmux/common.py index f5afbc106..c7fce9aec 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -14,7 +14,7 @@ from distutils.version import LooseVersion from . import exc -from ._compat import console_to_str +from ._compat import console_to_str, text_type logger = logging.getLogger(__name__) @@ -196,7 +196,7 @@ def __init__(self, *args, **kwargs): cmd = [tmux_bin] cmd += args # add the command arguments to cmd - cmd = [str(c) for c in cmd] + cmd = [text_type(c, encoding='utf_8') for c in cmd] self.cmd = cmd From 345de4596127e70796d0a67f3db071c59d2a80c9 Mon Sep 17 00:00:00 2001 From: Misha Wolfson Date: Mon, 9 Jul 2018 16:39:52 -0400 Subject: [PATCH 3/3] Make more extensive testing and more generic fix --- libtmux/_compat.py | 6 ++++++ libtmux/common.py | 4 ++-- tests/test_common.py | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libtmux/_compat.py b/libtmux/_compat.py index 1ba32179d..0ec847f86 100644 --- a/libtmux/_compat.py +++ b/libtmux/_compat.py @@ -92,3 +92,9 @@ def reraise(tp, value, tb=None): number_types = integer_types + (float,) + +def str_from_console(s): + try: + return text_type(s) + except UnicodeDecodeError: + return text_type(s, encoding='utf_8') diff --git a/libtmux/common.py b/libtmux/common.py index c7fce9aec..98bed804c 100644 --- a/libtmux/common.py +++ b/libtmux/common.py @@ -14,7 +14,7 @@ from distutils.version import LooseVersion from . import exc -from ._compat import console_to_str, text_type +from ._compat import console_to_str, str_from_console logger = logging.getLogger(__name__) @@ -196,7 +196,7 @@ def __init__(self, *args, **kwargs): cmd = [tmux_bin] cmd += args # add the command arguments to cmd - cmd = [text_type(c, encoding='utf_8') for c in cmd] + cmd = [str_from_console(c) for c in cmd] self.cmd = cmd diff --git a/tests/test_common.py b/tests/test_common.py index ff7b0b3ef..123e549dc 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -164,7 +164,7 @@ def test_tmux_cmd_raises_on_not_found(): def test_tmux_cmd_unicode(): - tmux_cmd('has-session', 'юникод') + tmux_cmd('new-window', '-t', 3, '-n', 'юникод', '-F', u'Ελληνικά') @pytest.mark.parametrize(