From 5d97c3186b43c4d94654c6cdacb73e7af509056e Mon Sep 17 00:00:00 2001 From: Valentin Schneider Date: Thu, 20 Sep 2018 12:37:52 -0700 Subject: [PATCH] Add a fallback for sys.stdout.encoding uses In some environments (e.g. nosetest), sys.stdout.encoding can be None. Add a fallback to handle these cases. --- devlib/instrument/acmecape.py | 2 +- devlib/instrument/energy_probe.py | 4 ++-- devlib/instrument/monsoon.py | 4 ++-- devlib/trace/ftrace.py | 2 +- devlib/utils/android.py | 2 +- devlib/utils/ssh.py | 2 +- devlib/utils/types.py | 4 ++-- devlib/utils/version.py | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/devlib/instrument/acmecape.py b/devlib/instrument/acmecape.py index adeba5a..16a3e1b 100644 --- a/devlib/instrument/acmecape.py +++ b/devlib/instrument/acmecape.py @@ -112,7 +112,7 @@ class AcmeCapeInstrument(Instrument): raise HostError(msg.format(output)) if self.process.returncode != 15: # iio-capture exits with 15 when killed if sys.version_info[0] == 3: - output += self.process.stdout.read().decode(sys.stdout.encoding, 'replace') + output += self.process.stdout.read().decode(sys.stdout.encoding or 'utf-8', 'replace') else: output += self.process.stdout.read() self.logger.info('ACME instrument encountered an error, ' diff --git a/devlib/instrument/energy_probe.py b/devlib/instrument/energy_probe.py index 6c9de83..16aa7e5 100644 --- a/devlib/instrument/energy_probe.py +++ b/devlib/instrument/energy_probe.py @@ -82,8 +82,8 @@ class EnergyProbeInstrument(Instrument): if self.process.returncode is not None: stdout, stderr = self.process.communicate() if sys.version_info[0] == 3: - stdout = stdout.decode(sys.stdout.encoding, 'replace') - stderr = stderr.decode(sys.stdout.encoding, 'replace') + stdout = stdout.decode(sys.stdout.encoding or 'utf-8', 'replace') + stderr = stderr.decode(sys.stdout.encoding or 'utf-8', 'replace') raise HostError( 'Energy Probe: Caiman exited unexpectedly with exit code {}.\n' 'stdout:\n{}\nstderr:\n{}'.format(self.process.returncode, diff --git a/devlib/instrument/monsoon.py b/devlib/instrument/monsoon.py index 6360249..ac1741d 100644 --- a/devlib/instrument/monsoon.py +++ b/devlib/instrument/monsoon.py @@ -101,8 +101,8 @@ class MonsoonInstrument(Instrument): if process.returncode is not None: stdout, stderr = process.communicate() if sys.version_info[0] == 3: - stdout = stdout.encode(sys.stdout.encoding) - stderr = stderr.encode(sys.stdout.encoding) + stdout = stdout.encode(sys.stdout.encoding or 'utf-8') + stderr = stderr.encode(sys.stdout.encoding or 'utf-8') raise HostError( 'Monsoon script exited unexpectedly with exit code {}.\n' 'stdout:\n{}\nstderr:\n{}'.format(process.returncode, diff --git a/devlib/trace/ftrace.py b/devlib/trace/ftrace.py index 8fef36c..3a0b513 100644 --- a/devlib/trace/ftrace.py +++ b/devlib/trace/ftrace.py @@ -281,7 +281,7 @@ class FtraceCollector(TraceCollector): process = subprocess.Popen(command, stderr=subprocess.PIPE, shell=True) _, error = process.communicate() if sys.version_info[0] == 3: - error = error.decode(sys.stdout.encoding, 'replace') + error = error.decode(sys.stdout.encoding or 'utf-8', 'replace') if process.returncode: raise TargetStableError('trace-cmd returned non-zero exit code {}'.format(process.returncode)) if error: diff --git a/devlib/utils/android.py b/devlib/utils/android.py index 604d820..719d2eb 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -152,7 +152,7 @@ class ApkInfo(object): try: output = subprocess.check_output(command, stderr=subprocess.STDOUT) if sys.version_info[0] == 3: - output = output.decode(sys.stdout.encoding, 'replace') + output = output.decode(sys.stdout.encoding or 'utf-8', 'replace') except subprocess.CalledProcessError as e: raise HostError('Error parsing APK file {}. `aapt` says:\n{}' .format(apk_path, e.output)) diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py index 8d4088b..f25d6cd 100644 --- a/devlib/utils/ssh.py +++ b/devlib/utils/ssh.py @@ -280,7 +280,7 @@ class SshConnection(object): # the regex removes line breaks potential introduced when writing # command to shell. if sys.version_info[0] == 3: - output = process_backspaces(self.conn.before.decode(sys.stdout.encoding, 'replace')) + output = process_backspaces(self.conn.before.decode(sys.stdout.encoding or 'utf-8', 'replace')) else: output = process_backspaces(self.conn.before) output = re.sub(r'\r([^\n])', r'\1', output) diff --git a/devlib/utils/types.py b/devlib/utils/types.py index c806f60..f212930 100644 --- a/devlib/utils/types.py +++ b/devlib/utils/types.py @@ -153,11 +153,11 @@ if sys.version_info[0] == 3: if isinstance(value, regex_type): if isinstance(value.pattern, bytes): return value - return re.compile(value.pattern.encode(sys.stdout.encoding), + return re.compile(value.pattern.encode(sys.stdout.encoding or 'utf-8'), value.flags & ~re.UNICODE) else: if isinstance(value, str): - value = value.encode(sys.stdout.encoding) + value = value.encode(sys.stdout.encoding or 'utf-8') return re.compile(value) else: def regex(value): diff --git a/devlib/utils/version.py b/devlib/utils/version.py index 5e7b688..16b4308 100644 --- a/devlib/utils/version.py +++ b/devlib/utils/version.py @@ -25,6 +25,6 @@ def get_commit(): if p.returncode: return None if sys.version_info[0] == 3 and isinstance(std, bytes): - return std[:8].decode(sys.stdout.encoding, 'replace') + return std[:8].decode(sys.stdout.encoding or 'utf-8', 'replace') else: return std[:8]