diff options
author | scheibelp <scheibel1@llnl.gov> | 2017-08-14 11:23:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-14 11:23:03 -0700 |
commit | 4d9ef49b49a5f2e3034e26de535e6744251a10fa (patch) | |
tree | efa117f7ad7355a9aeb3488ee6ed1cecb1bc2b4f | |
parent | 3cce3e3ce4c36cd9330c778cf5599e69b3dc1035 (diff) | |
download | spack-4d9ef49b49a5f2e3034e26de535e6744251a10fa.tar.gz spack-4d9ef49b49a5f2e3034e26de535e6744251a10fa.tar.bz2 spack-4d9ef49b49a5f2e3034e26de535e6744251a10fa.tar.xz spack-4d9ef49b49a5f2e3034e26de535e6744251a10fa.zip |
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.
-rw-r--r-- | lib/spack/spack/util/executable.py | 19 |
1 files 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. |