Skip to content

Commit

Permalink
use shutil.which when finding a suitable python
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 7, 2023
1 parent 809e98c commit af8d76a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,14 @@ def create_venv(
):
continue

python = "python" + python_to_try

if self._io.is_debug():
self._io.write_error_line(f"<debug>Trying {python}</debug>")
self._io.write_error_line(
f"<debug>Trying python{python_to_try}</debug>"
)

python = shutil.which(f"python{python_to_try}")
if python is None:
continue

try:
python_patch = decode(
Expand All @@ -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 <c1>{python}</c1> ({python_patch})"
Expand Down
9 changes: 7 additions & 2 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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")),
Expand Down Expand Up @@ -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"]
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")),
Expand Down

0 comments on commit af8d76a

Please sign in to comment.