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:
		| @@ -26,6 +26,7 @@ | |||||||
| import re | import re | ||||||
| import subprocess | import subprocess | ||||||
| from six import string_types | from six import string_types | ||||||
|  | import sys | ||||||
|  |  | ||||||
| import llnl.util.tty as tty | import llnl.util.tty as tty | ||||||
| import spack | import spack | ||||||
| @@ -184,9 +185,9 @@ def streamify(arg, mode): | |||||||
|             if output is str or error is str: |             if output is str or error is str: | ||||||
|                 result = '' |                 result = '' | ||||||
|                 if output is str: |                 if output is str: | ||||||
|                     result += out.decode('utf-8') |                     result += to_str(out) | ||||||
|                 if error is str: |                 if error is str: | ||||||
|                     result += err.decode('utf-8') |                     result += to_str(err) | ||||||
|                 return result |                 return result | ||||||
|  |  | ||||||
|         except OSError as e: |         except OSError as e: | ||||||
| @@ -223,6 +224,20 @@ def __str__(self): | |||||||
|         return ' '.join(self.exe) |         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): | def which(*args, **kwargs): | ||||||
|     """Finds an executable in the path like command-line which. |     """Finds an executable in the path like command-line which. | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 scheibelp
					scheibelp