From af8d76a401e977792b42bc735d1c144b5008ba95 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Fri, 7 Apr 2023 00:48:42 +0100 Subject: [PATCH] use shutil.which when finding a suitable python --- src/poetry/utils/env.py | 13 +++++++------ tests/utils/test_env.py | 9 +++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index 2a1074c5b9e..b0f1a3c2410 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -970,10 +970,14 @@ def create_venv( ): continue - python = "python" + python_to_try - if self._io.is_debug(): - self._io.write_error_line(f"Trying {python}") + self._io.write_error_line( + f"Trying python{python_to_try}" + ) + + python = shutil.which(f"python{python_to_try}") + if python is None: + continue try: python_patch = decode( @@ -985,9 +989,6 @@ def create_venv( except CalledProcessError: continue - if not python_patch: - continue - if supported_python.allows(Version.parse(python_patch)): self._io.write_error_line( f"Using {python} ({python_patch})" diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index b41d22e6b53..0fd848661a3 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -197,7 +197,8 @@ def check_output(cmd: list[str], *args: Any, **kwargs: Any) -> str: elif "sys.version_info[:2]" in python_cmd: return f"{version.major}.{version.minor}" elif "import sys; print(sys.executable)" in python_cmd: - return f"/usr/bin/{cmd[0]}" + basename = os.path.basename(cmd[0]) + return f"/usr/bin/{basename}" else: assert "import sys; print(sys.prefix)" in python_cmd return str(Path("/prefix")) @@ -1070,6 +1071,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_generic_ poetry.package.python_versions = "^3.6" mocker.patch("sys.version_info", (2, 7, 16)) + mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}") mocker.patch( "subprocess.check_output", side_effect=check_output_wrapper(Version.parse("3.7.5")), @@ -1107,6 +1109,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific poetry.package.python_versions = "^3.6" mocker.patch("sys.version_info", (2, 7, 16)) + mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}") mocker.patch( "subprocess.check_output", side_effect=["3.5.3", "3.9.0", "/usr/bin/python3.9"] ) @@ -1546,13 +1549,14 @@ def test_create_venv_accepts_fallback_version_w_nonzero_patchlevel( def mock_check_output(cmd: str, *args: Any, **kwargs: Any) -> str: if GET_PYTHON_VERSION_ONELINER in cmd: - if "python3.5" in cmd: + if "python3.5" in cmd[0]: return "3.5.12" else: return "3.7.1" else: return "/usr/bin/python3.5" + mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}") check_output = mocker.patch( "subprocess.check_output", side_effect=mock_check_output, @@ -1662,6 +1666,7 @@ def test_create_venv_project_name_empty_sets_correct_prompt( venv_name = manager.generate_env_name("", str(poetry.file.parent)) mocker.patch("sys.version_info", (2, 7, 16)) + mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}") mocker.patch( "subprocess.check_output", side_effect=check_output_wrapper(Version.parse("3.7.5")),