generate-constraints: Fix error handling in Python3

When handling an error, we were trying to combine data from the output
of multiple commands, some of which had already been converted to
strings (from bytes) and some of which had not. This is forbidden in
Python 3. (We were also trying to decode UTF-8 multiple times as a
result.)

Store all of the output as bytes, and decode if and when it is needed.

Change-Id: I35cd872f7bb38be1006bd6bf8dc910a1b52aadeb
This commit is contained in:
Zane Bitter 2018-10-04 16:04:39 -04:00
parent 3de0a910cd
commit cdecb6a46c

View File

@ -70,9 +70,9 @@ def _freeze(requirements, python):
output = [] output = []
try: try:
version_out = subprocess.check_output( version_out = subprocess.check_output(
[python, "--version"], stderr=subprocess.STDOUT).decode('utf-8') [python, "--version"], stderr=subprocess.STDOUT)
output.append(version_out) output.append(version_out)
version_all = version_out.split()[1] version_all = version_out.decode('utf-8').split()[1]
version = '.'.join(version_all.split('.')[:2]) version = '.'.join(version_all.split('.')[:2])
with fixtures.TempDir() as temp: with fixtures.TempDir() as temp:
output.append(subprocess.check_output( output.append(subprocess.check_output(
@ -83,15 +83,15 @@ def _freeze(requirements, python):
output.append(subprocess.check_output( output.append(subprocess.check_output(
[pip_bin, 'install', '-r', requirements])) [pip_bin, 'install', '-r', requirements]))
freeze = subprocess.check_output( freeze = subprocess.check_output(
[pip_bin, 'freeze']).decode('utf-8') [pip_bin, 'freeze'])
output.append(freeze) output.append(freeze)
return (version, _parse_freeze(freeze)) return (version, _parse_freeze(freeze.decode('utf-8')))
except Exception as exc: except Exception as exc:
if isinstance(exc, subprocess.CalledProcessError): if isinstance(exc, subprocess.CalledProcessError):
output.append(exc.output.decode('utf-8')) output.append(exc.output)
raise Exception( raise Exception(
"Failed to generate freeze: %s %s" "Failed to generate freeze: %s %s"
% ('\n'.join(output).decode('utf-8'), exc)) % (b'\n'.join(output).decode('utf-8'), exc))
def _combine_freezes(freezes, blacklist=None): def _combine_freezes(freezes, blacklist=None):