From 45f5fa21b16934a2e1f937295f749cdea0b4d750 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 14 Mar 2019 06:59:25 -0700 Subject: [PATCH 1/2] Fix Python 2 regression from #7366 using FileNotFoundError --- tests/python/pants_test/backend/python/BUILD | 3 ++- .../pants_test/backend/python/interpreter_selection_utils.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/python/pants_test/backend/python/BUILD b/tests/python/pants_test/backend/python/BUILD index 288c71860b8..eee01488e6b 100644 --- a/tests/python/pants_test/backend/python/BUILD +++ b/tests/python/pants_test/backend/python/BUILD @@ -72,6 +72,7 @@ python_library( name='interpreter_selection_utils', sources=['interpreter_selection_utils.py'], dependencies=[ - 'src/python/pants/util:process_handler' + 'src/python/pants/util:process_handler', + '3rdparty/python:future', ] ) diff --git a/tests/python/pants_test/backend/python/interpreter_selection_utils.py b/tests/python/pants_test/backend/python/interpreter_selection_utils.py index a5b21d0bb95..63d3e358434 100644 --- a/tests/python/pants_test/backend/python/interpreter_selection_utils.py +++ b/tests/python/pants_test/backend/python/interpreter_selection_utils.py @@ -7,6 +7,8 @@ import os from unittest import skipIf +from future.utils import PY2 + from pants.util.process_handler import subprocess @@ -46,6 +48,8 @@ def python_interpreter_path(version): :returns: the normalized path to the interpreter binary if found; otherwise `None` :rtype: string """ + if PY2: + FileNotFoundError = IOError try: command = ['python{}'.format(version), '-c', 'import sys; print(sys.executable)'] py_path = subprocess.check_output(command).decode('utf-8').strip() From 64de417993420a099d3eb994a3526b78b475a067 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 14 Mar 2019 07:13:36 -0700 Subject: [PATCH 2/2] Use conditional ternary Good suggestion Daniel! --- .../backend/python/interpreter_selection_utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/python/pants_test/backend/python/interpreter_selection_utils.py b/tests/python/pants_test/backend/python/interpreter_selection_utils.py index 63d3e358434..ecdfca7001d 100644 --- a/tests/python/pants_test/backend/python/interpreter_selection_utils.py +++ b/tests/python/pants_test/backend/python/interpreter_selection_utils.py @@ -7,7 +7,7 @@ import os from unittest import skipIf -from future.utils import PY2 +from future.utils import PY3 from pants.util.process_handler import subprocess @@ -48,13 +48,11 @@ def python_interpreter_path(version): :returns: the normalized path to the interpreter binary if found; otherwise `None` :rtype: string """ - if PY2: - FileNotFoundError = IOError try: command = ['python{}'.format(version), '-c', 'import sys; print(sys.executable)'] py_path = subprocess.check_output(command).decode('utf-8').strip() return os.path.realpath(py_path) - except (subprocess.CalledProcessError, FileNotFoundError): + except (subprocess.CalledProcessError, FileNotFoundError if PY3 else IOError): return None