diff options
author | Chase Qi <chase.qi@linaro.org> | 2017-06-26 15:54:05 +0800 |
---|---|---|
committer | Chase Qi <chase.qi@linaro.org> | 2017-06-27 09:50:34 +0800 |
commit | 3efa769f81945d816da1d1f16f362dd34ee15c2a (patch) | |
tree | 83b263ddfa01bc8dcb3f19e1f7b50ef43be41be1 | |
parent | f477f2e6f9e3760dc6bcafba470f3c1e38c3b52c (diff) |
test-runner: fix UnicodeEncodeError with Python2
decode('utf-8') was added to strip off b' prefix from Python3's output.
The current code also runs that with Python2, it worked in most cases
some how. Howerver, once test-runner output redirectory used, it hits
UnicodeEncodeError when running tests include wget download step like
ltp and libhugetlbfs tests.
Usage: python2 automated/utils/test-runner.py \
-d automated/linux/libhugetlbfs/libhugetlbfs.yaml \
-o /root/output/libhugetlbfs \
> test-runner-stdout.log 2> test-runner-stderr.log
Error: UnicodeEncodeError: 'ascii' codec can't encode character
u'\u2018' in position 11: ordinal not in range(128)
This patch detects Python version at runtime, then uses decode('utf-8')
for on Python3 pexpect and subprocess.check_output() output to remove
prefix 'b'.
Change-Id: Ie52192787a8e4cd0550e1cd05b1ca53a02f4ab63
Signed-off-by: Chase Qi <chase.qi@linaro.org>
-rwxr-xr-x | automated/utils/test-runner.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/automated/utils/test-runner.py b/automated/utils/test-runner.py index 88d4177..67175f1 100755 --- a/automated/utils/test-runner.py +++ b/automated/utils/test-runner.py @@ -37,7 +37,10 @@ def run_command(command, target=None): logger = logging.getLogger('RUNNER.run_command') logger.debug(run) - return subprocess.check_output(shlex.split(run)).strip().decode('utf-8') + if sys.version_info[0] < 3: + return subprocess.check_output(shlex.split(run)).strip() + else: + return subprocess.check_output(shlex.split(run)).strip().decode('utf-8') class TestPlan(object): @@ -287,7 +290,10 @@ class AutomatedTestRun(TestRun): break try: self.child.expect('\r\n') - print(self.child.before.decode('utf-8')) + if sys.version_info[0] < 3: + print(self.child.before) + else: + print(self.child.before.decode('utf-8')) except pexpect.TIMEOUT: continue except pexpect.EOF: @@ -575,10 +581,10 @@ class ResultParser(object): else: path = os.getcwd() os.chdir(self.test['test_path']) - # In Python3's subprocess module, check_output() returns byte - # string; while getoutput() returns string but it it not available - # in Python2. So use decode() to remove the prefix 'b' for Python3. - test_version = subprocess.check_output("git rev-parse HEAD", shell=True).decode('utf-8') + if sys.version_info[0] < 3: + test_version = subprocess.check_output("git rev-parse HEAD", shell=True) + else: + test_version = subprocess.check_output("git rev-parse HEAD", shell=True).decode('utf-8') self.results['version'] = test_version.rstrip() os.chdir(path) |