From e7c425835169428b32ffeb95437f579bd2e51ab3 Mon Sep 17 00:00:00 2001 From: Huanian Li Date: Fri, 7 Feb 2025 16:26:11 +0800 Subject: [PATCH] Support to print unicode string with python2 To run a shell command via Python, it may be invoked by python2 or python3 as '/usr/bin/env python' is used by tone. For python2, the output should be encoded as 'utf-8' before it is printed in case the language of the system under test is not English (e.g. zh_CN.UTF-8); For python3, it is okay to print the output directly because it naturally supports unicode. Signed-off-by: Huanian Li --- core/utils/exec_cmd.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/utils/exec_cmd.py b/core/utils/exec_cmd.py index 2b0c145..5e8f38e 100644 --- a/core/utils/exec_cmd.py +++ b/core/utils/exec_cmd.py @@ -206,8 +206,10 @@ class ExecCmd(object): break data += bufferline if not self.is_quiet: + # To support python2, we should encode the data + output = data.encode('utf-8') if sys.version_info.major < 3 else data try: - print(data,end='') + print(output, end='') except Exception as e: logger.debug("-------------------------------") raise BaseException("{}\n{}".format(e, traceback.format_exc())) -- Gitee