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.
This commit is contained in:
parent
3cce3e3ce4
commit
4d9ef49b49
@ -26,6 +26,7 @@
|
||||
import re
|
||||
import subprocess
|
||||
from six import string_types
|
||||
import sys
|
||||
|
||||
import llnl.util.tty as tty
|
||||
import spack
|
||||
@ -184,9 +185,9 @@ def streamify(arg, mode):
|
||||
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 @@ def __str__(self):
|
||||
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.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user