From cdecb6a46c25c7347c7d8fe2595397c8e1d85a53 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Thu, 4 Oct 2018 16:04:39 -0400 Subject: [PATCH] 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 --- openstack_requirements/cmds/generate.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/openstack_requirements/cmds/generate.py b/openstack_requirements/cmds/generate.py index 4c6d21ec3b..4cdebc49c1 100644 --- a/openstack_requirements/cmds/generate.py +++ b/openstack_requirements/cmds/generate.py @@ -70,9 +70,9 @@ def _freeze(requirements, python): output = [] try: version_out = subprocess.check_output( - [python, "--version"], stderr=subprocess.STDOUT).decode('utf-8') + [python, "--version"], stderr=subprocess.STDOUT) 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]) with fixtures.TempDir() as temp: output.append(subprocess.check_output( @@ -83,15 +83,15 @@ def _freeze(requirements, python): output.append(subprocess.check_output( [pip_bin, 'install', '-r', requirements])) freeze = subprocess.check_output( - [pip_bin, 'freeze']).decode('utf-8') + [pip_bin, 'freeze']) output.append(freeze) - return (version, _parse_freeze(freeze)) + return (version, _parse_freeze(freeze.decode('utf-8'))) except Exception as exc: if isinstance(exc, subprocess.CalledProcessError): - output.append(exc.output.decode('utf-8')) + output.append(exc.output) raise Exception( "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):