From 4d9ef49b49a5f2e3034e26de535e6744251a10fa Mon Sep 17 00:00:00 2001 From: scheibelp Date: Mon, 14 Aug 2017 11:23:03 -0700 Subject: Decode process stream only for python3 Popen.communicate outputs a str object for python2 and a bytes object for python3. This updates the Executable.__call__ function to call .decode on the output of Popen.communicate only for python3. This ensures that Executable.__call__ returns a str for python2 and python3. --- lib/spack/spack/util/executable.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/spack/spack/util/executable.py b/lib/spack/spack/util/executable.py index 584e224db7..5fd40790ed 100644 --- a/lib/spack/spack/util/executable.py +++ b/lib/spack/spack/util/executable.py @@ -26,6 +26,7 @@ import os import re import subprocess from six import string_types +import sys import llnl.util.tty as tty import spack @@ -184,9 +185,9 @@ class Executable(object): if output is str or error is str: result = '' if output is str: - result += out.decode('utf-8') + result += to_str(out) if error is str: - result += err.decode('utf-8') + result += to_str(err) return result except OSError as e: @@ -223,6 +224,20 @@ class Executable(object): return ' '.join(self.exe) +def to_str(content): + """Produce a str type from the content of a process stream obtained with + Popen.communicate. + """ + # Prior to python3, Popen.communicate returns a str type. For python3 it + # returns a bytes type. In the case of python3 we decode the + # byte string to produce a str type. This will generate junk if the + # encoding is not UTF-8 (which includes ASCII). + if sys.version_info < (3, 0, 0): + return content + else: + return content.decode('utf-8') + + def which(*args, **kwargs): """Finds an executable in the path like command-line which. -- cgit v1.2.3-60-g2f50