From a266d9d65def005c7608ce9d09e8cdb88d35d35e Mon Sep 17 00:00:00 2001 From: Pavel Dikov Date: Sat, 20 Jul 2024 18:50:49 +0100 Subject: [PATCH 1/3] fix(tox_env.python): do not process absolute paths to interpreter as PythonSpec Fixes: #3191 --- docs/changelog/3191.bugfix.rst | 1 + src/tox/tox_env/python/api.py | 3 +++ tests/tox_env/python/test_python_api.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 docs/changelog/3191.bugfix.rst diff --git a/docs/changelog/3191.bugfix.rst b/docs/changelog/3191.bugfix.rst new file mode 100644 index 000000000..5d494ef82 --- /dev/null +++ b/docs/changelog/3191.bugfix.rst @@ -0,0 +1 @@ +``base_python`` now accepts absolute paths to interpreter executable - by :user:`paveldikov`. \ No newline at end of file diff --git a/src/tox/tox_env/python/api.py b/src/tox/tox_env/python/api.py index bb40ee583..01e79c9eb 100644 --- a/src/tox/tox_env/python/api.py +++ b/src/tox/tox_env/python/api.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +import os import re import sys from abc import ABC, abstractmethod @@ -164,6 +165,8 @@ def _validate_base_python( if env_base_python is not None: spec_name = PythonSpec.from_string_spec(env_base_python) for base_python in base_pythons: + if os.path.isabs(base_python): # noqa: PTH117 + return [base_python] spec_base = PythonSpec.from_string_spec(base_python) if any( getattr(spec_base, key) != getattr(spec_name, key) diff --git a/tests/tox_env/python/test_python_api.py b/tests/tox_env/python/test_python_api.py index 4489d5f45..6b3697cd1 100644 --- a/tests/tox_env/python/test_python_api.py +++ b/tests/tox_env/python/test_python_api.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import sys from typing import TYPE_CHECKING, Callable from unittest.mock import patch @@ -120,6 +121,24 @@ def test_base_python_env_no_conflict(env: str, base_python: list[str], ignore_co assert result is base_python +@pytest.mark.parametrize( + ("env", "base_python", "platform"), + [ + ("py312-unix", ["/opt/python312/bin/python"], "posix"), + ("py312-win", [r"C:\Program Files\Python312\python.exe"], "nt"), + ("py311-win", [r"\\a\python311\python.exe"], "nt"), + ("py310-win", [r"\\?\UNC\a\python310\python.exe"], "nt"), + ("py310", ["//a/python310/bin/python"], None), + ], + ids=lambda a: "|".join(a) if isinstance(a, list) else str(a), +) +def test_base_python_absolute(env: str, base_python: list[str], platform: str | None) -> None: + if platform and platform != os.name: + pytest.skip(f"Not applicable to this platform. ({platform} != {os.name})") + result = Python._validate_base_python(env, base_python, False) # noqa: SLF001 + assert result == base_python + + @pytest.mark.parametrize("ignore_conflict", [True, False]) @pytest.mark.parametrize( ("env", "base_python", "expected", "conflict"), From 0b6eb9ee32d160bc2de7211781752fbffc1f8068 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Jul 2024 17:59:20 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/changelog/3191.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog/3191.bugfix.rst b/docs/changelog/3191.bugfix.rst index 5d494ef82..60c954d26 100644 --- a/docs/changelog/3191.bugfix.rst +++ b/docs/changelog/3191.bugfix.rst @@ -1 +1 @@ -``base_python`` now accepts absolute paths to interpreter executable - by :user:`paveldikov`. \ No newline at end of file +``base_python`` now accepts absolute paths to interpreter executable - by :user:`paveldikov`. From eb43ff1e698e871db17ee353786087f57904581c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 7 Aug 2024 10:17:13 -0700 Subject: [PATCH 3/3] PR Feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bernát Gábor --- src/tox/tox_env/python/api.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tox/tox_env/python/api.py b/src/tox/tox_env/python/api.py index a618d6784..8d07bc6a0 100644 --- a/src/tox/tox_env/python/api.py +++ b/src/tox/tox_env/python/api.py @@ -3,10 +3,10 @@ from __future__ import annotations import logging -import os import re import sys from abc import ABC, abstractmethod +from pathlib import Path from typing import TYPE_CHECKING, Any, List, NamedTuple, cast from virtualenv.discovery.py_spec import PythonSpec @@ -15,7 +15,6 @@ from tox.tox_env.errors import Fail, Recreate, Skip if TYPE_CHECKING: - from pathlib import Path from tox.config.main import Config @@ -170,7 +169,7 @@ def _validate_base_python( if env_base_python is not None: spec_name = PythonSpec.from_string_spec(env_base_python) for base_python in base_pythons: - if os.path.isabs(base_python): # noqa: PTH117 + if Path(base_python).is_absolute(): return [base_python] spec_base = PythonSpec.from_string_spec(base_python) if any(