Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup the spec string for sys.executable #3327

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog/3327.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix and test the string spec for the ``sys.executable`` interpreter (introduced in :pull:`3325`)
- by :user:`hroncok`
11 changes: 9 additions & 2 deletions src/tox/tox_env/python/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def extract_base_python(cls, env_name: str) -> str | None:
return next(iter(candidates))
return None

@classmethod
hroncok marked this conversation as resolved.
Show resolved Hide resolved
def _python_spec_for_sys_executable(cls) -> PythonSpec:
implementation = sys.implementation.name
version = sys.version_info
bits = "64" if sys.maxsize > 2**32 else "32"
hroncok marked this conversation as resolved.
Show resolved Hide resolved
string_spec = f"{implementation}{version.major}{version.minor}-{bits}"
return PythonSpec.from_string_spec(string_spec)

@classmethod
def _validate_base_python(
cls,
Expand All @@ -172,8 +180,7 @@ def _validate_base_python(
if spec_base.path is not None:
path = Path(spec_base.path).absolute()
if str(spec_base.path) == sys.executable:
ver, is_64 = sys.version_info, sys.maxsize != 2**32
spec_base = PythonSpec.from_string_spec(f"{sys.implementation}{ver.major}{ver.minor}-{is_64}")
spec_base = cls._python_spec_for_sys_executable()
else:
spec_base = cls.python_spec_for_path(path)
if any(
Expand Down
27 changes: 27 additions & 0 deletions tests/tox_env/python/test_python_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import sys
from types import SimpleNamespace
from typing import TYPE_CHECKING, Callable
from unittest.mock import patch

Expand Down Expand Up @@ -289,3 +290,29 @@ def test_usedevelop_with_nonexistent_basepython(tox_project: ToxProjectCreator)
project = tox_project({"tox.ini": ini})
result = project.run()
assert result.code == 0


@pytest.mark.parametrize(
("impl", "major", "minor", "arch"),
[
("cpython", 3, 12, 64),
("pypy", 3, 9, 32),
],
)
def test_python_spec_for_sys_executable(impl: str, major: int, minor: int, arch: int, mocker: MockerFixture) -> None:
version_info = SimpleNamespace(major=major, minor=minor, micro=5, releaselevel="final", serial=0)
implementation = SimpleNamespace(
name=impl,
cache_tag=f"{impl}-{major}{minor}",
version=version_info,
hexversion=...,
_multiarch=...,
)
mocker.patch.object(sys, "version_info", version_info)
mocker.patch.object(sys, "implementation", implementation)
mocker.patch.object(sys, "maxsize", 2**arch // 2 - 1)
spec = Python._python_spec_for_sys_executable() # noqa: SLF001
assert spec.implementation == impl
assert spec.major == major
assert spec.minor == minor
assert spec.architecture == arch
Loading