diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cda4695..8a4d961 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: priority: 5 args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"] - repo: https://github.com/gaborbernat/simplint-pre-commit - rev: "f15525433ac9797aa1580f46fdadfe60dd77923c" # frozen: v0.8.0 + rev: "5f12b15e1199c42bdeb3e13ea1c8b45952fbd802" # frozen: v0.11.0 hooks: - id: simplint priority: 6 diff --git a/docs/changelog/+simplint.change.rst b/docs/changelog/+simplint.change.rst new file mode 100644 index 0000000..deb7466 --- /dev/null +++ b/docs/changelog/+simplint.change.rst @@ -0,0 +1 @@ +Restore ``MarkerEnvironment`` as a package-root export after its removal during simplint adoption. diff --git a/pyproject.toml b/pyproject.toml index 2c88e81..3ed83ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -133,12 +133,13 @@ lint.ignore = [ "missing-trailing-comma", # Conflict with formatter "multi-line-summary-first-line", # `multi-line-summary-first-line` and `multi-line-summary-second-line` are incompatible "single-line-implicit-string-concatenation", # Conflict with formatter - "undocumented-magic-method", # undocumented-public-api (simplint SMP903) covers the public API instead - "undocumented-public-class", # undocumented-public-api (simplint SMP903) covers the public API instead - "undocumented-public-function", # undocumented-public-api (simplint SMP903) covers the public API instead - "undocumented-public-init", # undocumented-public-api (simplint SMP903) covers the public API instead - "undocumented-public-method", # undocumented-public-api (simplint SMP903) covers the public API instead - "undocumented-public-nested-class", # undocumented-public-api (simplint SMP903) covers the public API instead + "undocumented-magic-method", # undocumented-public-api (simplint) covers the public API instead + "undocumented-public-class", # undocumented-public-api (simplint) covers the public API instead + "undocumented-public-function", # undocumented-public-api (simplint) covers the public API instead + "undocumented-public-init", # undocumented-public-api (simplint) covers the public API instead + "undocumented-public-method", # undocumented-public-api (simplint) covers the public API instead + "undocumented-public-nested-class", # undocumented-public-api (simplint) covers the public API instead + "unnecessary-regular-expression-compile", # conflicts with simplint inline-regex ] lint.per-file-ignores."docs/**/*.py" = [ "implicit-namespace-package", # no __init__.py in docs directory @@ -153,12 +154,9 @@ lint.per-file-ignores."src/python_discovery/_py_info_collect.py" = [ "ANN", # file must parse on Python 2.7 for the version gate: no annotations "missing-required-import", # `from __future__ import annotations` is a SyntaxError below 3.7 "module-import-not-at-top-of-file", # the version gate must run before importing sysconfig, absent on 3.0/3.1 - "PTH", # must use os.path — file runs as subprocess script with only stdlib + "PTH", # probe exchanges string paths with older interpreters "UP", # pyupgrade pushes syntax newer than the 2.7 parse floor, e.g. f-strings ] -lint.per-file-ignores."src/python_discovery/_windows/_pep514.py" = [ - "PTH", # os.path.exists is monkeypatched in tests; pathlib.Path.exists bypasses the mock -] lint.per-file-ignores."tasks/**/*.py" = [ "D", # release helper scripts, not API "implicit-namespace-package", # no __init__.py in tasks directory @@ -175,12 +173,12 @@ lint.per-file-ignores."tests/**/*.py" = [ "magic-value-comparison", # Magic value used in comparison "private-member-access", # private member access needed to test internals "pytest-fixture-autouse", # shared setup fixtures are meant to apply to every test - "subprocess-without-shell-equals-true", # `subprocess` call: check for execution of untrusted input - "suspicious-subprocess-import", # subprocess import + "subprocess-without-shell-equals-true", # tests launch known interpreters and test-created scripts + "suspicious-subprocess-import", # subprocess calls exercise interpreter discovery "too-many-arguments", # too many arguments (pytest fixtures) "too-many-positional-arguments", # too many positional arguments (pytest fixtures) ] -lint.per-file-ignores."tests/windows/winreg_mock_values.py" = [ +lint.per-file-ignores."tests/python_discovery/_windows/winreg_mock_values.py" = [ "undefined-name", # undefined name (winreg available only on Windows) ] lint.isort = { known-first-party = [ @@ -199,7 +197,7 @@ count = true max_supported_python = "3.15" [tool.ty] -src.exclude = [ "tests/windows/winreg_mock_values.py" ] +src.exclude = [ "tests/python_discovery/_windows/winreg_mock_values.py" ] [[tool.ty.overrides]] include = [ @@ -243,8 +241,12 @@ paths.source = [ ] report.fail_under = 100 report.omit = [ - "src/python_discovery/_windows/*", - "tests/windows/*", + "src/python_discovery/_windows/_cancel_io.py", + "src/python_discovery/_windows/_pep514.py", + "src/python_discovery/_windows/_propose.py", + "tests/python_discovery/_windows/conftest.py", + "tests/python_discovery/_windows/test_pep514.py", + "tests/python_discovery/_windows/winreg_mock_values.py", ] report.partial_branches = [ "assert any\\(", diff --git a/src/python_discovery/_cache.py b/src/python_discovery/_cache.py index 16a07b1..48f2166 100644 --- a/src/python_discovery/_cache.py +++ b/src/python_discovery/_cache.py @@ -231,10 +231,10 @@ def locked(self) -> Generator[None]: class NoOpContentStore(ContentStore): # Content store that does nothing -- implements ContentStore protocol. - def exists(self) -> bool: # ruff:ignore[no-self-use] + def exists(self) -> bool: # ruff:ignore[no-self-use] cache protocol return False - def read(self) -> dict | None: # ruff:ignore[no-self-use] keeps the cache interface uniform across implementations + def read(self) -> dict | None: # ruff:ignore[no-self-use] cache protocol return None def write(self, content: dict) -> None: @@ -244,13 +244,13 @@ def remove(self) -> None: pass @contextmanager - def locked(self) -> Generator[None]: # ruff:ignore[no-self-use] keeps the cache interface uniform across implementations + def locked(self) -> Generator[None]: # ruff:ignore[no-self-use] cache protocol yield class NoOpCache(PyInfoCache): # Cache that does nothing -- implements PyInfoCache protocol. - def py_info(self, path: Path) -> NoOpContentStore: # ruff:ignore[unused-method-argument, no-self-use] + def py_info(self, path: Path) -> NoOpContentStore: # ruff:ignore[unused-method-argument, no-self-use] protocol return NoOpContentStore() def py_info_clear(self) -> None: diff --git a/src/python_discovery/_cached_py_info.py b/src/python_discovery/_cached_py_info.py index 473fefe..47e59d5 100644 --- a/src/python_discovery/_cached_py_info.py +++ b/src/python_discovery/_cached_py_info.py @@ -18,7 +18,9 @@ from pathlib import Path from select import select from shlex import quote -from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, list2cmdline # ruff:ignore[suspicious-subprocess-import] list2cmdline only formats the command for logging + +# ruff:ignore[suspicious-subprocess-import] interpreter interrogation +from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, list2cmdline from threading import Event, Thread from time import monotonic from typing import TYPE_CHECKING, Final, NamedTuple @@ -28,7 +30,7 @@ from ._py_info_collect import MIN_INTERROGATE_VERSION, UNSUPPORTED_EXIT_CODE, UNSUPPORTED_MARKER if sys.platform == "win32": # pragma: win32 cover - from ._windows._cancel_io import stop_query_reader + from ._windows import stop_query_reader if TYPE_CHECKING: from collections.abc import Generator, Mapping @@ -207,7 +209,7 @@ def _run_subprocess( env["PYTHONUTF8"] = "1" _LOGGER.debug("get interpreter info via cmd: %s", cmd if isinstance(cmd, str) else LogCmd(cmd)) try: - process = Popen( # ruff:ignore[subprocess-without-shell-equals-true] argv list without a shell, so no shell injection + process = Popen( # ruff:ignore[subprocess-without-shell-equals-true] interrogate discovered interpreter cmd, universal_newlines=True, stdin=DEVNULL, @@ -460,5 +462,4 @@ def clear(cache: PyInfoCache) -> None: "LogCmd", "clear", "from_exe", - "gen_cookie", ] diff --git a/src/python_discovery/_discovery.py b/src/python_discovery/_discovery.py index a2667ac..6e58f7f 100644 --- a/src/python_discovery/_discovery.py +++ b/src/python_discovery/_discovery.py @@ -150,7 +150,7 @@ def _propose_current_and_windows( yield current_python, True if IS_WIN: # pragma: win32 cover - from ._windows._propose import propose_interpreters as win_propose # ruff:ignore[import-outside-top-level] the module is Windows-only and imported only there + from ._windows import propose_interpreters as win_propose # ruff:ignore[import-outside-top-level] Windows-only for interpreter in win_propose(spec, cache, env): if _is_new_exe(str(interpreter.executable), tested_exes): @@ -433,7 +433,7 @@ def _uv_specifier_matches(key: _UvKey, specifier: SimpleSpecifier) -> bool: precision = numeric_version.count(".") + 1 release = ".".join(str(part) for part in key.version.release[:precision]) if key.version.pre_type is not None and ( - precision == 3 or specifier.version.pre_type is not None # ruff:ignore[magic-value-comparison] 3 is the precision of a full version tuple + precision == 3 or specifier.version.pre_type is not None # ruff:ignore[magic-value-comparison] full version ): release = f"{release}{key.version.pre_type}{key.version.pre_num}" return specifier.contains(release) @@ -529,7 +529,7 @@ def iter_interpreters( yield from _iter_for_spec(spec_str, first_with, cache, env_map, predicate, seen) -def _iter_for_spec( # ruff:ignore[too-many-arguments, too-many-positional-arguments] each argument is a separate spec input +def _iter_for_spec( # ruff:ignore[too-many-arguments, too-many-positional-arguments] distinct spec inputs spec_str: str | None, try_first_with: tuple[str, ...], cache: PyInfoCache | None, @@ -576,11 +576,9 @@ class _UvInstall: __all__ = [ - "IS_WIN", "LazyPathDump", "get_interpreter", "get_paths", "iter_interpreters", - "path_exe_finder", "propose_interpreters", ] diff --git a/src/python_discovery/_py_info.py b/src/python_discovery/_py_info.py index cf05729..ba40d02 100644 --- a/src/python_discovery/_py_info.py +++ b/src/python_discovery/_py_info.py @@ -1,3 +1,4 @@ +# simplint: ignore[flattened-package] # ruff: ignore[commented-out-code] # keep the existing private path """Concrete Python interpreter information, collected in-process or via subprocess interrogation.""" from __future__ import annotations @@ -44,7 +45,7 @@ def _get_path_extensions() -> list[str]: EXTENSIONS: Final[list[str]] = _get_path_extensions() -class PythonInfo: # ruff:ignore[too-many-public-methods] PythonInfo is the public query surface; splitting it breaks callers +class PythonInfo: # ruff:ignore[too-many-public-methods] public query API """ Contains information for a Python interpreter. @@ -200,7 +201,7 @@ def from_exe( # ruff:ignore[too-many-arguments] each argument is a separate pro ) @classmethod - def _from_exe_with_args( # ruff:ignore[too-many-arguments] each argument is a separate probe input + def _from_exe_with_args( # ruff:ignore[too-many-arguments] distinct probe inputs cls, exe: str, cache: PyInfoCache | None = None, @@ -211,8 +212,7 @@ def _from_exe_with_args( # ruff:ignore[too-many-arguments] each argument is a s resolve_to_host: bool = True, env: Mapping[str, str] | None = None, ) -> PythonInfo | None: - # Interrogate an executable with optional platform-specific leading arguments. - from ._cached_py_info import from_exe # ruff:ignore[import-outside-top-level] + from ._cached_py_info import from_exe # ruff:ignore[import-outside-top-level] deferred import avoids a cycle env = os.environ if env is None else env proposed = from_exe( @@ -472,12 +472,13 @@ def clear_cache(cls, cache: PyInfoCache) -> None: :param cache: the cache store to clear. """ - from ._cached_py_info import clear # ruff:ignore[import-outside-top-level] deferred import avoids a cycle with _cached_py_info + from ._cached_py_info import clear # ruff:ignore[import-outside-top-level] import cycle clear(cache) cls._cache_exe_discovery.clear() - def satisfies(self, spec: PythonSpec, *, impl_must_match: bool) -> bool: # ruff:ignore[too-many-return-statements] one return per distinct satisfaction outcome + # ruff:ignore[too-many-return-statements] distinct outcomes + def satisfies(self, spec: PythonSpec, *, impl_must_match: bool) -> bool: """ Check if a given specification can be satisfied by this python interpreter instance. @@ -535,7 +536,8 @@ def _satisfies_version_specifier(self, spec: PythonSpec) -> bool: release = ".".join(str(c) for c in [version_info.major, version_info.minor, version_info.micro][:precision]) if ( version_info.releaselevel != "final" - and (precision == 3 or specifier.version.pre_type is not None) # ruff:ignore[magic-value-comparison] 3 is the precision of a full version tuple + # ruff:ignore[magic-value-comparison] full version has three parts + and (precision == 3 or specifier.version.pre_type is not None) and (suffix := {"alpha": "a", "beta": "b", "candidate": "rc"}.get(version_info.releaselevel)) ): release = f"{release}{suffix}{version_info.serial}" @@ -629,7 +631,7 @@ def _possible_base(self) -> Generator[str, None, None]: for base in possible_base: lower = base.lower() yield lower - from ._compat import fs_is_case_sensitive # ruff:ignore[import-outside-top-level] imported only when this path is walked + from ._compat import fs_is_case_sensitive # ruff:ignore[import-outside-top-level] cold path if fs_is_case_sensitive(): # pragma: no branch, the branch depends on the host filesystem if base != lower: @@ -780,9 +782,7 @@ def normalize_isa(isa: str) -> str: __all__ = [ - "EXTENSIONS", "KNOWN_ARCHITECTURES", - "InfoValue", "MarkerEnvironment", "PythonInfo", "VersionInfo", diff --git a/src/python_discovery/_py_info_collect.py b/src/python_discovery/_py_info_collect.py index 83fd59e..ca2a807 100644 --- a/src/python_discovery/_py_info_collect.py +++ b/src/python_discovery/_py_info_collect.py @@ -1,3 +1,4 @@ +# simplint: ignore[flattened-package] # ruff: ignore[commented-out-code] # keep the existing private path """ Collect interpreter information, also run as the subprocess interrogation script (stdlib only). @@ -246,10 +247,10 @@ def _distutils_install(): # pragma: <3.11 cover # 3.11+ uses the "venv" scheme warnings.simplefilter("ignore") try: # ruff:ignore[import-outside-top-level] distutils is optional and removed in newer Pythons - from distutils import dist # ty: ignore[unresolved-import] distutils is removed from the stdlib in Python 3.12 + from distutils import dist # ty: ignore[unresolved-import] removed in 3.12 # ruff:ignore[import-outside-top-level] distutils is optional and removed in newer Pythons - from distutils.command.install import SCHEME_KEYS # ty: ignore[unresolved-import] distutils is removed from the stdlib in Python 3.12 + from distutils.command.install import SCHEME_KEYS # ty: ignore[unresolved-import] removed in 3.12 except ImportError: # pragma: no cover # if removed or not installed ignore return {} @@ -257,7 +258,7 @@ def _distutils_install(): # pragma: <3.11 cover # 3.11+ uses the "venv" scheme "script_args": "--no-user-cfg", }) # conf files not parsed so they do not hijack paths if hasattr(sys, "_framework"): # pragma: no cover # macOS framework builds only - sys._framework = None # ty: ignore[invalid-assignment] # ruff:ignore[private-member-access] # disable macOS static paths for framework + sys._framework = None # ty: ignore[invalid-assignment] # ruff:ignore[private-member-access] macOS paths with warnings.catch_warnings(): # disable warning for PEP-632 warnings.simplefilter("ignore") @@ -311,7 +312,7 @@ def _get_tcl_tk_libs(): # pragma: no cover # tkinter availability varies; teste else: # Tk.__init__ reads profile scripts from HOME or the working directory; probing needs only library metadata. class _TclWithoutProfile(tk.Tk): - def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] # Tk override must be an instance method. + def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] Tk override return None try: @@ -324,17 +325,16 @@ def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] # Tk overr return tcl_lib, tk_lib @staticmethod - def _resolve_tk_lib(tcl, tcl_lib): # pragma: no cover, Tcl probing needs a Tk install the test runners lack + def _resolve_tk_lib(tcl, tcl_lib): # pragma: no cover # Tk is absent on test runners # Resolve the TK library path by direct query or path construction. tk_lib = PythonInfoCollector._query_tk_library(tcl) if tk_lib is not None: return tk_lib - import _tkinter # ruff:ignore[import-outside-top-level,import-private-name] # novermin # linked Tk version + import _tkinter # ruff:ignore[import-outside-top-level,import-private-name] # simplint: ignore[private-import-path] # novermin - # Loading Tk can block on DISPLAY; the linked version is enough to locate its library. - tk_version = _tkinter.TK_VERSION tcl_parent = os.path.dirname(tcl_lib) - for version in (tk_version, tk_version.split(".")[0]): + version_text = _tkinter.TK_VERSION + for version in (version_text, version_text.split(".", maxsplit=1)[0]): tk_lib_path = os.path.join(tcl_parent, "tk{}".format(version)) if os.path.isdir(tk_lib_path) and os.path.exists(os.path.join(tk_lib_path, "tk.tcl")): return tk_lib_path diff --git a/src/python_discovery/_py_spec.py b/src/python_discovery/_py_spec.py index 5532895..1f273f3 100644 --- a/src/python_discovery/_py_spec.py +++ b/src/python_discovery/_py_spec.py @@ -1,3 +1,4 @@ +# simplint: ignore[flattened-package] # ruff: ignore[commented-out-code] # keep the existing private path """A Python specification is an abstract requirement definition of an interpreter.""" from __future__ import annotations @@ -131,6 +132,9 @@ def _parse_specifier(string_spec: str) -> PythonSpec | None: ) +_GET_REQUIRED_PRECISION_PATTERN: Final[re.Pattern[str]] = re.compile(r"a|b|rc") + + class PythonSpec: """ Contains specification about a Python Interpreter. @@ -152,7 +156,7 @@ class PythonSpec: constraints, or ``None``. """ - def __init__( # ruff:ignore[too-many-arguments, too-many-positional-arguments] making the arguments keyword-only breaks positional callers + def __init__( # ruff:ignore[too-many-arguments, too-many-positional-arguments] positional callers self, str_spec: str, implementation: str | None, @@ -243,7 +247,7 @@ def generate_re(self, *, windows: bool, all_implementations: bool = False) -> re flags=re.IGNORECASE, ) - def satisfies(self, spec: PythonSpec) -> bool: # ruff:ignore[too-many-return-statements] one return per distinct satisfaction outcome + def satisfies(self, spec: PythonSpec) -> bool: # ruff:ignore[too-many-return-statements] distinct outcomes """ Check if this spec is compatible with the given *spec* (e.g. PEP-514 on Windows). @@ -310,7 +314,7 @@ def _get_required_precision(item: SimpleSpecifier) -> int | None: return None if item.wildcard_precision is not None: return item.wildcard_precision - return re.split(r"a|b|rc", item.version_str, maxsplit=1)[0].count(".") + 1 + return _GET_REQUIRED_PRECISION_PATTERN.split(item.version_str, maxsplit=1)[0].count(".") + 1 __all__ = [ diff --git a/src/python_discovery/_specifier.py b/src/python_discovery/_specifier.py index 312d588..cc34236 100644 --- a/src/python_discovery/_specifier.py +++ b/src/python_discovery/_specifier.py @@ -95,7 +95,7 @@ def __eq__(self, other: object) -> bool: def __hash__(self) -> int: return hash((self.release, self.pre_type, self.pre_num)) - def __lt__(self, other: object) -> bool: # ruff:ignore[too-many-return-statements] one return per distinct comparison outcome + def __lt__(self, other: object) -> bool: # ruff:ignore[too-many-return-statements] distinct outcomes if not isinstance(other, SimpleVersion): return NotImplemented if self.release != other.release: @@ -207,34 +207,30 @@ def contains(self, version_str: str) -> bool: candidate = SimpleVersion.from_string(version_str) if isinstance(version_str, str) else version_str except ValueError: return False - if self.version is None: + if (version := self.version) is None: return False if self.is_wildcard: - return self._check_wildcard(candidate) - return self._check_standard(candidate) + return self._check_wildcard(candidate, version) + return self._check_standard(candidate, version) - def _check_wildcard(self, candidate: SimpleVersion) -> bool: - if self.version is None: # pragma: no branch, defensive guard; the constructors always set the version - return False # pragma: no cover, defensive guard; the constructors always set the version + def _check_wildcard(self, candidate: SimpleVersion, version: SimpleVersion) -> bool: if self.operator == "==": - return candidate.release[: self.wildcard_precision] == self.version.release[: self.wildcard_precision] + return candidate.release[: self.wildcard_precision] == version.release[: self.wildcard_precision] if self.operator == "!=": - return candidate.release[: self.wildcard_precision] != self.version.release[: self.wildcard_precision] + return candidate.release[: self.wildcard_precision] != version.release[: self.wildcard_precision] return False - def _check_standard(self, candidate: SimpleVersion) -> bool: - if self.version is None: # pragma: no branch, defensive guard; the constructors always set the version - return False # pragma: no cover, defensive guard; the constructors always set the version + def _check_standard(self, candidate: SimpleVersion, version: SimpleVersion) -> bool: if self.operator == "===": - return str(candidate) == str(self.version) + return str(candidate) == str(version) if self.operator == "~=": - return self._check_compatible_release(candidate) + return self._check_compatible_release(candidate, version) # PEP 440 excludes prereleases of a final release named by an exclusive upper bound. if ( self.operator == "<" and candidate.pre_type is not None - and self.version.pre_type is None - and candidate.release == self.version.release + and version.pre_type is None + and candidate.release == version.release ): return False cmp_ops = { @@ -246,18 +242,16 @@ def _check_standard(self, candidate: SimpleVersion) -> bool: ">=": operator.ge, } if self.operator in cmp_ops: - return cmp_ops[self.operator](candidate, self.version) + return cmp_ops[self.operator](candidate, version) return False - def _check_compatible_release(self, candidate: SimpleVersion) -> bool: - if self.version is None: - return False - if candidate < self.version: + def _check_compatible_release(self, candidate: SimpleVersion, version: SimpleVersion) -> bool: + if candidate < version: return False # ~=X.Y is >=X.Y, ==X.*: compare all but the last part the spec wrote (release is padded to three). # A bare ~=X still compares its one part, so it stays ==X.*. prefix = max(self.version_str.count("."), 1) - return candidate.release[:prefix] == self.version.release[:prefix] + return candidate.release[:prefix] == version.release[:prefix] @dataclass(**DC_KW) diff --git a/src/python_discovery/_windows/__init__.py b/src/python_discovery/_windows/__init__.py index 177916a..83cc279 100644 --- a/src/python_discovery/_windows/__init__.py +++ b/src/python_discovery/_windows/__init__.py @@ -1 +1,11 @@ """Windows-specific Python discovery via PEP 514 registry entries.""" + +from __future__ import annotations + +from ._cancel_io import stop_query_reader +from ._propose import propose_interpreters + +__all__ = [ + "propose_interpreters", + "stop_query_reader", +] diff --git a/src/python_discovery/_windows/_cancel_io.py b/src/python_discovery/_windows/_cancel_io.py index 499a9cf..9a9b6f8 100644 --- a/src/python_discovery/_windows/_cancel_io.py +++ b/src/python_discovery/_windows/_cancel_io.py @@ -2,10 +2,13 @@ from __future__ import annotations -from ctypes import WinDLL, WinError, get_last_error, wintypes # ty: ignore[unresolved-import] # Windows-only APIs +import sys from time import monotonic from typing import TYPE_CHECKING, Final +if sys.platform == "win32": + from ctypes import WinDLL, WinError, get_last_error, wintypes + if TYPE_CHECKING: from threading import Thread diff --git a/src/python_discovery/_windows/_pep514.py b/src/python_discovery/_windows/_pep514.py index 2f23e52..99e5b05 100644 --- a/src/python_discovery/_windows/_pep514.py +++ b/src/python_discovery/_windows/_pep514.py @@ -3,12 +3,11 @@ from __future__ import annotations import logging -import os import re import sys import winreg from logging import basicConfig, getLogger -from pathlib import PureWindowsPath +from pathlib import Path, PureWindowsPath from typing import TYPE_CHECKING, Any, Final, NamedTuple if TYPE_CHECKING: @@ -46,19 +45,19 @@ ) -def _enum_keys(key: Any) -> Generator[str, None, None]: # ruff:ignore[any-type] winreg handles have no importable type on other platforms +def _enum_keys(key: Any) -> Generator[str, None, None]: # ruff:ignore[any-type] Windows-only handle at = 0 while True: try: - yield winreg.EnumKey(key, at) # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + yield winreg.EnumKey(key, at) # ty: ignore[unresolved-attribute] Windows-only except OSError: break at += 1 -def _get_value(key: Any, value_name: str | None) -> Any: # ruff:ignore[any-type] winreg handles have no importable type on other platforms +def _get_value(key: Any, value_name: str | None) -> Any: # ruff:ignore[any-type] registry value try: - return winreg.QueryValueEx(key, value_name)[0] # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + return winreg.QueryValueEx(key, value_name)[0] # ty: ignore[unresolved-attribute] Windows-only except OSError: return None @@ -73,11 +72,14 @@ class _RegistrySpec(NamedTuple): args: str | None -def _discover_pythons() -> Generator[_RegistrySpec, None, None]: +def discover_pythons() -> Generator[_RegistrySpec, None, None]: for hive, hive_name, key, flags, default_arch in [ - (winreg.HKEY_CURRENT_USER, "HKEY_CURRENT_USER", r"Software\Python", 0, 64), # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - (winreg.HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE", r"Software\Python", winreg.KEY_WOW64_64KEY, 64), # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - (winreg.HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE", r"Software\Python", winreg.KEY_WOW64_32KEY, 32), # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + # ty: ignore[unresolved-attribute] winreg constants exist only on Windows + (winreg.HKEY_CURRENT_USER, "HKEY_CURRENT_USER", r"Software\Python", 0, 64), + # ty: ignore[unresolved-attribute] winreg constants exist only on Windows + (winreg.HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE", r"Software\Python", winreg.KEY_WOW64_64KEY, 64), + # ty: ignore[unresolved-attribute] winreg constants exist only on Windows + (winreg.HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE", r"Software\Python", winreg.KEY_WOW64_32KEY, 32), ]: yield from _process_set(hive, hive_name, key, flags, default_arch) @@ -90,7 +92,8 @@ def _process_set( default_arch: int, ) -> Generator[_RegistrySpec, None, None]: try: - with winreg.OpenKeyEx(hive, key, 0, winreg.KEY_READ | flags) as root_key: # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + # ty: ignore[unresolved-attribute] winreg is Windows-only + with winreg.OpenKeyEx(hive, key, 0, winreg.KEY_READ | flags) as root_key: for company in _enum_keys(root_key): if company == "PyLauncher": # reserved continue @@ -102,17 +105,18 @@ def _process_set( def _process_company( hive_name: str, company: str, - root_key: Any, # ruff:ignore[any-type] winreg handles have no importable type on other platforms + root_key: Any, # ruff:ignore[any-type] Windows-only handle default_arch: int, ) -> Generator[_RegistrySpec, None, None]: - with winreg.OpenKeyEx(root_key, company) as company_key: # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + with winreg.OpenKeyEx(root_key, company) as company_key: # ty: ignore[unresolved-attribute] Windows-only for tag in _enum_keys(company_key): if (spec := _process_tag(hive_name, company, company_key, tag, default_arch)) is not None: yield spec -def _process_tag(hive_name: str, company: str, company_key: Any, tag: str, default_arch: int) -> _RegistrySpec | None: # ruff:ignore[any-type] winreg handles have no importable type on other platforms - with winreg.OpenKeyEx(company_key, tag) as tag_key: # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it +# ruff:ignore[any-type] winreg handles have no portable type +def _process_tag(hive_name: str, company: str, company_key: Any, tag: str, default_arch: int) -> _RegistrySpec | None: + with winreg.OpenKeyEx(company_key, tag) as tag_key: # ty: ignore[unresolved-attribute] Windows-only for candidate, key_path in [ (_get_value(tag_key, "SysVersion"), f"{hive_name}/{company}/{tag}/SysVersion"), (tag, f"{hive_name}/{company}/{tag}"), @@ -145,11 +149,13 @@ def _process_tag(hive_name: str, company: str, company_key: Any, tag: str, defau return _RegistrySpec(company, version.major, version.minor, arch, threaded, exe, args) -def _load_exe(hive_name: str, company: str, company_key: Any, tag: str) -> tuple[str, str | None] | None: # ruff:ignore[any-type] winreg handles have no importable type on other platforms +# ruff:ignore[any-type] winreg handles have no portable type +def _load_exe(hive_name: str, company: str, company_key: Any, tag: str) -> tuple[str, str | None] | None: key_path = f"{hive_name}/{company}/{tag}" try: - with winreg.OpenKeyEx(company_key, rf"{tag}\InstallPath") as ip_key, ip_key: # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - if (exe := _resolve_exe(ip_key, key_path)) is not None and os.path.exists(exe): + # ty: ignore[unresolved-attribute] winreg is Windows-only + with winreg.OpenKeyEx(company_key, rf"{tag}\InstallPath") as ip_key, ip_key: + if (exe := _resolve_exe(ip_key, key_path)) is not None and Path(exe).exists(): if (args := _get_value(ip_key, "ExecutableArguments")) is None or isinstance(args, str): return exe, args if args and args.strip() else None _msg(f"{key_path}/ExecutableArguments", f"expected a string, got {type(args).__name__}") @@ -160,20 +166,20 @@ def _load_exe(hive_name: str, company: str, company_key: Any, tag: str) -> tuple return None -def _resolve_exe(ip_key: Any, key_path: str) -> str | None: # ruff:ignore[any-type] winreg handles have no importable type on other platforms +def _resolve_exe(ip_key: Any, key_path: str) -> str | None: # ruff:ignore[any-type] Windows-only handle if (exe := _get_value(ip_key, "ExecutablePath")) is None: if (exe := _get_value(ip_key, None)) is None: _msg(key_path, "no ExecutablePath or default for it") return None if isinstance(exe, str): - exe = os.path.join(exe, "python.exe") + exe = str(PureWindowsPath(exe) / "python.exe") if not isinstance(exe, str) or not PureWindowsPath(exe).is_absolute(): _msg(key_path, f"expected an absolute executable path, got {exe!r}") return None return exe -def _parse_arch(arch_str: Any) -> int: # ruff:ignore[any-type] winreg handles have no importable type on other platforms +def _parse_arch(arch_str: Any) -> int: # ruff:ignore[any-type] untyped registry value if isinstance(arch_str, str): if match := _ARCH_RE.match(arch_str): return int(next(iter(match.groups()))) @@ -189,7 +195,7 @@ class _Version(NamedTuple): micro: int | None -def _parse_version(version_str: Any) -> _Version: # ruff:ignore[any-type] winreg handles have no importable type on other platforms +def _parse_version(version_str: Any) -> _Version: # ruff:ignore[any-type] untyped registry value if isinstance(version_str, str): if match := _VERSION_RE.match(version_str): major, minor, micro = match["major"], match["minor"], match["micro"] @@ -210,9 +216,14 @@ def _msg(path: str, what: str | Exception) -> None: def _run() -> None: basicConfig() - sys.stdout.write("\n".join(sorted([repr(tuple(spec)) for spec in _discover_pythons()]))) + sys.stdout.write("\n".join(sorted([repr(tuple(spec)) for spec in discover_pythons()]))) sys.stdout.write("\n") +__all__ = [ + "discover_pythons", +] + + if __name__ == "__main__": _run() diff --git a/src/python_discovery/_windows/_propose.py b/src/python_discovery/_windows/_propose.py index b150b21..a116a31 100644 --- a/src/python_discovery/_windows/_propose.py +++ b/src/python_discovery/_windows/_propose.py @@ -2,15 +2,14 @@ from typing import TYPE_CHECKING -from python_discovery._py_info import PythonInfo -from python_discovery._py_spec import PythonSpec +from python_discovery import PythonInfo, PythonSpec -from ._pep514 import _discover_pythons +from ._pep514 import discover_pythons if TYPE_CHECKING: from collections.abc import Generator, Mapping - from python_discovery._cache import PyInfoCache + from python_discovery import PyInfoCache def propose_interpreters( @@ -18,7 +17,7 @@ def propose_interpreters( cache: PyInfoCache | None, env: Mapping[str, str], ) -> Generator[PythonInfo, None, None]: - existing = list(_discover_pythons()) + existing = list(discover_pythons()) existing.sort( key=lambda item: ( *tuple(-1 if j is None else j for j in item[1:4]), @@ -37,7 +36,7 @@ def propose_interpreters( skip_pre_filter = implementation.lower() != "cpython" registry_spec = PythonSpec("", implementation, major, minor, None, arch, exe, free_threaded=threaded) if skip_pre_filter or registry_spec.satisfies(spec): - interpreter = _Pep514PythonInfo._from_exe_with_args( # ruff:ignore[private-member-access] same package; the Windows proposer builds the same info object + interpreter = _Pep514PythonInfo._from_exe_with_args( # ruff:ignore[private-member-access] PEP 514 arguments exe, cache, env=env, @@ -48,9 +47,7 @@ def propose_interpreters( yield interpreter -class _Pep514PythonInfo(PythonInfo): - # A Python information acquired from PEP-514. - ... +class _Pep514PythonInfo(PythonInfo): ... __all__ = [ diff --git a/tests/py_info/test_py_info.py b/tests/py_info/test_py_info.py deleted file mode 100644 index af6e9df..0000000 --- a/tests/py_info/test_py_info.py +++ /dev/null @@ -1,581 +0,0 @@ -from __future__ import annotations - -import copy -import itertools -import json -import logging -import os -import sys -import sysconfig -from pathlib import Path -from textwrap import dedent -from typing import TYPE_CHECKING, Final, NamedTuple - -import pytest -from packaging.markers import default_environment -from setuptools.dist import Distribution - -from python_discovery import DiskCache, MarkerEnvironment, PythonInfo, PythonSpec -from python_discovery import _cached_py_info as cached_py_info -from python_discovery._py_info import VersionInfo - -if TYPE_CHECKING: - from packaging.markers import Environment - from pytest_mock import MockerFixture - - from python_discovery import PyInfoCache - -_IS_PYPY: Final[bool] = PythonInfo.current_system().implementation == "PyPy" - -_CURRENT: Final[PythonInfo] = PythonInfo.current_system() -_EXECUTABLE_SUFFIX: Final[str] = Path(_CURRENT.executable).suffix if sys.platform == "win32" else "" -_ORIGINAL_SUFFIX: Final[str] = Path(_CURRENT.original_executable).suffix if sys.platform == "win32" else "" -_THREADED: Final[str] = "t" if _CURRENT.free_threaded else "" -_UNTHREADED: Final[str] = "" if _CURRENT.free_threaded else "t" -_OTHER_ARCHITECTURE: Final[int] = {32: 64, 64: 32}[_CURRENT.architecture] - - -@pytest.mark.graalpy -def test_current_as_json() -> None: - parsed = json.loads(_CURRENT.to_json()) - major, minor, micro, releaselevel, serial = sys.version_info - free_threaded = sysconfig.get_config_var("Py_GIL_DISABLED") == 1 - assert parsed["version_info"] == { - "major": major, - "minor": minor, - "micro": micro, - "releaselevel": releaselevel, - "serial": serial, - } - assert parsed["free_threaded"] is free_threaded - assert parsed["debug_build"] is bool(sysconfig.get_config_var("Py_DEBUG")) - - -def test_bad_exe_py_info_raise(tmp_path: Path, session_cache: DiskCache) -> None: - exe = str(tmp_path) - with pytest.raises(RuntimeError) as context: - PythonInfo.from_exe(exe, session_cache) - msg = str(context.value) - assert "code" in msg - assert exe in msg - - -def test_bad_exe_py_info_no_raise( - tmp_path: Path, - caplog: pytest.LogCaptureFixture, - capsys: pytest.CaptureFixture[str], - session_cache: DiskCache, -) -> None: - caplog.set_level(logging.NOTSET) - exe = str(tmp_path) - result = PythonInfo.from_exe(exe, session_cache, raise_on_error=False) - assert result is None - out, _ = capsys.readouterr() - assert not out - messages = [r.message for r in caplog.records if r.name != "filelock"] - assert len(messages) == 4 - assert "get interpreter info via cmd: " in messages[0] - assert "retrying" in messages[1] - assert "get interpreter info via cmd: " in messages[2] - assert str(exe) in messages[3] - assert "code" in messages[3] - - -@pytest.mark.parametrize( - "spec", - [sys.executable] - + [ - f"{impl}{'.'.join(str(i) for i in ver)}{'t' if _CURRENT.free_threaded else ''}{arch}" - for impl, ver, arch in itertools.product( - ( - [_CURRENT.implementation] - + (["python"] if _CURRENT.implementation == "CPython" else []) - + ( - [_CURRENT.implementation.lower()] - if _CURRENT.implementation != _CURRENT.implementation.lower() - else [] - ) - ), - [sys.version_info[0 : i + 1] for i in range(3)], - ["", f"-{_CURRENT.architecture}"], - ) - ], -) -def test_satisfy_py_info(spec: str) -> None: - matches = _CURRENT.satisfies(PythonSpec.from_string_spec(spec), impl_must_match=True) - assert matches is True - - -def test_satisfy_not_arch() -> None: - parsed_spec = PythonSpec.from_string_spec( - f"{_CURRENT.implementation}-{_OTHER_ARCHITECTURE}", - ) - matches = _CURRENT.satisfies(parsed_spec, impl_must_match=True) - assert matches is False - - -def test_satisfy_not_threaded() -> None: - parsed_spec = PythonSpec.from_string_spec( - f"{_CURRENT.implementation}{_CURRENT.version_info.major}{_UNTHREADED}", - ) - matches = _CURRENT.satisfies(parsed_spec, impl_must_match=True) - assert matches is False - - -def _generate_not_match_current_interpreter_version() -> list[str]: - result: list[str] = [] - for depth in range(3): - ver: list[int] = [int(part) for part in sys.version_info[0 : depth + 1]] - for idx in range(len(ver)): - for offset in [-1, 1]: - temp = ver.copy() - temp[idx] += offset - result.append(".".join(str(part) for part in temp)) - return result - - -_NON_MATCH_VER: Final[list[str]] = _generate_not_match_current_interpreter_version() - - -@pytest.mark.parametrize("spec", _NON_MATCH_VER) -def test_satisfy_not_version(spec: str) -> None: - matches = _CURRENT.satisfies(PythonSpec.from_string_spec(f"{_CURRENT.implementation}{spec}"), impl_must_match=True) - assert matches is False - - -def test_py_info_cached_error(mocker: MockerFixture, tmp_path: Path, session_cache: DiskCache) -> None: - spy = mocker.spy(cached_py_info, "_run_subprocess") - with pytest.raises(RuntimeError): - PythonInfo.from_exe(str(tmp_path), session_cache) - with pytest.raises(RuntimeError): - PythonInfo.from_exe(str(tmp_path), session_cache) - assert spy.call_count == 4 - - -def test_py_info_cache_clear(mocker: MockerFixture, session_cache: DiskCache) -> None: - result = PythonInfo.from_exe(sys.executable, session_cache) - assert result is not None - - PythonInfo.clear_cache(session_cache) - assert not cached_py_info._CACHE - - spy = mocker.spy(cached_py_info, "_run_subprocess") - info = PythonInfo.from_exe(sys.executable, session_cache) - assert info is not None - - native_difference = int(info.system_executable == info.executable) - assert spy.call_count + native_difference >= 1 - - -class PyInfoMock(NamedTuple): - implementation: str - architecture: int - version_info: VersionInfo - - -@pytest.mark.parametrize( - ("target", "position", "discovered"), - [ - pytest.param( - PyInfoMock("CPython", 64, VersionInfo(3, 6, 8, "final", 0)), - 0, - [ - PyInfoMock("CPython", 64, VersionInfo(3, 6, 9, "final", 0)), - PyInfoMock("PyPy", 64, VersionInfo(3, 6, 8, "final", 0)), - ], - id="pypy-candidate-3.6.8", - ), - pytest.param( - PyInfoMock("CPython", 64, VersionInfo(3, 6, 8, "final", 0)), - 0, - [ - PyInfoMock("CPython", 64, VersionInfo(3, 6, 9, "final", 0)), - PyInfoMock("CPython", 32, VersionInfo(3, 6, 9, "final", 0)), - ], - id="cpython-32bit-candidate-3.6.9", - ), - pytest.param( - PyInfoMock("CPython", 64, VersionInfo(3, 8, 1, "final", 0)), - 0, - [ - PyInfoMock("CPython", 32, VersionInfo(2, 7, 12, "rc", 2)), - PyInfoMock("PyPy", 64, VersionInfo(3, 8, 1, "final", 0)), - ], - id="pypy-candidate-3.8.1", - ), - ], -) -def test_system_executable_no_exact_match( - target: PyInfoMock, - discovered: list[PyInfoMock], - position: int, - *, - tmp_path: Path, - mocker: MockerFixture, - caplog: pytest.LogCaptureFixture, - session_cache: DiskCache, -) -> None: - caplog.set_level(logging.DEBUG) - - def _make_py_info(of: PyInfoMock) -> PythonInfo: - base = copy.deepcopy(_CURRENT) - base.implementation = of.implementation - base.version_info = of.version_info - base.architecture = of.architecture - return base - - discovered_with_path: dict[str, PythonInfo] = {} - names: list[str] = [] - for pos, info in enumerate(discovered): - path = tmp_path / str(pos) - path.write_text("", encoding="utf-8") - py_info = _make_py_info(info) - system_executable = _CURRENT.system_executable - assert system_executable is not None - py_info.system_executable = system_executable - py_info.executable = system_executable - py_info.base_executable = str(path) # ty: ignore[unresolved-attribute] the test stub is a plain object, not a PythonInfo - discovered_with_path[str(path)] = py_info - names.append(path.name) - - target_py_info = _make_py_info(target) - mocker.patch.object(target_py_info, "_find_possible_exe_names", return_value=names, autospec=True) - mocker.patch.object(target_py_info, "_find_possible_folders", return_value=[str(tmp_path)], autospec=True) - - def func(exe_path: str, _cache: PyInfoCache | None = None, **_kwargs: object) -> PythonInfo: - return discovered_with_path[exe_path] - - mocker.patch.object(target_py_info, "from_exe", side_effect=func, autospec=True) - target_py_info.real_prefix = str(tmp_path) - - target_py_info.system_executable = None - target_py_info.executable = str(tmp_path) - mapped = target_py_info.resolve_to_system(session_cache, target_py_info) - assert mapped.system_executable == _CURRENT.system_executable - found = discovered_with_path[mapped.base_executable] - assert found is discovered_with_path[str(tmp_path / str(position))] - - assert caplog.records[0].msg == "discover exe for %s in %s" - for record in caplog.records[1:-1]: - assert record.message.startswith("refused interpreter ") - assert record.levelno == logging.DEBUG - - warn_similar = caplog.records[-1] - assert warn_similar.levelno == logging.DEBUG - assert warn_similar.msg.startswith("no exact match found, chosen most similar") - - -@pytest.mark.parametrize("attr", ["free_threaded", "debug_build"]) -def test_discover_exe_validates_abi_flag( - attr: str, tmp_path: Path, mocker: MockerFixture, session_cache: DiskCache -) -> None: - def candidate(*, flag: bool, name: str) -> PythonInfo: - info = copy.deepcopy(_CURRENT) - setattr(info, attr, flag) - exe = tmp_path / name - exe.write_text("", encoding="utf-8") - info.executable = str(exe) - return info - - target = candidate(flag=True, name="target") - mismatch = candidate(flag=False, name="release") - match = candidate(flag=True, name="debug") - by_path = {info.executable: info for info in (mismatch, match)} - - mocker.patch.object(target, "_find_possible_exe_names", return_value=["release", "debug"], autospec=True) - mocker.patch.object(target, "_find_possible_folders", return_value=[str(tmp_path)], autospec=True) - mocker.patch.object(target, "from_exe", side_effect=lambda exe, *_a, **_k: by_path[exe], autospec=True) - - found = target.discover_exe(session_cache, prefix=str(tmp_path), exact=True) - assert found is match # the release build is refused even though version, impl and arch match - - -def test_py_info_ignores_distutils_config(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: - raw = f""" - [install] - prefix={tmp_path}{os.sep}prefix - install_purelib={tmp_path}{os.sep}purelib - install_platlib={tmp_path}{os.sep}platlib - install_headers={tmp_path}{os.sep}headers - install_scripts={tmp_path}{os.sep}scripts - install_data={tmp_path}{os.sep}data - """ - (tmp_path / "setup.cfg").write_text(dedent(raw), encoding="utf-8") - monkeypatch.chdir(tmp_path) - py_info = PythonInfo.from_exe(sys.executable) - assert py_info is not None - distutils = py_info.distutils_install - for key, value in distutils.items(): # pragma: no cover # distutils_install is empty with "venv" scheme - assert not value.startswith(str(tmp_path)), f"{key}={value}" - - -def test_discover_exe_on_path_non_spec_name_match(mocker: MockerFixture) -> None: - suffixed_name = f"python{_CURRENT.version_info.major}.{_CURRENT.version_info.minor}m{_EXECUTABLE_SUFFIX}" - spec = PythonSpec.from_string_spec(suffixed_name) - mocker.patch.object(_CURRENT, "executable", str(Path(_CURRENT.executable).parent / suffixed_name)) - assert _CURRENT.satisfies(spec, impl_must_match=True) is True - - -def test_satisfies_debug_spec_requires_debug_build(mocker: MockerFixture) -> None: - spec = PythonSpec.from_string_spec(f"python{_CURRENT.version_info.major}.{_CURRENT.version_info.minor}-dbg") - assert spec.debug is True - mocker.patch.object(_CURRENT, "free_threaded", spec.free_threaded) - - mocker.patch.object(_CURRENT, "debug_build", False) - assert _CURRENT.satisfies(spec, impl_must_match=True) is False - - mocker.patch.object(_CURRENT, "debug_build", True) - assert _CURRENT.satisfies(spec, impl_must_match=True) is True - - -def test_discover_exe_on_path_non_spec_name_not_match(mocker: MockerFixture) -> None: - suffixed_name = f"python{_CURRENT.version_info.major}.{_CURRENT.version_info.minor}m{_ORIGINAL_SUFFIX}" - spec = PythonSpec.from_string_spec(suffixed_name) - mocker.patch.object( - _CURRENT, - "original_executable", - str(Path(_CURRENT.executable).parent / f"e{suffixed_name}"), - ) - assert _CURRENT.satisfies(spec, impl_must_match=True) is False - - -@pytest.mark.skipif(_IS_PYPY, reason="setuptools distutils patching does not work") -def test_py_info_setuptools() -> None: - assert Distribution - PythonInfo() - - -@pytest.mark.usefixtures("_skip_if_test_in_system") -def test_py_info_to_system_raises( # pragma: no cover # skipped in venv environments - session_cache: DiskCache, - mocker: MockerFixture, - caplog: pytest.LogCaptureFixture, -) -> None: - caplog.set_level(logging.DEBUG) - mocker.patch.object(PythonInfo, "_find_possible_folders", return_value=[], autospec=True) - result = PythonInfo.from_exe(sys.executable, cache=session_cache, raise_on_error=False) - assert result is None - log = caplog.records[-1] - assert log.levelno == logging.INFO - expected = f"ignore {sys.executable} due cannot resolve system due to RuntimeError('failed to detect " - assert expected in log.message - - -def test_sysconfig_vars_include_shared_lib_keys() -> None: - for key in ("Py_ENABLE_SHARED", "INSTSONAME", "LIBDIR"): - assert key in _CURRENT.sysconfig_vars - - -def test_py_info_has_sysconfig_platform() -> None: - assert hasattr(_CURRENT, "sysconfig_platform") - assert _CURRENT.sysconfig_platform is not None - assert isinstance(_CURRENT.sysconfig_platform, str) - assert len(_CURRENT.sysconfig_platform) > 0 - - -def test_py_info_machine_property() -> None: - machine = _CURRENT.machine - assert machine is not None - assert isinstance(machine, str) - assert len(machine) > 0 - assert machine == machine.lower(), f"machine value should be lowercase: {machine}" - - -def test_py_info_machine_in_spec() -> None: - spec = _CURRENT.spec - assert _CURRENT.machine in spec - assert f"-{_CURRENT.architecture}-{_CURRENT.machine}" in spec - - -def test_py_info_sysconfig_platform_matches_sysconfig() -> None: - assert _CURRENT.sysconfig_platform == sysconfig.get_platform() - - -@pytest.mark.parametrize( - ("platform", "expected"), - [ - pytest.param("win32", "x86", id="win32"), - pytest.param("win-amd64", "x86_64", id="win-amd64"), - pytest.param("win-arm64", "arm64", id="win-arm64"), - pytest.param("linux-x86_64", "x86_64", id="linux-x86_64"), - pytest.param("linux-aarch64", "arm64", id="linux-aarch64"), - pytest.param("linux-riscv64", "riscv64", id="linux-riscv64"), - pytest.param("linux-ppc64le", "ppc64le", id="linux-ppc64le"), - pytest.param("linux-s390x", "s390x", id="linux-s390x"), - pytest.param("macosx-14.0-arm64", "arm64", id="macos-arm64"), - pytest.param("macosx-14.0-x86_64", "x86_64", id="macos-x86_64"), - ], -) -def test_py_info_machine_derivation(platform: str, expected: str) -> None: - info = copy.deepcopy(_CURRENT) - info.sysconfig_platform = platform - assert info.machine == expected - - -@pytest.mark.parametrize("runtime_isa", ["arm64", "x86_64"]) -def test_py_info_machine_derivation_universal2(mocker: MockerFixture, runtime_isa: str) -> None: - info = copy.deepcopy(_CURRENT) - info.sysconfig_platform = "macosx-11.0-universal2" - mocker.patch("python_discovery._py_info.platform.machine", return_value=runtime_isa, autospec=True) - assert info.machine == runtime_isa - - -def test_py_info_satisfies_with_machine() -> None: - threaded = _THREADED - spec_str = ( - f"{_CURRENT.implementation}{_CURRENT.version_info.major}{threaded}-{_CURRENT.architecture}-{_CURRENT.machine}" - ) - parsed_spec = PythonSpec.from_string_spec(spec_str) - assert _CURRENT.satisfies(parsed_spec, impl_must_match=True) is True - - -def test_py_info_satisfies_not_machine() -> None: - other_machine = {"arm64": "x86_64"}.get(_CURRENT.machine, "arm64") - parsed_spec = PythonSpec.from_string_spec(f"{_CURRENT.implementation}-{_CURRENT.architecture}-{other_machine}") - assert _CURRENT.satisfies(parsed_spec, impl_must_match=True) is False - - -def test_py_info_satisfies_no_machine_in_spec() -> None: - spec_str = f"{_CURRENT.implementation}{_CURRENT.version_info.major}{_THREADED}-{_CURRENT.architecture}" - parsed_spec = PythonSpec.from_string_spec(spec_str) - assert parsed_spec.machine is None - assert _CURRENT.satisfies(parsed_spec, impl_must_match=True) is True - - -@pytest.mark.parametrize( - ("platform", "spec_machine"), - [ - pytest.param("linux-x86_64", "amd64", id="amd64-matches-x86_64"), - pytest.param("macosx-14.0-arm64", "aarch64", id="aarch64-matches-arm64"), - ], -) -def test_py_info_satisfies_machine_cross_os_normalization(platform: str, spec_machine: str) -> None: - info = copy.deepcopy(_CURRENT) - info.sysconfig_platform = platform - spec = PythonSpec.from_string_spec(f"{info.implementation}-{info.architecture}-{spec_machine}") - assert info.satisfies(spec, impl_must_match=True) is True - - -@pytest.mark.parametrize("debug", [True, False], ids=["debug", "release"]) -def test_py_info_debug_build_in_spec(*, debug: bool) -> None: - info = copy.deepcopy(_CURRENT) - info.debug_build = debug - assert ("d-" in info.spec) is debug - - -@pytest.mark.parametrize("debug", [True, False], ids=["debug", "release"]) -def test_py_info_debug_build_exe_names(*, debug: bool) -> None: - info = copy.deepcopy(_CURRENT) - info.debug_build = debug - names = info._find_possible_exe_names() - abiflag_name = f"python{info.version_info.major}.{info.version_info.minor}d" - assert any("_d" in n for n in names) is debug - assert any(n.endswith("-dbg") for n in names) is debug - assert (abiflag_name in names) is debug - - -def test_py_info_debug_build_json_round_trip() -> None: - info = copy.deepcopy(_CURRENT) - info.debug_build = True - restored = PythonInfo.from_json(info.to_json()) - assert restored.debug_build is True - - -def test_py_info_to_dict_includes_sysconfig_platform() -> None: - data = _CURRENT.to_dict() - assert "sysconfig_platform" in data - assert data["sysconfig_platform"] == sysconfig.get_platform() - - -def test_py_info_to_dict_preserves_property_named_answer_key() -> None: - data = _CURRENT.to_dict() - data["system_include"] = "supplied path" - assert PythonInfo.from_dict(data).to_dict()["system_include"] == "supplied path" - - -def test_py_info_json_round_trip() -> None: - json_str = _CURRENT.to_json() - parsed = json.loads(json_str) - assert "sysconfig_platform" in parsed - restored = PythonInfo.from_json(json_str) - assert (restored.sysconfig_platform, restored.machine) == (_CURRENT.sysconfig_platform, _CURRENT.machine) - - -def test_marker_environment_matches_target_interpreter() -> None: - info: Final[PythonInfo | None] = PythonInfo.from_exe(sys.executable, ignore_cache=True, resolve_to_host=False) - assert info is not None - assert info.marker_environment == default_environment() - - -def test_marker_environment_type_has_standard_keys() -> None: - assert MarkerEnvironment.__required_keys__ == default_environment().keys() - - -def test_marker_environment_uses_target_platform(tmp_path: Path) -> None: - (tmp_path / "sitecustomize.py").write_text( - 'import platform\nplatform.machine = lambda: "target-machine"\nplatform.release = lambda: "target-release"\n', - encoding="utf-8", - ) - expected: Final[Environment] = default_environment() - assert expected["platform_machine"] != "target-machine" - assert expected["platform_release"] != "target-release" - info: Final[PythonInfo | None] = PythonInfo.from_exe( - sys.executable, - ignore_cache=True, - resolve_to_host=False, - env={**os.environ, "PYTHONPATH": str(tmp_path)}, - ) - assert info is not None - assert info.marker_environment == { - **expected, - "platform_machine": "target-machine", - "platform_release": "target-release", - } - - -def test_marker_environment_survives_json_round_trip() -> None: - assert PythonInfo.from_json(_CURRENT.to_json()).marker_environment == _CURRENT.marker_environment - - -def test_marker_environment_formats_prerelease(mocker: MockerFixture) -> None: - version: Final[VersionInfo] = VersionInfo(*sys.implementation.version) - mocker.patch.object(sys.implementation, "version", version._replace(releaselevel="candidate", serial=1)) - - assert PythonInfo().marker_environment["implementation_version"] == ( - f"{version.major}.{version.minor}.{version.micro}c1" - ) - - -@pytest.mark.parametrize( - ("target_platform", "discovered_platforms", "expected_idx"), - [ - pytest.param("linux-x86_64", ["linux-aarch64", "linux-x86_64"], 1, id="x86_64-over-aarch64"), - pytest.param("macosx-14.0-arm64", ["macosx-14.0-x86_64", "macosx-14.0-arm64"], 1, id="arm64-over-x86_64"), - ], -) -def test_select_most_likely_prefers_machine_match( - target_platform: str, - discovered_platforms: list[str], - expected_idx: int, -) -> None: - target = copy.deepcopy(_CURRENT) - target.sysconfig_platform = target_platform - discovered = [copy.deepcopy(_CURRENT) for _ in discovered_platforms] - for info, plat in zip(discovered, discovered_platforms): - info.sysconfig_platform = plat - result = PythonInfo._select_most_likely(discovered, target) - assert result.sysconfig_platform == discovered_platforms[expected_idx] - - -@pytest.mark.parametrize( - ("system_executable", "expected"), - [ - pytest.param("/usr/bin/python3", "/usr/bin/python3", id="resolved"), - pytest.param(None, "/venv/bin/python", id="unresolved falls back to executable"), - ], -) -def test_py_info_system_exe(system_executable: str | None, expected: str) -> None: - info = copy.deepcopy(_CURRENT) - info.executable = "/venv/bin/python" - info.system_executable = system_executable - assert info.system_exe == expected diff --git a/tests/py_info/test_py_info_exe_based_of.py b/tests/py_info/test_py_info_exe_based_of.py deleted file mode 100644 index 8089e43..0000000 --- a/tests/py_info/test_py_info_exe_based_of.py +++ /dev/null @@ -1,90 +0,0 @@ -from __future__ import annotations - -import contextlib -import logging -from pathlib import Path -from typing import TYPE_CHECKING, Final - -import pytest - -from python_discovery import DiskCache, PythonInfo -from python_discovery._compat import fs_is_case_sensitive -from python_discovery._discovery import IS_WIN -from python_discovery._py_info import EXTENSIONS - -if TYPE_CHECKING: - from collections.abc import Callable - -_CURRENT: Final[PythonInfo] = PythonInfo.current() -_THREADED: Final[str] = "t" if _CURRENT.free_threaded else "" -_CASE_FOLD: Final[Callable[[str], str]] = str.lower if not fs_is_case_sensitive() else str - - -def _fs_supports_symlink() -> bool: - return not IS_WIN - - -def test_discover_empty_folder(tmp_path: Path, session_cache: DiskCache) -> None: - with pytest.raises(RuntimeError): - _CURRENT.discover_exe(session_cache, prefix=str(tmp_path)) - - -def _discover_base_folders() -> tuple[str, ...]: - exe_dir = str(Path(_CURRENT.executable).parent) - folders: dict[str, None] = {} - if ( - exe_dir.startswith(_CURRENT.prefix) - and ( # pragma: no branch, the branch depends on the host interpreter layout - relative := exe_dir[len(_CURRENT.prefix) :].lstrip("/\\") - ) - ): - folders[relative] = None - folders["."] = None - return tuple(folders) - - -_BASE: Final[tuple[str, ...]] = _discover_base_folders() - - -@pytest.mark.skipif(not _fs_supports_symlink(), reason="symlink is not supported") -@pytest.mark.parametrize("suffix", sorted({".exe", ""} & set(EXTENSIONS) if IS_WIN else [""])) -@pytest.mark.parametrize("into", _BASE) -@pytest.mark.parametrize( - "arch", - [pytest.param(_CURRENT.architecture, id="current"), pytest.param("", id="empty")], -) -@pytest.mark.parametrize("version", [".".join(str(i) for i in _CURRENT.version_info[0:i]) for i in range(3, 0, -1)]) -@pytest.mark.parametrize( - "impl", - [pytest.param(_CURRENT.implementation, id="current"), pytest.param("python", id="python")], -) -def test_discover_ok( - tmp_path: Path, - suffix: str, - impl: str, - version: str, - *, - arch: int | str, - into: str, - caplog: pytest.LogCaptureFixture, - session_cache: DiskCache, -) -> None: - caplog.set_level(logging.DEBUG) - folder = tmp_path / into - folder.mkdir(parents=True, exist_ok=True) - dest = folder / f"{impl}{version}{_THREADED}{f'-{arch}' * bool(arch)}{suffix}" - Path(str(dest)).symlink_to(_CURRENT.executable) - pyvenv = Path(_CURRENT.executable).parents[1] / "pyvenv.cfg" - with contextlib.suppress(FileNotFoundError): - (folder / pyvenv.name).write_text(pyvenv.read_text(encoding="utf-8"), encoding="utf-8") - inside_folder = str(tmp_path) - found = _CURRENT.discover_exe(session_cache, inside_folder).executable - dest_str = str(dest) - assert _CASE_FOLD(found) == _CASE_FOLD(dest_str) - assert len(caplog.messages) >= 1, caplog.text - assert "get interpreter info via cmd: " in caplog.text - - dest.rename(dest.parent / (f"{dest.name}-1")) - _CURRENT._cache_exe_discovery.clear() - with pytest.raises(RuntimeError): - _CURRENT.discover_exe(session_cache, inside_folder) diff --git a/tests/windows/conftest.py b/tests/python_discovery/_windows/conftest.py similarity index 78% rename from tests/windows/conftest.py rename to tests/python_discovery/_windows/conftest.py index 0cbd8f9..c83ada2 100644 --- a/tests/windows/conftest.py +++ b/tests/python_discovery/_windows/conftest.py @@ -1,7 +1,6 @@ from __future__ import annotations import importlib -import os import runpy import sys from contextlib import contextmanager @@ -13,12 +12,12 @@ import pytest if TYPE_CHECKING: - from collections.abc import Generator + from collections.abc import Generator, Mapping from pytest_mock import MockerFixture, MockType from typing_extensions import Self - from python_discovery import PythonSpec + from python_discovery import PyInfoCache, PythonSpec @pytest.fixture @@ -61,12 +60,13 @@ def _query_value_ex(key: _Key | int, value_name: str) -> tuple[str, int]: monkeypatch.setattr(winreg, "EnumKey", _enum_key) monkeypatch.setattr(winreg, "QueryValueEx", _query_value_ex) monkeypatch.setattr(winreg, "OpenKeyEx", _open_key_ex) - real_exists = os.path.exists + real_exists = Path.exists - def _mock_exists(path: str) -> bool: - return (isinstance(path, str) and ("\\" in path or path.startswith(("C:", "Z:")))) or real_exists(path) + def _mock_exists(path: Path) -> bool: + raw = str(path) + return ("\\" in raw or raw.startswith(("C:", "Z:"))) or real_exists(path) - monkeypatch.setattr("os.path.exists", _mock_exists) + monkeypatch.setattr(Path, "exists", _mock_exists) # _pep514 binds winreg at import, so every test must patch the same fake module. @@ -74,15 +74,15 @@ def _mock_exists(path: str) -> bool: def _create_winreg_mock() -> ModuleType: # Create a mock winreg module that works on all platforms. winreg = ModuleType("winreg") - winreg.HKEY_CURRENT_USER = 0x80000001 # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - winreg.HKEY_LOCAL_MACHINE = 0x80000002 # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - winreg.KEY_READ = 0x20019 # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - winreg.KEY_WOW64_64KEY = 0x0100 # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - winreg.KEY_WOW64_32KEY = 0x0200 # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + winreg.HKEY_CURRENT_USER = 0x80000001 # ty: ignore[unresolved-attribute] Windows-only + winreg.HKEY_LOCAL_MACHINE = 0x80000002 # ty: ignore[unresolved-attribute] Windows-only + winreg.KEY_READ = 0x20019 # ty: ignore[unresolved-attribute] Windows-only + winreg.KEY_WOW64_64KEY = 0x0100 # ty: ignore[unresolved-attribute] Windows-only + winreg.KEY_WOW64_32KEY = 0x0200 # ty: ignore[unresolved-attribute] Windows-only # the fixture swaps these for registry lookups; the attributes must exist for mocker to patch them - winreg.EnumKey = None # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - winreg.QueryValueEx = None # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it - winreg.OpenKeyEx = None # ty: ignore[unresolved-attribute] winreg is Windows-only, so type checkers cannot resolve it + winreg.EnumKey = None # ty: ignore[unresolved-attribute] Windows-only + winreg.QueryValueEx = None # ty: ignore[unresolved-attribute] Windows-only + winreg.OpenKeyEx = None # ty: ignore[unresolved-attribute] Windows-only return winreg @@ -104,8 +104,8 @@ def __exit__( @pytest.fixture -def _populate_pyinfo_cache(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None: - from python_discovery._cached_py_info import _CACHE +def _mock_interpreter_query(mocker: MockerFixture) -> None: + from python_discovery import PythonInfo python_core_path = "C:\\Users\\user\\AppData\\Local\\Programs\\Python" interpreters = [ @@ -122,19 +122,28 @@ def _populate_pyinfo_cache(monkeypatch: pytest.MonkeyPatch, mocker: MockerFixtur ("PythonCore", 3, 4, 64, False, "C:\\Python34\\python.exe"), ("CompanyA", 3, 6, 64, False, "Z:\\CompanyA\\Python\\3.6\\python.exe"), ] - for _, major, minor, arch, threaded, exe in interpreters: - info = _mock_pyinfo(mocker, major, minor, arch, exe, threaded) - monkeypatch.setitem(_CACHE, (Path(info.executable), None), info) + entries = { + exe: _mock_pyinfo(mocker, major, minor, arch, exe, threaded) + for _, major, minor, arch, threaded, exe in interpreters + } + + def query( + exe: str, + _cache: PyInfoCache | None, + **_kwargs: str | bool | Mapping[str, str] | None, + ) -> MockType: + return entries[exe] + + mocker.patch.object(PythonInfo, "_from_exe_with_args", autospec=True, side_effect=query) def _mock_pyinfo(mocker: MockerFixture, major: int, minor: int, arch: int, exe: str, threaded: bool) -> MockType: - from python_discovery._py_info import PythonInfo, VersionInfo + from python_discovery import PythonInfo - info = mocker.MagicMock(spec=PythonInfo, base_prefix=str(Path(exe).parent)) + info = mocker.create_autospec(PythonInfo, instance=True, base_prefix=str(Path(exe).parent)) info.executable = info.original_executable = info.system_executable = exe info.implementation = "CPython" info.architecture = arch - info.version_info = VersionInfo(major, minor, 0, "final", 0) info.free_threaded = threaded info.sysconfig_platform = "win-amd64" if arch == 64 else "win32" info.machine = "x86_64" if arch == 64 else "x86" diff --git a/tests/windows/test_windows_pep514.py b/tests/python_discovery/_windows/test_pep514.py similarity index 69% rename from tests/windows/test_windows_pep514.py rename to tests/python_discovery/_windows/test_pep514.py index 23d5a13..101c3b4 100644 --- a/tests/windows/test_windows_pep514.py +++ b/tests/python_discovery/_windows/test_pep514.py @@ -12,17 +12,62 @@ import pytest +from python_discovery import PythonSpec + if TYPE_CHECKING: from collections.abc import Callable from pytest_mock import MockerFixture +@pytest.mark.skipif(sys.platform != "win32", reason="propose_interpreters calls from_exe with Windows paths") +@pytest.mark.usefixtures("_mock_registry") +@pytest.mark.usefixtures("_mock_interpreter_query") +@pytest.mark.parametrize( + ("string_spec", "expected_exe"), + [ + pytest.param("python3.10", "C:\\Users\\user\\Miniconda3-64\\python.exe", id="python3.10"), + pytest.param("cpython3.10", "C:\\Users\\user\\Miniconda3-64\\python.exe", id="cpython3.10"), + pytest.param( + "python3.12", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="python3.12" + ), + pytest.param( + "cpython3.12", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="cpython3.12" + ), + pytest.param( + "python", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", id="python" + ), + pytest.param( + "cpython", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", id="cpython" + ), + pytest.param( + "python3", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="python3" + ), + pytest.param( + "cpython3", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="cpython3" + ), + pytest.param("python3.6", "Z:\\CompanyA\\Python\\3.6\\python.exe", id="python3.6"), + pytest.param("cpython3.6", "Z:\\CompanyA\\Python\\3.6\\python.exe", id="cpython3.6"), + pytest.param("3t", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", id="3t"), + pytest.param( + "python3.13t", + "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", + id="python3.13t", + ), + ], +) +def test_propose_interpreters(string_spec: str, expected_exe: str) -> None: + from python_discovery._windows._propose import propose_interpreters + + interpreter = next(propose_interpreters(spec=PythonSpec.from_string_spec(string_spec), cache=None, env={})) + assert interpreter.executable == expected_exe + + @pytest.mark.usefixtures("_mock_registry") def test_pep514_discovers_interpreters() -> None: - from python_discovery._windows._pep514 import _discover_pythons + from python_discovery._windows._pep514 import discover_pythons - interpreters = list(_discover_pythons()) + interpreters = list(discover_pythons()) assert len(interpreters) == 12 companies = {i[0] for i in interpreters} assert "ContinuumAnalytics" in companies @@ -53,9 +98,9 @@ def test_pep514_parse_functions() -> None: @pytest.mark.skipif(sys.platform != "win32", reason="path joining differs on POSIX") @pytest.mark.usefixtures("_mock_registry") def test_pep514() -> None: - from python_discovery._windows._pep514 import _discover_pythons + from python_discovery._windows._pep514 import discover_pythons - interpreters = list(_discover_pythons()) + interpreters = list(discover_pythons()) assert interpreters == [ ("ContinuumAnalytics", 3, 10, 32, False, "C:\\Users\\user\\Miniconda3\\python.exe", None), ("ContinuumAnalytics", 3, 10, 64, False, "C:\\Users\\user\\Miniconda3-64\\python.exe", None), @@ -131,9 +176,9 @@ def test_pep514() -> None: @pytest.mark.skipif(sys.platform != "win32", reason="path joining differs on POSIX") @pytest.mark.usefixtures("_mock_registry") def test_pep514_run(capsys: pytest.CaptureFixture[str], caplog: pytest.LogCaptureFixture) -> None: - from python_discovery._windows import _pep514 as pep514 + from python_discovery._windows._pep514 import _run - pep514._run() + _run() out, err = capsys.readouterr() py = r"C:\\Users\\user\\AppData\\Local\\Programs\\Python" expected = textwrap.dedent( @@ -191,32 +236,40 @@ def test_pep514_rejects_non_absolute_executable( executable_path: str | int | bytes | list[str] | None, install_path: str | int | None, ) -> None: - from python_discovery._windows import _pep514 as pep514 + from python_discovery._windows._pep514 import _load_exe registered = {"ExecutablePath": executable_path, None: install_path} - mocker.patch.object( - pep514.winreg, "OpenKeyEx", return_value=mocker.MagicMock(spec=AbstractContextManager), autospec=True + mocker.patch( + "python_discovery._windows._pep514.winreg.OpenKeyEx", + return_value=mocker.create_autospec(AbstractContextManager, instance=True), + autospec=True, + ) + mocker.patch( + "python_discovery._windows._pep514._get_value", + side_effect=lambda _key, name: registered.get(name), + autospec=True, ) - mocker.patch.object(pep514, "_get_value", side_effect=lambda _key, name: registered.get(name), autospec=True) - mocker.patch.object(pep514.os.path, "exists", return_value=True, autospec=True) + mocker.patch("python_discovery._windows._pep514.Path.exists", return_value=True, autospec=True) - assert pep514._load_exe("HKCU", "PythonCore", object(), "3.11") is None + assert _load_exe("HKCU", "PythonCore", object(), "3.11") is None assert "expected an absolute executable path" in caplog.text @pytest.fixture def load_exe_with_args(mocker: MockerFixture) -> Callable[[str | int | None], tuple[str, str | None] | None]: - from python_discovery._windows import _pep514 as pep514 + from python_discovery._windows._pep514 import _load_exe - mocker.patch.object( - pep514.winreg, "OpenKeyEx", return_value=mocker.MagicMock(spec=AbstractContextManager), autospec=True + mocker.patch( + "python_discovery._windows._pep514.winreg.OpenKeyEx", + return_value=mocker.create_autospec(AbstractContextManager, instance=True), + autospec=True, ) - mocker.patch.object(pep514, "_resolve_exe", return_value=r"C:\py.exe", autospec=True) - mocker.patch.object(pep514.os.path, "exists", return_value=True, autospec=True) + mocker.patch("python_discovery._windows._pep514._resolve_exe", return_value=r"C:\py.exe", autospec=True) + mocker.patch("python_discovery._windows._pep514.Path.exists", return_value=True, autospec=True) def load(registered: str | int | None) -> tuple[str, str | None] | None: - mocker.patch.object(pep514, "_get_value", return_value=registered, autospec=True) - return pep514._load_exe("HKCU", "PythonCore", object(), "3.11") + mocker.patch("python_discovery._windows._pep514._get_value", return_value=registered, autospec=True) + return _load_exe("HKCU", "PythonCore", object(), "3.11") return load @@ -255,8 +308,8 @@ def _launcher_selects_311() -> bool: sys.platform != "win32" or not _launcher_selects_311(), reason="requires the Windows launcher with Python 3.11" ) def test_pep514_launcher_arguments_select_interpreter(mocker: MockerFixture, tmp_path: Path) -> None: - from python_discovery import DiskCache, PythonSpec - from python_discovery._windows import _propose + from python_discovery import DiskCache + from python_discovery._windows._propose import propose_interpreters launcher = shutil.which("py") assert launcher is not None @@ -265,15 +318,12 @@ def test_pep514_launcher_arguments_select_interpreter(mocker: MockerFixture, tmp ) probe = subprocess.run([launcher, "-3.11", "-c", script], capture_output=True, text=True, check=True) selected, major, minor, arch = json.loads(probe.stdout) - mocker.patch.object( - _propose, - "_discover_pythons", + mocker.patch( + "python_discovery._windows._propose.discover_pythons", return_value=[("PythonCore", major, minor, arch, False, launcher, "-3.11")], autospec=True, ) - info = next( - _propose.propose_interpreters(PythonSpec.from_string_spec("python3.11"), DiskCache(tmp_path), os.environ) - ) + info = next(propose_interpreters(PythonSpec.from_string_spec("python3.11"), DiskCache(tmp_path), os.environ)) assert Path(info.executable).samefile(selected) diff --git a/tests/windows/winreg_mock_values.py b/tests/python_discovery/_windows/winreg_mock_values.py similarity index 100% rename from tests/windows/winreg_mock_values.py rename to tests/python_discovery/_windows/winreg_mock_values.py diff --git a/tests/test_cache.py b/tests/python_discovery/test_cache.py similarity index 100% rename from tests/test_cache.py rename to tests/python_discovery/test_cache.py diff --git a/tests/test_cached_py_info.py b/tests/python_discovery/test_cached_py_info.py similarity index 84% rename from tests/test_cached_py_info.py rename to tests/python_discovery/test_cached_py_info.py index 2b85a73..550b953 100644 --- a/tests/test_cached_py_info.py +++ b/tests/python_discovery/test_cached_py_info.py @@ -284,27 +284,29 @@ def test_collect_query_output_read_error(mocker: MockerFixture) -> None: def test_collect_query_output_requires_pipes( mocker: MockerFixture, stdout: StringIO | None, stderr: StringIO | None ) -> None: - process = mocker.MagicMock(spec=Popen, stdout=stdout, stderr=stderr) + process = mocker.create_autospec(Popen, instance=True, stdout=stdout, stderr=stderr) with pytest.raises(ValueError, match="query output pipes are required"): _collect_query_output(process, 1) def test_load_cached_py_info_valid(mocker: MockerFixture) -> None: - result = _load_cached_py_info(PythonInfo, mocker.MagicMock(spec=ContentStore), PythonInfo().to_dict()) + result = _load_cached_py_info( + PythonInfo, mocker.create_autospec(ContentStore, instance=True), PythonInfo().to_dict() + ) assert result is not None assert isinstance(result, PythonInfo) def test_load_cached_py_info_bad_data(mocker: MockerFixture) -> None: - store = mocker.MagicMock(spec=ContentStore) + store = mocker.create_autospec(ContentStore, instance=True) result = _load_cached_py_info(PythonInfo, store, {"bad": "data"}) assert result is None store.remove.assert_called_once() def test_load_cached_py_info_system_exe_missing(mocker: MockerFixture) -> None: - store = mocker.MagicMock(spec=ContentStore) + store = mocker.create_autospec(ContentStore, instance=True) content = PythonInfo().to_dict() content["system_executable"] = "/nonexistent/python" mocker.patch("os.path.exists", return_value=False, autospec=True) @@ -607,3 +609,108 @@ def test_executable_args_memory_cache_keyed_by_args(tmp_path: Path, mocker: Mock assert results == [first, second, first] assert query.call_count == 2 + + +@pytest.mark.parametrize( + ("raw", "expected"), + [ + pytest.param(None, 15.0, id="default"), + pytest.param("30", 30.0, id="seconds"), + pytest.param(" 0.5 ", 0.5, id="padded-fraction"), + pytest.param("inf", None, id="inf"), + pytest.param("1e30", None, id="beyond-platform-wait"), + pytest.param("2147483.647", 2147483.647, id="wait-limit"), + pytest.param("2147483.648", None, id="above-wait-limit"), + ], +) +def test_from_exe_query_timeout( + mocker: MockerFixture, caplog: pytest.LogCaptureFixture, raw: str | None, expected: float | None +) -> None: + collect = mocker.patch( + "python_discovery._cached_py_info._collect_query_output", side_effect=_collect_query_output, autospec=True + ) + env = {key: value for key, value in {**os.environ, "PY_DISCOVERY_TIMEOUT": raw}.items() if value is not None} + result = PythonInfo.from_exe(sys.executable, env=env, ignore_cache=True, resolve_to_host=False) + assert result is not None + assert result.version_info == sys.version_info + assert collect.call_args_list[0].args[1] == expected + assert not caplog.records + + +@pytest.mark.parametrize( + "raw", + [ + pytest.param("", id="empty"), + pytest.param("abc", id="words"), + pytest.param("0x10", id="hex"), + pytest.param("nan", id="nan"), + pytest.param("0", id="zero"), + pytest.param("-1", id="negative"), + pytest.param("-inf", id="negative-inf"), + pytest.param("1e-999", id="underflow"), + ], +) +def test_from_exe_invalid_timeout_warns_once(mocker: MockerFixture, caplog: pytest.LogCaptureFixture, raw: str) -> None: + collect = mocker.patch( + "python_discovery._cached_py_info._collect_query_output", side_effect=_collect_query_output, autospec=True + ) + env = {**os.environ, "PY_DISCOVERY_TIMEOUT": raw} + for _ in range(2): + result = PythonInfo.from_exe(sys.executable, env=env, ignore_cache=True, resolve_to_host=False) + assert result is not None + assert result.version_info == sys.version_info + assert [call.args[1] for call in collect.call_args_list] == [15.0] * 2 + assert [(record.levelno, record.getMessage()) for record in caplog.records] == [ + (logging.WARNING, f"ignoring PY_DISCOVERY_TIMEOUT={raw!r}, not a positive number of seconds; using 15.0") + ] + + +def test_from_exe_timeout_warning_cache_is_bounded(caplog: pytest.LogCaptureFixture) -> None: + for index in range(129): + result = PythonInfo.from_exe( + sys.executable, + env={**os.environ, "PY_DISCOVERY_TIMEOUT": f"invalid-timeout-{index}"}, + ignore_cache=True, + resolve_to_host=False, + ) + assert result is not None + assert result.version_info == sys.version_info + caplog.clear() + result = PythonInfo.from_exe( + sys.executable, + env={**os.environ, "PY_DISCOVERY_TIMEOUT": "invalid-timeout-128"}, + ignore_cache=True, + resolve_to_host=False, + ) + assert result is not None + assert result.version_info == sys.version_info + result = PythonInfo.from_exe( + sys.executable, + env={**os.environ, "PY_DISCOVERY_TIMEOUT": "invalid-timeout-0"}, + ignore_cache=True, + resolve_to_host=False, + ) + assert result is not None + assert result.version_info == sys.version_info + assert [record.getMessage() for record in caplog.records] == [ + "ignoring PY_DISCOVERY_TIMEOUT='invalid-timeout-0', not a positive number of seconds; using 15.0" + ] + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX shell launcher") +def test_from_exe_timeout_with_inherited_pipes(tmp_path: Path) -> None: + executable = tmp_path / "python3.11" + executable.write_text("#!/bin/sh\n/bin/sleep 3 &\nexit 0\n") + executable.chmod(0o755) + + started = monotonic() + result = PythonInfo.from_exe( + str(executable), + env={**os.environ, "PY_DISCOVERY_TIMEOUT": "0.1"}, + ignore_cache=True, + raise_on_error=False, + resolve_to_host=False, + ) + + assert result is None + assert monotonic() - started < 2.5 diff --git a/tests/test_discovery.py b/tests/python_discovery/test_discovery.py similarity index 83% rename from tests/test_discovery.py rename to tests/python_discovery/test_discovery.py index b03ab33..0d5de82 100644 --- a/tests/test_discovery.py +++ b/tests/python_discovery/test_discovery.py @@ -15,7 +15,16 @@ import pytest from python_discovery import DiskCache, PythonInfo, PythonSpec, SimpleSpecifierSet, get_interpreter, iter_interpreters -from python_discovery._discovery import IS_WIN, LazyPathDump, get_paths, propose_interpreters +from python_discovery._discovery import ( + IS_WIN, + LazyPathDump, + _active_versions, + _read_python_version_file, + _resolve_shim, + get_paths, + path_exe_finder, + propose_interpreters, +) if TYPE_CHECKING: from collections.abc import Callable, Iterable @@ -93,6 +102,17 @@ def test_discovery_via_path_literal_symlink(tmp_path: Path) -> None: assert interpreter.executable == str(link) +@pytest.mark.skipif(sys.platform != "win32", reason="Windows executable alias") +def test_discovery_via_windows_path_literal_symlink(tmp_path: Path) -> None: # pragma: win32 cover + link = tmp_path / "python_custom.exe" + link.symlink_to(Path(sys.base_prefix) / "python.exe") + + interpreter = get_interpreter("python_custom", [], env={"PATH": f"{tmp_path};{sys.base_prefix}"}) + + assert interpreter is not None + assert interpreter.executable == str(link) + + @pytest.mark.skipif(sys.implementation.name != "cpython", reason="requires CPython") @pytest.mark.parametrize("name", [pytest.param("pypy", id="pypy"), pytest.param("Python", id="mixed-case-python")]) def test_pypy_spec_rejects_cpython_alias(name: str, tmp_path: Path, session_cache: DiskCache) -> None: @@ -847,7 +867,7 @@ def _probed(**kwargs: object) -> list[str]: return _probed -@pytest.mark.parametrize("expand_user", [True, False], ids=["expands-user", "absolute"]) +@pytest.mark.parametrize("expand_user", [pytest.param(True, id="expands-user"), pytest.param(False, id="absolute")]) def test_discovery_uv_store_install_dir_overrides_the_platform_default( uv_home: Path, monkeypatch: pytest.MonkeyPatch, @@ -1080,3 +1100,191 @@ def test_discovery_uv_invalid_hand_built_spec_skips_install(tmp_path: Path, mock candidates = list(propose_interpreters(spec, [], env={"PATH": "", "UV_PYTHON_INSTALL_DIR": str(store)})) assert all(candidate is not info for candidate, _ in candidates) + + +def test_propose_interpreters_abs_path_oserror(tmp_path: Path) -> None: + results = list(propose_interpreters(PythonSpec.from_string_spec(str(tmp_path / "nonexistent")), [])) + assert results == [] + + +def test_propose_interpreters_try_first_with_valid(session_cache: DiskCache) -> None: + results = list(propose_interpreters(PythonSpec.from_string_spec("python"), [sys.executable], session_cache)) + assert len(results) >= 1 + + +def test_propose_interpreters_try_first_with_missing(tmp_path: Path) -> None: + gen = propose_interpreters(PythonSpec.from_string_spec("python"), [str(tmp_path / "nonexistent")]) + results = [] + for result in gen: # pragma: no branch, the loop runs zero or more times depending on the host + results.append(result) + break + assert len(results) >= 0 + + +def test_propose_interpreters_try_first_with_duplicate(session_cache: DiskCache) -> None: + spec = PythonSpec.from_string_spec("python") + results = list(propose_interpreters(spec, [sys.executable, sys.executable], session_cache)) + set().update([r[0].executable for r in results if r[0] is not None][:2]) + + +def test_propose_interpreters_relative_path( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + session_cache: DiskCache, +) -> None: + Path(sys.executable) + link = tmp_path / _PYTHON_NAME + Path(str(link)).symlink_to(sys.executable) + spec = PythonSpec.from_string_spec(link.name) + spec.path = link.name + monkeypatch.setenv("PATH", str(tmp_path)) + results = list(propose_interpreters(spec, [], session_cache)) + assert len(results) >= 0 + + +def test_lazy_path_dump_basic(tmp_path: Path) -> None: + result = repr(LazyPathDump(0, tmp_path, {})) + assert "PATH[0]" in result + assert "with =>" not in result + + +def test_lazy_path_dump_debug_with_dir(tmp_path: Path) -> None: + (tmp_path / "subdir").mkdir() + result = repr(LazyPathDump(0, tmp_path, {"_VIRTUALENV_DEBUG": "1"})) + assert "subdir" not in result + + +@pytest.mark.skipif(IS_WIN, reason="POSIX test") +def test_lazy_path_dump_debug_non_executable(tmp_path: Path) -> None: + (tmp_path / "not_executable").touch(mode=0o644) + result = repr(LazyPathDump(0, tmp_path, {"_VIRTUALENV_DEBUG": "1"})) + assert "not_executable" not in result + + +def test_lazy_path_dump_debug_oserror(tmp_path: Path, mocker: MockerFixture) -> None: + (tmp_path / "bad_file").touch() + mocker.patch("pathlib.Path.is_dir", side_effect=[False, False], autospec=True) + mocker.patch("pathlib.Path.stat", side_effect=OSError("permission denied"), autospec=True) + repr(LazyPathDump(0, tmp_path, {"_VIRTUALENV_DEBUG": "1"})) + + +def test_active_versions_pyenv_version(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("PYENV_VERSION", "3.12.0:3.11.0") + versions = list(_active_versions(dict(os.environ))) + assert versions == ["3.12.0", "3.11.0"] + + +def test_active_versions_python_version_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("PYENV_VERSION", raising=False) + (tmp_path / ".python-version").write_text("3.12.0\n", encoding="utf-8") + monkeypatch.chdir(tmp_path) + versions = list(_active_versions(dict(os.environ))) + assert versions == ["3.12.0"] + + +def test_active_versions_global_version_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("PYENV_VERSION", raising=False) + monkeypatch.setenv("PYENV_ROOT", str(tmp_path)) + workdir = tmp_path / "workdir" + workdir.mkdir() + monkeypatch.chdir(workdir) + (tmp_path / "version").write_text("3.11.0\n", encoding="utf-8") + versions = list(_active_versions(dict(os.environ))) + assert versions == ["3.11.0"] + + +def test_active_versions_no_source(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("PYENV_VERSION", raising=False) + monkeypatch.delenv("PYENV_ROOT", raising=False) + monkeypatch.chdir(tmp_path) + versions = list(_active_versions(dict(os.environ))) + assert versions == [] + + +def test_read_python_version_file_found(tmp_path: Path) -> None: + (tmp_path / ".python-version").write_text("3.12.0\n# comment\n", encoding="utf-8") + result = _read_python_version_file(str(tmp_path)) + assert result == ["3.12.0"] + + +def test_read_python_version_file_not_found(tmp_path: Path) -> None: + result = _read_python_version_file(str(tmp_path), search_parents=False) + assert result is None + + +def test_read_python_version_file_search_parents(tmp_path: Path) -> None: + (tmp_path / ".python-version").write_text("3.11.0\n", encoding="utf-8") + child = tmp_path / "child" + child.mkdir() + result = _read_python_version_file(str(child)) + assert result == ["3.11.0"] + + +def test_read_python_version_file_direct_path(tmp_path: Path) -> None: + version_file = tmp_path / "version" + version_file.write_text("3.12.0\n", encoding="utf-8") + result = _read_python_version_file(str(version_file), search_parents=False) + assert result == ["3.12.0"] + + +def test_resolve_shim_no_match() -> None: + result = _resolve_shim("/some/random/path", dict(os.environ)) + assert result is None + + +def test_path_exe_finder_returns_callable(tmp_path: Path) -> None: + finder = path_exe_finder(PythonSpec.from_string_spec("python3.12")) + assert callable(finder) + results = list(finder(tmp_path)) + assert results == [] + + +def test_propose_interpreters_abs_path_exists(session_cache: DiskCache) -> None: + results = list(propose_interpreters(PythonSpec.from_string_spec(sys.executable), [], session_cache)) + assert len(results) >= 1 + + +def test_propose_interpreters_relative_spec_is_abs(tmp_path: Path, session_cache: DiskCache) -> None: + link = tmp_path / _PYTHON_NAME + Path(str(link)).symlink_to(sys.executable) + spec = PythonSpec.from_string_spec(str(link)) + spec.path = str(link) + results = list(propose_interpreters(spec, [], session_cache)) + assert len(results) >= 1 + + +def test_resolve_shim_match_no_binary(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + shims = tmp_path / "shims" + shims.mkdir() + (tmp_path / "versions").mkdir() + monkeypatch.setenv("PYENV_ROOT", str(tmp_path)) + result = _resolve_shim(str(shims / "python3"), dict(os.environ)) + assert result is None + + +def test_resolve_shim_dir_no_match(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("PYENV_ROOT", str(tmp_path)) + result = _resolve_shim("/other/dir/python3", dict(os.environ)) + assert result is None + + +def test_read_python_version_file_reaches_root() -> None: + result = _read_python_version_file("/nonexistent/deep/path/that/does/not/exist") + assert result is None + + +def test_read_python_version_file_empty_versions(tmp_path: Path) -> None: + (tmp_path / ".python-version").write_text("# only comments\n\n", encoding="utf-8") + result = _read_python_version_file(str(tmp_path), search_parents=False) + assert result is None + + +def test_get_interpreter_multi_spec_all_fail(session_cache: DiskCache) -> None: + result = get_interpreter(["magic-one", "magic-two"], cache=session_cache) + assert result is None + + +def test_get_interpreter_multi_spec_fallback(session_cache: DiskCache) -> None: + result = get_interpreter(["magic-one", sys.executable], cache=session_cache) + assert result is not None + assert result.executable == sys.executable diff --git a/tests/python_discovery/test_py_info.py b/tests/python_discovery/test_py_info.py new file mode 100644 index 0000000..36aa4e7 --- /dev/null +++ b/tests/python_discovery/test_py_info.py @@ -0,0 +1,1247 @@ +from __future__ import annotations + +import contextlib +import copy +import itertools +import json +import logging +import os +import sys +import sysconfig +from pathlib import Path +from textwrap import dedent +from typing import TYPE_CHECKING, Final, NamedTuple + +import pytest +from packaging.markers import default_environment +from setuptools.dist import Distribution + +from python_discovery import DiskCache, MarkerEnvironment, PyInfoCache, PythonInfo, PythonSpec, SimpleSpecifierSet +from python_discovery._py_info import EXTENSIONS, VersionInfo + +if TYPE_CHECKING: + from packaging.markers import Environment + from pytest_mock import MockerFixture + + +_IS_PYPY: Final[bool] = PythonInfo.current_system().implementation == "PyPy" + +_CURRENT: Final[PythonInfo] = PythonInfo.current_system() +_EXECUTABLE_SUFFIX: Final[str] = Path(_CURRENT.executable).suffix if sys.platform == "win32" else "" +_ORIGINAL_SUFFIX: Final[str] = Path(_CURRENT.original_executable).suffix if sys.platform == "win32" else "" +_THREADED: Final[str] = "t" if _CURRENT.free_threaded else "" +_UNTHREADED: Final[str] = "" if _CURRENT.free_threaded else "t" +_OTHER_ARCHITECTURE: Final[int] = {32: 64, 64: 32}[_CURRENT.architecture] + + +@pytest.mark.graalpy +def test_current_as_json() -> None: + parsed = json.loads(_CURRENT.to_json()) + major, minor, micro, releaselevel, serial = sys.version_info + free_threaded = sysconfig.get_config_var("Py_GIL_DISABLED") == 1 + assert parsed["version_info"] == { + "major": major, + "minor": minor, + "micro": micro, + "releaselevel": releaselevel, + "serial": serial, + } + assert parsed["free_threaded"] is free_threaded + assert parsed["debug_build"] is bool(sysconfig.get_config_var("Py_DEBUG")) + + +def test_bad_exe_py_info_raise(tmp_path: Path, session_cache: DiskCache) -> None: + exe = str(tmp_path) + with pytest.raises(RuntimeError) as context: + PythonInfo.from_exe(exe, session_cache) + msg = str(context.value) + assert "code" in msg + assert exe in msg + + +def test_bad_exe_py_info_no_raise( + tmp_path: Path, + caplog: pytest.LogCaptureFixture, + capsys: pytest.CaptureFixture[str], + session_cache: DiskCache, +) -> None: + caplog.set_level(logging.NOTSET) + exe = str(tmp_path) + result = PythonInfo.from_exe(exe, session_cache, raise_on_error=False) + assert result is None + out, _ = capsys.readouterr() + assert not out + messages = [r.message for r in caplog.records if r.name != "filelock"] + assert len(messages) == 4 + assert "get interpreter info via cmd: " in messages[0] + assert "retrying" in messages[1] + assert "get interpreter info via cmd: " in messages[2] + assert str(exe) in messages[3] + assert "code" in messages[3] + + +@pytest.mark.parametrize( + "spec", + [sys.executable] + + [ + f"{impl}{'.'.join(str(i) for i in ver)}{'t' if _CURRENT.free_threaded else ''}{arch}" + for impl, ver, arch in itertools.product( + ( + [_CURRENT.implementation] + + (["python"] if _CURRENT.implementation == "CPython" else []) + + ( + [_CURRENT.implementation.lower()] + if _CURRENT.implementation != _CURRENT.implementation.lower() + else [] + ) + ), + [sys.version_info[0 : i + 1] for i in range(3)], + ["", f"-{_CURRENT.architecture}"], + ) + ], +) +def test_satisfy_py_info(spec: str) -> None: + matches = _CURRENT.satisfies(PythonSpec.from_string_spec(spec), impl_must_match=True) + assert matches is True + + +def test_satisfy_not_arch() -> None: + parsed_spec = PythonSpec.from_string_spec( + f"{_CURRENT.implementation}-{_OTHER_ARCHITECTURE}", + ) + matches = _CURRENT.satisfies(parsed_spec, impl_must_match=True) + assert matches is False + + +def test_satisfy_not_threaded() -> None: + parsed_spec = PythonSpec.from_string_spec( + f"{_CURRENT.implementation}{_CURRENT.version_info.major}{_UNTHREADED}", + ) + matches = _CURRENT.satisfies(parsed_spec, impl_must_match=True) + assert matches is False + + +def _generate_not_match_current_interpreter_version() -> list[str]: + result: list[str] = [] + for depth in range(3): + ver: list[int] = [int(part) for part in sys.version_info[0 : depth + 1]] + for idx in range(len(ver)): + for offset in [-1, 1]: + temp = ver.copy() + temp[idx] += offset + result.append(".".join(str(part) for part in temp)) + return result + + +_NON_MATCH_VER: Final[list[str]] = _generate_not_match_current_interpreter_version() + + +@pytest.mark.parametrize("spec", _NON_MATCH_VER) +def test_satisfy_not_version(spec: str) -> None: + matches = _CURRENT.satisfies(PythonSpec.from_string_spec(f"{_CURRENT.implementation}{spec}"), impl_must_match=True) + assert matches is False + + +def test_py_info_cached_error(caplog: pytest.LogCaptureFixture, tmp_path: Path, session_cache: DiskCache) -> None: + caplog.set_level(logging.DEBUG) + with pytest.raises(RuntimeError): + PythonInfo.from_exe(str(tmp_path), session_cache) + with pytest.raises(RuntimeError): + PythonInfo.from_exe(str(tmp_path), session_cache) + assert sum("get interpreter info via cmd:" in message for message in caplog.messages) == 4 + + +def test_py_info_cache_clear(caplog: pytest.LogCaptureFixture, session_cache: DiskCache) -> None: + result = PythonInfo.from_exe(sys.executable, session_cache) + assert result is not None + + PythonInfo.clear_cache(session_cache) + caplog.set_level(logging.DEBUG) + info = PythonInfo.from_exe(sys.executable, session_cache) + assert info is not None + assert any("get interpreter info via cmd:" in message for message in caplog.messages) + + +class PyInfoMock(NamedTuple): + implementation: str + architecture: int + version_info: VersionInfo + + +@pytest.mark.parametrize( + ("target", "position", "discovered"), + [ + pytest.param( + PyInfoMock("CPython", 64, VersionInfo(3, 6, 8, "final", 0)), + 0, + [ + PyInfoMock("CPython", 64, VersionInfo(3, 6, 9, "final", 0)), + PyInfoMock("PyPy", 64, VersionInfo(3, 6, 8, "final", 0)), + ], + id="pypy-candidate-3.6.8", + ), + pytest.param( + PyInfoMock("CPython", 64, VersionInfo(3, 6, 8, "final", 0)), + 0, + [ + PyInfoMock("CPython", 64, VersionInfo(3, 6, 9, "final", 0)), + PyInfoMock("CPython", 32, VersionInfo(3, 6, 9, "final", 0)), + ], + id="cpython-32bit-candidate-3.6.9", + ), + pytest.param( + PyInfoMock("CPython", 64, VersionInfo(3, 8, 1, "final", 0)), + 0, + [ + PyInfoMock("CPython", 32, VersionInfo(2, 7, 12, "rc", 2)), + PyInfoMock("PyPy", 64, VersionInfo(3, 8, 1, "final", 0)), + ], + id="pypy-candidate-3.8.1", + ), + ], +) +def test_system_executable_no_exact_match( + target: PyInfoMock, + discovered: list[PyInfoMock], + position: int, + *, + tmp_path: Path, + mocker: MockerFixture, + caplog: pytest.LogCaptureFixture, + session_cache: DiskCache, +) -> None: + caplog.set_level(logging.DEBUG) + + def _make_py_info(of: PyInfoMock) -> PythonInfo: + base = copy.deepcopy(_CURRENT) + base.implementation = of.implementation + base.version_info = of.version_info + base.architecture = of.architecture + return base + + discovered_with_path: dict[str, PythonInfo] = {} + names: list[str] = [] + for pos, info in enumerate(discovered): + path = tmp_path / str(pos) + path.write_text("", encoding="utf-8") + py_info = _make_py_info(info) + system_executable = _CURRENT.system_executable + assert system_executable is not None + py_info.system_executable = system_executable + py_info.executable = system_executable + discovered_with_path[str(path)] = py_info + names.append(path.name) + + target_py_info = _make_py_info(target) + mocker.patch.object(target_py_info, "_find_possible_exe_names", return_value=names, autospec=True) + mocker.patch.object(target_py_info, "_find_possible_folders", return_value=[str(tmp_path)], autospec=True) + + def func(exe_path: str, _cache: PyInfoCache | None = None, **_kwargs: object) -> PythonInfo: + return discovered_with_path[exe_path] + + mocker.patch.object(target_py_info, "from_exe", side_effect=func, autospec=True) + target_py_info.real_prefix = str(tmp_path) + + target_py_info.system_executable = None + target_py_info.executable = str(tmp_path) + mapped = target_py_info.resolve_to_system(session_cache, target_py_info) + assert mapped.system_executable == _CURRENT.system_executable + expected = discovered_with_path[str(tmp_path / str(position))] + assert (mapped.implementation, mapped.architecture, mapped.version_info) == ( + expected.implementation, + expected.architecture, + expected.version_info, + ) + + assert caplog.records[0].msg == "discover exe for %s in %s" + for record in caplog.records[1:-1]: + assert record.message.startswith("refused interpreter ") + assert record.levelno == logging.DEBUG + + warn_similar = caplog.records[-1] + assert warn_similar.levelno == logging.DEBUG + assert warn_similar.msg.startswith("no exact match found, chosen most similar") + + +@pytest.mark.parametrize("attr", ["free_threaded", "debug_build"]) +def test_discover_exe_validates_abi_flag( + attr: str, tmp_path: Path, mocker: MockerFixture, session_cache: DiskCache +) -> None: + def candidate(*, flag: bool, name: str) -> PythonInfo: + info = copy.deepcopy(_CURRENT) + setattr(info, attr, flag) + exe = tmp_path / name + exe.write_text("", encoding="utf-8") + info.executable = str(exe) + return info + + target = candidate(flag=True, name="target") + mismatch = candidate(flag=False, name="release") + match = candidate(flag=True, name="debug") + by_path = {info.executable: info for info in (mismatch, match)} + + mocker.patch.object(target, "_find_possible_exe_names", return_value=["release", "debug"], autospec=True) + mocker.patch.object(target, "_find_possible_folders", return_value=[str(tmp_path)], autospec=True) + mocker.patch.object(target, "from_exe", side_effect=lambda exe, *_a, **_k: by_path[exe], autospec=True) + + found = target.discover_exe(session_cache, prefix=str(tmp_path), exact=True) + assert found is match # the release build is refused even though version, impl and arch match + + +def test_py_info_ignores_distutils_config(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + raw = f""" + [install] + prefix={tmp_path}{os.sep}prefix + install_purelib={tmp_path}{os.sep}purelib + install_platlib={tmp_path}{os.sep}platlib + install_headers={tmp_path}{os.sep}headers + install_scripts={tmp_path}{os.sep}scripts + install_data={tmp_path}{os.sep}data + """ + (tmp_path / "setup.cfg").write_text(dedent(raw), encoding="utf-8") + monkeypatch.chdir(tmp_path) + py_info = PythonInfo.from_exe(sys.executable) + assert py_info is not None + distutils = py_info.distutils_install + for key, value in distutils.items(): # pragma: no cover # distutils_install is empty with "venv" scheme + assert not value.startswith(str(tmp_path)), f"{key}={value}" + + +def test_discover_exe_on_path_non_spec_name_match(mocker: MockerFixture) -> None: + suffixed_name = f"python{_CURRENT.version_info.major}.{_CURRENT.version_info.minor}m{_EXECUTABLE_SUFFIX}" + spec = PythonSpec.from_string_spec(suffixed_name) + mocker.patch.object(_CURRENT, "executable", str(Path(_CURRENT.executable).parent / suffixed_name)) + assert _CURRENT.satisfies(spec, impl_must_match=True) is True + + +def test_satisfies_debug_spec_requires_debug_build(mocker: MockerFixture) -> None: + spec = PythonSpec.from_string_spec(f"python{_CURRENT.version_info.major}.{_CURRENT.version_info.minor}-dbg") + assert spec.debug is True + mocker.patch.object(_CURRENT, "free_threaded", spec.free_threaded) + + mocker.patch.object(_CURRENT, "debug_build", False) + assert _CURRENT.satisfies(spec, impl_must_match=True) is False + + mocker.patch.object(_CURRENT, "debug_build", True) + assert _CURRENT.satisfies(spec, impl_must_match=True) is True + + +def test_discover_exe_on_path_non_spec_name_not_match(mocker: MockerFixture) -> None: + suffixed_name = f"python{_CURRENT.version_info.major}.{_CURRENT.version_info.minor}m{_ORIGINAL_SUFFIX}" + spec = PythonSpec.from_string_spec(suffixed_name) + mocker.patch.object( + _CURRENT, + "original_executable", + str(Path(_CURRENT.executable).parent / f"e{suffixed_name}"), + ) + assert _CURRENT.satisfies(spec, impl_must_match=True) is False + + +@pytest.mark.skipif(_IS_PYPY, reason="setuptools distutils patching does not work") +def test_py_info_setuptools() -> None: + assert Distribution + PythonInfo() + + +@pytest.mark.usefixtures("_skip_if_test_in_system") +def test_py_info_to_system_raises( # pragma: no cover # skipped in venv environments + session_cache: DiskCache, + mocker: MockerFixture, + caplog: pytest.LogCaptureFixture, +) -> None: + caplog.set_level(logging.DEBUG) + mocker.patch.object(PythonInfo, "_find_possible_folders", return_value=[], autospec=True) + result = PythonInfo.from_exe(sys.executable, cache=session_cache, raise_on_error=False) + assert result is None + log = caplog.records[-1] + assert log.levelno == logging.INFO + expected = f"ignore {sys.executable} due cannot resolve system due to RuntimeError('failed to detect " + assert expected in log.message + + +def test_sysconfig_vars_include_shared_lib_keys() -> None: + for key in ("Py_ENABLE_SHARED", "INSTSONAME", "LIBDIR"): + assert key in _CURRENT.sysconfig_vars + + +def test_py_info_has_sysconfig_platform() -> None: + assert hasattr(_CURRENT, "sysconfig_platform") + assert _CURRENT.sysconfig_platform is not None + assert isinstance(_CURRENT.sysconfig_platform, str) + assert len(_CURRENT.sysconfig_platform) > 0 + + +def test_py_info_machine_property() -> None: + machine = _CURRENT.machine + assert machine is not None + assert isinstance(machine, str) + assert len(machine) > 0 + assert machine == machine.lower(), f"machine value should be lowercase: {machine}" + + +def test_py_info_machine_in_spec() -> None: + spec = _CURRENT.spec + assert _CURRENT.machine in spec + assert f"-{_CURRENT.architecture}-{_CURRENT.machine}" in spec + + +def test_py_info_sysconfig_platform_matches_sysconfig() -> None: + assert _CURRENT.sysconfig_platform == sysconfig.get_platform() + + +@pytest.mark.parametrize( + ("platform", "expected"), + [ + pytest.param("win32", "x86", id="win32"), + pytest.param("win-amd64", "x86_64", id="win-amd64"), + pytest.param("win-arm64", "arm64", id="win-arm64"), + pytest.param("linux-x86_64", "x86_64", id="linux-x86_64"), + pytest.param("linux-aarch64", "arm64", id="linux-aarch64"), + pytest.param("linux-riscv64", "riscv64", id="linux-riscv64"), + pytest.param("linux-ppc64le", "ppc64le", id="linux-ppc64le"), + pytest.param("linux-s390x", "s390x", id="linux-s390x"), + pytest.param("macosx-14.0-arm64", "arm64", id="macos-arm64"), + pytest.param("macosx-14.0-x86_64", "x86_64", id="macos-x86_64"), + ], +) +def test_py_info_machine_derivation(platform: str, expected: str) -> None: + info = copy.deepcopy(_CURRENT) + info.sysconfig_platform = platform + assert info.machine == expected + + +@pytest.mark.parametrize("runtime_isa", ["arm64", "x86_64"]) +def test_py_info_machine_derivation_universal2(mocker: MockerFixture, runtime_isa: str) -> None: + info = copy.deepcopy(_CURRENT) + info.sysconfig_platform = "macosx-11.0-universal2" + mocker.patch("python_discovery._py_info.platform.machine", return_value=runtime_isa, autospec=True) + assert info.machine == runtime_isa + + +def test_py_info_satisfies_with_machine() -> None: + threaded = _THREADED + spec_str = ( + f"{_CURRENT.implementation}{_CURRENT.version_info.major}{threaded}-{_CURRENT.architecture}-{_CURRENT.machine}" + ) + parsed_spec = PythonSpec.from_string_spec(spec_str) + assert _CURRENT.satisfies(parsed_spec, impl_must_match=True) is True + + +def test_py_info_satisfies_not_machine() -> None: + other_machine = {"arm64": "x86_64"}.get(_CURRENT.machine, "arm64") + parsed_spec = PythonSpec.from_string_spec(f"{_CURRENT.implementation}-{_CURRENT.architecture}-{other_machine}") + assert _CURRENT.satisfies(parsed_spec, impl_must_match=True) is False + + +def test_py_info_satisfies_no_machine_in_spec() -> None: + spec_str = f"{_CURRENT.implementation}{_CURRENT.version_info.major}{_THREADED}-{_CURRENT.architecture}" + parsed_spec = PythonSpec.from_string_spec(spec_str) + assert parsed_spec.machine is None + assert _CURRENT.satisfies(parsed_spec, impl_must_match=True) is True + + +@pytest.mark.parametrize( + ("platform", "spec_machine"), + [ + pytest.param("linux-x86_64", "amd64", id="amd64-matches-x86_64"), + pytest.param("macosx-14.0-arm64", "aarch64", id="aarch64-matches-arm64"), + ], +) +def test_py_info_satisfies_machine_cross_os_normalization(platform: str, spec_machine: str) -> None: + info = copy.deepcopy(_CURRENT) + info.sysconfig_platform = platform + spec = PythonSpec.from_string_spec(f"{info.implementation}-{info.architecture}-{spec_machine}") + assert info.satisfies(spec, impl_must_match=True) is True + + +@pytest.mark.parametrize("debug", [pytest.param(True, id="debug"), pytest.param(False, id="release")]) +def test_py_info_debug_build_in_spec(*, debug: bool) -> None: + info = copy.deepcopy(_CURRENT) + info.debug_build = debug + assert ("d-" in info.spec) is debug + + +@pytest.mark.parametrize("debug", [pytest.param(True, id="debug"), pytest.param(False, id="release")]) +def test_py_info_debug_build_exe_names(*, debug: bool) -> None: + info = copy.deepcopy(_CURRENT) + info.debug_build = debug + names = info._find_possible_exe_names() + abiflag_name = f"python{info.version_info.major}.{info.version_info.minor}d" + assert any("_d" in n for n in names) is debug + assert any(n.endswith("-dbg") for n in names) is debug + assert (abiflag_name in names) is debug + + +def test_py_info_debug_build_json_round_trip() -> None: + info = copy.deepcopy(_CURRENT) + info.debug_build = True + restored = PythonInfo.from_json(info.to_json()) + assert restored.debug_build is True + + +def test_py_info_to_dict_includes_sysconfig_platform() -> None: + data = _CURRENT.to_dict() + assert "sysconfig_platform" in data + assert data["sysconfig_platform"] == sysconfig.get_platform() + + +def test_py_info_to_dict_preserves_property_named_answer_key() -> None: + data = _CURRENT.to_dict() + data["system_include"] = "supplied path" + assert PythonInfo.from_dict(data).to_dict()["system_include"] == "supplied path" + + +def test_py_info_json_round_trip() -> None: + json_str = _CURRENT.to_json() + parsed = json.loads(json_str) + assert "sysconfig_platform" in parsed + restored = PythonInfo.from_json(json_str) + assert (restored.sysconfig_platform, restored.machine) == (_CURRENT.sysconfig_platform, _CURRENT.machine) + + +def test_marker_environment_matches_target_interpreter() -> None: + info: Final[PythonInfo | None] = PythonInfo.from_exe(sys.executable, ignore_cache=True, resolve_to_host=False) + assert info is not None + assert info.marker_environment == default_environment() + + +def test_marker_environment_type_has_standard_keys() -> None: + assert MarkerEnvironment.__required_keys__ == default_environment().keys() + + +def test_marker_environment_uses_target_platform(tmp_path: Path) -> None: + (tmp_path / "sitecustomize.py").write_text( + 'import platform\nplatform.machine = lambda: "target-machine"\nplatform.release = lambda: "target-release"\n', + encoding="utf-8", + ) + expected: Final[Environment] = default_environment() + assert expected["platform_machine"] != "target-machine" + assert expected["platform_release"] != "target-release" + info: Final[PythonInfo | None] = PythonInfo.from_exe( + sys.executable, + ignore_cache=True, + resolve_to_host=False, + env={**os.environ, "PYTHONPATH": str(tmp_path)}, + ) + assert info is not None + assert info.marker_environment == { + **expected, + "platform_machine": "target-machine", + "platform_release": "target-release", + } + + +def test_marker_environment_survives_json_round_trip() -> None: + assert PythonInfo.from_json(_CURRENT.to_json()).marker_environment == _CURRENT.marker_environment + + +def test_marker_environment_formats_prerelease(mocker: MockerFixture) -> None: + version: Final[VersionInfo] = VersionInfo(*sys.implementation.version) + mocker.patch.object(sys.implementation, "version", version._replace(releaselevel="candidate", serial=1)) + + assert PythonInfo().marker_environment["implementation_version"] == ( + f"{version.major}.{version.minor}.{version.micro}c1" + ) + + +@pytest.mark.parametrize( + ("target_platform", "discovered_platforms", "expected_idx"), + [ + pytest.param("linux-x86_64", ["linux-aarch64", "linux-x86_64"], 1, id="x86_64-over-aarch64"), + pytest.param("macosx-14.0-arm64", ["macosx-14.0-x86_64", "macosx-14.0-arm64"], 1, id="arm64-over-x86_64"), + ], +) +def test_select_most_likely_prefers_machine_match( + target_platform: str, + discovered_platforms: list[str], + expected_idx: int, +) -> None: + target = copy.deepcopy(_CURRENT) + target.sysconfig_platform = target_platform + discovered = [copy.deepcopy(_CURRENT) for _ in discovered_platforms] + for info, plat in zip(discovered, discovered_platforms): + info.sysconfig_platform = plat + result = PythonInfo._select_most_likely(discovered, target) + assert result.sysconfig_platform == discovered_platforms[expected_idx] + + +@pytest.mark.parametrize( + ("system_executable", "expected"), + [ + pytest.param("/usr/bin/python3", "/usr/bin/python3", id="resolved"), + pytest.param(None, "/venv/bin/python", id="unresolved falls back to executable"), + ], +) +def test_py_info_system_exe(system_executable: str | None, expected: str) -> None: + info = copy.deepcopy(_CURRENT) + info.executable = "/venv/bin/python" + info.system_executable = system_executable + assert info.system_exe == expected + + +def test_py_info_pypy_version(mocker: MockerFixture) -> None: + mocker.patch("platform.python_implementation", return_value="PyPy", autospec=True) + mocker.patch.object(sys, "pypy_version_info", (7, 3, 11, "final", 0), create=True) + info = PythonInfo() + assert (info.implementation, info.pypy_version_info) == ("PyPy", (7, 3, 11, "final", 0)) + + +def test_py_info_canonicalizes_graalvm(mocker: MockerFixture) -> None: + mocker.patch("platform.python_implementation", return_value="GraalVM", autospec=True) + info = PythonInfo() + assert info.implementation == "GraalPy" + + +def test_has_venv_attribute() -> None: + info = PythonInfo() + assert isinstance(info.has_venv, bool) + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX only") +@pytest.mark.skipif(bool(_CURRENT.sysconfig_vars.get("PYTHONFRAMEWORK")), reason="framework builds keep recorded path") +def test_from_exe_resolves_executable_only_symlink( # pragma: no cover # skipped on framework interpreter hosts + tmp_path: Path, + session_cache: DiskCache, +) -> None: + system_exe = _CURRENT.system_executable + assert system_exe is not None + link = tmp_path / "python3" + link.symlink_to(system_exe) + info = PythonInfo.from_exe(str(link), session_cache, ignore_cache=True) + assert info is not None + assert info.system_executable is not None + assert Path(info.system_executable).samefile(system_exe) + assert Path(info.system_executable).parent != tmp_path + + +def test_version_str() -> None: + assert _CURRENT.version_str == ".".join(str(i) for i in sys.version_info[:3]) + + +def test_version_release_str() -> None: + assert _CURRENT.version_release_str == ".".join(str(i) for i in sys.version_info[:2]) + + +def test_python_name() -> None: + assert _CURRENT.python_name == f"python{sys.version_info.major}.{sys.version_info.minor}" + + +def test_is_old_virtualenv() -> None: + info = copy.deepcopy(_CURRENT) + info.real_prefix = "/some/prefix" + assert info.is_old_virtualenv is True + info.real_prefix = None + assert info.is_old_virtualenv is False + + +def test_is_venv() -> None: + assert _CURRENT.is_venv == (_CURRENT.base_prefix is not None) + + +def test_system_prefix() -> None: + info = copy.deepcopy(_CURRENT) + info.real_prefix = "/real" + assert info.system_prefix == "/real" + info.real_prefix = None + info.base_prefix = "/base" + assert info.system_prefix == "/base" + + +def test_system_exec_prefix() -> None: + info = copy.deepcopy(_CURRENT) + info.real_prefix = "/real" + assert info.system_exec_prefix == "/real" + info.real_prefix = None + assert info.system_exec_prefix == info.base_exec_prefix or info.exec_prefix + + +def test_repr() -> None: + result = repr(_CURRENT) + assert "PythonInfo" in result + + +def test_str() -> None: + result = str(_CURRENT) + assert "PythonInfo" in result + assert "spec=" in result + + +def test_machine_none_platform() -> None: + info = copy.deepcopy(_CURRENT) + info.sysconfig_platform = None + assert info.machine == "unknown" + + +def test_from_json_round_trip() -> None: + restored = PythonInfo.from_json(_CURRENT.to_json()) + assert (restored.version_info, restored.implementation) == (_CURRENT.version_info, _CURRENT.implementation) + + +def test_from_dict() -> None: + restored = PythonInfo.from_dict(_CURRENT.to_dict()) + assert restored.version_info == _CURRENT.version_info + + +def test_resolve_to_system_circle(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: + caplog.set_level(logging.DEBUG) + target = copy.deepcopy(_CURRENT) + target.system_executable = None + target.real_prefix = None + target.base_prefix = "/prefix_a" + + second = copy.deepcopy(_CURRENT) + second.system_executable = None + second.real_prefix = None + second.base_prefix = "/prefix_b" + + third = copy.deepcopy(_CURRENT) + third.system_executable = None + third.real_prefix = None + third.base_prefix = "/prefix_a" + + mocker.patch.object(PythonInfo, "discover_exe", side_effect=[second, third], autospec=True) + + with pytest.raises(RuntimeError, match="prefixes are causing a circle"): + PythonInfo.resolve_to_system(None, target) + + +def test_resolve_to_system_single_prefix_self_link(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: + caplog.set_level(logging.INFO) + target = copy.deepcopy(_CURRENT) + target.system_executable = None + target.real_prefix = None + target.base_prefix = "/prefix_a" + + second = copy.deepcopy(_CURRENT) + second.system_executable = None + second.real_prefix = None + second.base_prefix = "/prefix_a" + + mocker.patch.object(PythonInfo, "discover_exe", return_value=second, autospec=True) + + result = PythonInfo.resolve_to_system(None, target) + assert result.system_executable is not None + assert any("links back to itself" in r.message for r in caplog.records) + + +def test_resolve_to_system_repeated_prefix_with_parent_segments(tmp_path: Path, mocker: MockerFixture) -> None: + (tmp_path / "child").mkdir() + target = PythonInfo() + target.system_executable = target.real_prefix = None + target.base_prefix = str(tmp_path) + repeated = PythonInfo() + repeated.system_executable = repeated.real_prefix = None + repeated.base_prefix = str(tmp_path / "child" / "..") + mocker.patch.object(PythonInfo, "discover_exe", return_value=repeated, autospec=True) + + result = PythonInfo.resolve_to_system(None, target) + + assert result.system_executable == repeated.executable + + +def test_discover_exe_cache_hit(mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + cached = copy.deepcopy(_CURRENT) + PythonInfo._cache_exe_discovery["/some/prefix", True, info.version_info] = cached + try: + result = info.discover_exe( + mocker.create_autospec(PyInfoCache, instance=True), prefix="/some/prefix", exact=True + ) + assert result is cached + finally: + del PythonInfo._cache_exe_discovery["/some/prefix", True, info.version_info] + + +def test_discover_exe_cache_keeps_requested_version(tmp_path: Path) -> None: + prefix = _CURRENT.prefix + assert prefix is not None + cache = DiskCache(tmp_path) + PythonInfo.clear_cache(cache) + try: + found = _CURRENT.discover_exe(cache, prefix) + assert found.version_info == _CURRENT.version_info + other = copy.deepcopy(_CURRENT) + other.version_info = other.version_info._replace(major=0) + with pytest.raises(RuntimeError): + other.discover_exe(cache, prefix) + finally: + PythonInfo.clear_cache(cache) + + +def test_check_exe_none_path(tmp_path: Path, mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + result = info._check_exe( + mocker.create_autospec(PyInfoCache, instance=True), + str(tmp_path), + "nonexistent", + [], + dict(os.environ), + exact=True, + ) + assert result is None + + +def test_satisfies_version_specifier() -> None: + spec = PythonSpec.from_string_spec(f">={sys.version_info.major}.{sys.version_info.minor}") + assert _CURRENT.satisfies(spec, impl_must_match=False) is True + + +def test_satisfies_version_specifier_fails() -> None: + spec = PythonSpec.from_string_spec(f">{sys.version_info.major + 1}") + assert _CURRENT.satisfies(spec, impl_must_match=False) is False + + +@pytest.mark.parametrize( + ("suffix", "expected"), + [ + pytest.param("t", _CURRENT.free_threaded, id="free-threaded"), + pytest.param("", True, id="any-build"), + ], +) +def test_satisfies_version_specifier_free_threaded(suffix: str, expected: bool) -> None: + spec = PythonSpec.from_string_spec(f">={sys.version_info.major}.{sys.version_info.minor}{suffix}") + assert _CURRENT.satisfies(spec, impl_must_match=False) is expected + + +def test_satisfies_version_specifier_unparsable_version() -> None: + specifier = SimpleSpecifierSet.from_string(">=3.x") + spec = PythonSpec(">=3.x", None, None, None, None, None, None, version_specifier=specifier) + assert _CURRENT.satisfies(spec, impl_must_match=False) is False + + +@pytest.mark.parametrize( + ("version_info", "spec_str", "expected"), + [ + pytest.param(VersionInfo(3, 14, 0, "alpha", 1), ">=3.14.0a1", True, id="alpha_match_exact"), + pytest.param(VersionInfo(3, 14, 0, "beta", 1), ">=3.14.0b1", True, id="beta_match_exact"), + pytest.param(VersionInfo(3, 14, 0, "candidate", 1), ">=3.14.0rc1", True, id="rc_match_exact"), + pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15", True, id="prerelease_match_major_minor"), + pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15.0", False, id="prerelease_not_match_full_precision"), + pytest.param(VersionInfo(3, 15, 0, "alpha", 5), "<3.15.0a6", True, id="earlier_prerelease_less_than"), + pytest.param(VersionInfo(3, 15, 0, "alpha", 6), "<3.15.0a6", False, id="prerelease_not_less_than_itself"), + pytest.param(VersionInfo(3, 12, 0, "candidate", 1), "<3.12.0", False, id="prerelease_not_below_final_bound"), + pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15.0a6", True, id="prerelease_match_itself"), + pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15.0a7", False, id="prerelease_not_match_later"), + pytest.param(VersionInfo(3, 15, 0, "final", 0), ">=3.15.0a6", True, id="final_greater_than_prerelease"), + pytest.param(VersionInfo(3, 15, 0, "final", 0), "<3.15.0a6", False, id="final_not_less_than_prerelease"), + pytest.param(VersionInfo(3, 15, 0, "final", 0), ">=3.15", True, id="final_match_major_minor"), + pytest.param(VersionInfo(3, 15, 1, "alpha", 1), ">=3.15.0", True, id="later_micro_prerelease_match"), + pytest.param(VersionInfo(3, 13, 7, "final", 0), "~=3.10", True, id="compatible_release_later_minor"), + pytest.param(VersionInfo(4, 0, 0, "final", 0), "~=3.10", False, id="compatible_release_next_major"), + pytest.param(VersionInfo(3, 13, 0, "alpha", 1), "~=3.12.0", False, id="compatible_release_next_minor_alpha"), + ], +) +def test_satisfies_version_specifier_prerelease(version_info: VersionInfo, spec_str: str, expected: bool) -> None: + info = copy.deepcopy(_CURRENT) + info.version_info = version_info + spec = PythonSpec.from_string_spec(spec_str) + assert info.satisfies(spec, impl_must_match=False) is expected + + +def test_satisfies_path_not_abs_basename_match() -> None: + info = copy.deepcopy(_CURRENT) + # stem drops everything from the first dot, so python3.15t arrives as python3 and stops naming a free-threaded + # build; only the Windows .exe belongs off the end. + spec = PythonSpec.from_string_spec(Path(info.original_executable).name.removesuffix(".exe")) + assert info.satisfies(spec, impl_must_match=False) is True + + +def test_satisfies_path_not_abs_basename_no_match() -> None: + info = copy.deepcopy(_CURRENT) + spec = PythonSpec.from_string_spec("completely_different_name") + assert info.satisfies(spec, impl_must_match=False) is False + + +@pytest.mark.skipif(sys.platform == "win32", reason="win32 tested separately") +def test_satisfies_path_win32(mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + mocker.patch.object(sys, "platform", "win32") + info.executable = "/some/path/python.exe" + spec = PythonSpec.from_string_spec("python") + spec.path = "python" + assert info.satisfies(spec, impl_must_match=False) is True + + +def test_install_path() -> None: + assert isinstance(_CURRENT.install_path("purelib"), str) + + +def test_install_path_rejects_format_width() -> None: + info = copy.deepcopy(_CURRENT) + info.distutils_install = {} + info.sysconfig_paths["purelib"] = "{base:>1000000}/lib" + with pytest.raises(ValueError, match="format specifiers"): + info.install_path("purelib") + + +def test_system_include() -> None: + result = _CURRENT.system_include + assert isinstance(result, str) + + +@pytest.mark.parametrize( + ("distutils_install", "existing", "expected"), + [ + pytest.param({}, [], "include", id="no-headers-path"), + pytest.param({"headers": "headers/UNKNOWN"}, ["headers"], "headers", id="headers"), + pytest.param({"headers": "headers/UNKNOWN"}, [], "include", id="headers-missing"), + pytest.param({"headers": "headers/UNKNOWN"}, ["include", "headers"], "include", id="include-present"), + ], +) +def test_system_include_fallback( + tmp_path: Path, distutils_install: dict[str, str], existing: list[str], expected: str +) -> None: + info = copy.deepcopy(_CURRENT) + info.prefix, info.base_prefix, info.real_prefix = str(tmp_path), None, None + info.sysconfig_vars = {"base": str(tmp_path)} + info.sysconfig_paths = {"include": "{base}/include"} + info.distutils_install = distutils_install + for name in existing: + (tmp_path / name).mkdir() + assert info.system_include == str(tmp_path / expected) + + +def test_sysconfig_path_missing_key() -> None: + assert not _CURRENT.sysconfig_path("nonexistent_key") + + +def test_sysconfig_path_with_config_var() -> None: + result = _CURRENT.sysconfig_path("stdlib", {}) + assert isinstance(result, str) + + +def test_current_system_cached(session_cache: DiskCache) -> None: + PythonInfo._current_system = None + result1 = PythonInfo.current_system(session_cache) + result2 = PythonInfo.current_system(session_cache) + assert result1 is result2 + + +def test_current_cached(session_cache: DiskCache) -> None: + PythonInfo._current = None + result1 = PythonInfo.current(session_cache) + result2 = PythonInfo.current(session_cache) + assert result1 is result2 + + +def test_from_exe_resolve_error(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: + caplog.set_level(logging.INFO) + fake_info = PythonInfo() + fake_info.original_executable = "/fake/python" + mocker.patch( + "python_discovery._cached_py_info.from_exe", + return_value=fake_info, + autospec=True, + ) + mocker.patch.object(PythonInfo, "resolve_to_system", side_effect=RuntimeError("test error"), autospec=True) + result = PythonInfo.from_exe(sys.executable, raise_on_error=False, resolve_to_host=True) + assert result is None + assert any("cannot resolve system" in r.message for r in caplog.records) + + +def test_sysconfig_path_no_config_var() -> None: + result = _CURRENT.sysconfig_path("stdlib") + assert isinstance(result, str) + assert len(result) > 0 + + +def test_satisfies_abs_spec_path_falls_through(tmp_path: Path) -> None: + info = copy.deepcopy(_CURRENT) + spec = PythonSpec("", None, None, None, None, None, str(tmp_path / "other" / "python")) + assert spec.is_abs is True + assert info.satisfies(spec, impl_must_match=False) is True + + +def test_satisfies_abs_spec_path_match() -> None: + info = copy.deepcopy(_CURRENT) + spec = PythonSpec("", None, None, None, None, None, info.executable) + assert info.satisfies(spec, impl_must_match=False) is True + + +def test_current_returns_none_raises(mocker: MockerFixture) -> None: + PythonInfo._current = None + mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) + with pytest.raises(RuntimeError, match="failed to query current Python interpreter"): + PythonInfo.current() + PythonInfo._current = None + + +def test_current_system_returns_none_raises(mocker: MockerFixture) -> None: + PythonInfo._current_system = None + mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) + with pytest.raises(RuntimeError, match="failed to query current system Python interpreter"): + PythonInfo.current_system() + PythonInfo._current_system = None + + +def test_check_exe_from_exe_returns_none(tmp_path: Path, mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + (tmp_path / "python").touch() + mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) + result = info._check_exe( + mocker.create_autospec(PyInfoCache, instance=True), str(tmp_path), "python", [], dict(os.environ), exact=True + ) + assert result is None + + +def test_check_exe_mismatch_not_exact(tmp_path: Path, mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + (tmp_path / "python").touch() + other = copy.deepcopy(_CURRENT) + other.architecture = {32: 64, 64: 32}[info.architecture] + mocker.patch.object(PythonInfo, "from_exe", return_value=other, autospec=True) + discovered: list[PythonInfo] = [] + result = info._check_exe( + mocker.create_autospec(PyInfoCache, instance=True), + str(tmp_path), + "python", + discovered, + dict(os.environ), + exact=False, + ) + assert result is None + assert len(discovered) == 1 + + +def test_check_exe_mismatch_exact(tmp_path: Path, mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + (tmp_path / "python").touch() + other = copy.deepcopy(_CURRENT) + other.architecture = {32: 64, 64: 32}[info.architecture] + mocker.patch.object(PythonInfo, "from_exe", return_value=other, autospec=True) + discovered: list[PythonInfo] = [] + result = info._check_exe( + mocker.create_autospec(PyInfoCache, instance=True), + str(tmp_path), + "python", + discovered, + dict(os.environ), + exact=True, + ) + assert result is None + assert len(discovered) == 0 + + +def test_find_possible_exe_names_free_threaded() -> None: + info = copy.deepcopy(_CURRENT) + info.free_threaded = True + names = info._find_possible_exe_names() + assert any("t" in n for n in names) + + +def test_possible_base_python_basename() -> None: + info = copy.deepcopy(_CURRENT) + info.executable = "/usr/bin/python" + info.implementation = "CPython" + names = list(info._possible_base()) + assert "python" in names + assert "cpython" in names + + +def test_possible_base_case_sensitive(mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + info.executable = "/usr/bin/CPython3.12" + info.implementation = "CPython" + mocker.patch("python_discovery._compat.fs_is_case_sensitive", return_value=True, autospec=True) + names = list(info._possible_base()) + lower_names = [n for n in names if n.islower()] + upper_names = [n for n in names if n.isupper()] + assert len(lower_names) >= 1 + assert len(upper_names) >= 1 + + +def test_possible_base_case_sensitive_upper_equals_base(mocker: MockerFixture) -> None: + info = copy.deepcopy(_CURRENT) + info.executable = "/usr/bin/JYTHON" + info.implementation = "JYTHON" + mocker.patch("python_discovery._compat.fs_is_case_sensitive", return_value=True, autospec=True) + names = list(info._possible_base()) + assert "jython" in names + assert "JYTHON" in names + + +def test_resolve_to_system_resolved_from_exe(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: + caplog.set_level(logging.DEBUG) + target = copy.deepcopy(_CURRENT) + target.system_executable = "/some/system/python" + target.executable = "/some/venv/python" + + resolved = copy.deepcopy(_CURRENT) + resolved.system_executable = "/some/system/python" + resolved.executable = "/some/system/python" + + mocker.patch.object(PythonInfo, "from_exe", return_value=resolved, autospec=True) + result = PythonInfo.resolve_to_system(None, target) + assert result.executable == "/some/venv/python" + + +def test_from_exe_uses_supplied_environment_for_base_probe( + mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("PYTHONPATH", "process-only") + target = copy.deepcopy(_CURRENT) + target.executable = "venv" + target.system_executable = "system" + mapped = copy.deepcopy(_CURRENT) + mapped.executable = mapped.system_executable = "system" + mapped.path = ["mapped"] + process_only = copy.deepcopy(mapped) + process_only.path = ["process-only"] + mocker.patch( + "python_discovery._cached_py_info.from_exe", + side_effect=lambda _cls, _cache, exe, **kwargs: { + "venv": target, + "system": mapped if kwargs["env"] == {"PYTHONPATH": "mapped"} else process_only, + }[exe], + autospec=True, + ) + + info = PythonInfo.from_exe("venv", env={"PYTHONPATH": "mapped"}) + + assert info is not None + assert info.path == ["mapped"] + + +def test_resolve_to_system_uses_supplied_environment_for_prefix_probe(mocker: MockerFixture) -> None: + target = copy.deepcopy(_CURRENT) + target.system_executable = None + target.real_prefix = None + target.base_prefix = "/prefix" + + mapped = copy.deepcopy(_CURRENT) + mapped.path = ["mapped"] + mapped.system_executable = mapped.executable + fallback = copy.deepcopy(_CURRENT) + fallback.path = ["process"] + fallback.system_executable = fallback.executable + mocker.patch.object( + PythonInfo, + "discover_exe", + side_effect=lambda _cls, _cache, **kwargs: ( + mapped if kwargs.get("env") == {"PYTHONPATH": "mapped"} else fallback + ), + autospec=True, + ) + + resolved = PythonInfo.resolve_to_system(None, target, env={"PYTHONPATH": "mapped"}) + + assert resolved.path == ["mapped"] + + +def test_resolve_to_system_from_exe_returns_none(mocker: MockerFixture) -> None: + target = copy.deepcopy(_CURRENT) + target.system_executable = "/some/system/python" + target.executable = "/some/venv/python" + + mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) + result = PythonInfo.resolve_to_system(None, target) + assert result.executable == "/some/venv/python" + + +@pytest.mark.parametrize( + "system_executable", + [ + pytest.param("/some/system/python", id="resolved"), + pytest.param(None, id="discovered"), + ], +) +def test_resolve_to_system_leaves_shared_info_untouched(mocker: MockerFixture, system_executable: str | None) -> None: + shared = copy.deepcopy(_CURRENT) + shared.system_executable = shared.executable = "/some/system/python" + mocker.patch.object(PythonInfo, "from_exe", return_value=shared, autospec=True) + mocker.patch.object(PythonInfo, "discover_exe", return_value=shared, autospec=True) + + results: list[PythonInfo] = [] + for venv in ("/some/venv-a", "/some/venv-b"): + target = copy.deepcopy(_CURRENT) + target.system_executable = system_executable + target.real_prefix, target.base_prefix, target.executable = None, venv, f"{venv}/python" + results.append(PythonInfo.resolve_to_system(None, target)) + + assert [(info.executable, info.system_executable) for info in results] == [ + ("/some/venv-a/python", "/some/system/python"), + ("/some/venv-b/python", "/some/system/python"), + ] + assert shared.executable == "/some/system/python" + + +def test_resolve_to_system_returns_target_when_executable_unchanged() -> None: + target = copy.deepcopy(_CURRENT) + target.system_executable = target.executable = "/some/system/python" + assert PythonInfo.resolve_to_system(None, target) is target + + +_DISCOVERY_CURRENT: Final[PythonInfo] = PythonInfo.current() +_DISCOVERY_THREADED: Final[str] = "t" if _DISCOVERY_CURRENT.free_threaded else "" + + +def _fs_supports_symlink() -> bool: + return sys.platform != "win32" + + +def test_discover_empty_folder(tmp_path: Path, session_cache: DiskCache) -> None: + with pytest.raises(RuntimeError): + _DISCOVERY_CURRENT.discover_exe(session_cache, prefix=str(tmp_path)) + + +def _discover_base_folders() -> tuple[str, ...]: + exe_dir = str(Path(_DISCOVERY_CURRENT.executable).parent) + folders: dict[str, None] = {} + if ( + exe_dir.startswith(_DISCOVERY_CURRENT.prefix) + and ( # pragma: no branch, the branch depends on the host interpreter layout + relative := exe_dir[len(_DISCOVERY_CURRENT.prefix) :].lstrip("/\\") + ) + ): + folders[relative] = None + folders["."] = None + return tuple(folders) + + +_BASE: Final[tuple[str, ...]] = _discover_base_folders() + + +@pytest.mark.skipif(not _fs_supports_symlink(), reason="symlink is not supported") +@pytest.mark.parametrize("suffix", sorted({".exe", ""} & set(EXTENSIONS) if sys.platform == "win32" else [""])) +@pytest.mark.parametrize("into", _BASE) +@pytest.mark.parametrize( + "arch", + [pytest.param(_DISCOVERY_CURRENT.architecture, id="current"), pytest.param("", id="empty")], +) +@pytest.mark.parametrize( + "version", [".".join(str(i) for i in _DISCOVERY_CURRENT.version_info[0:i]) for i in range(3, 0, -1)] +) +@pytest.mark.parametrize( + "impl", + [pytest.param(_DISCOVERY_CURRENT.implementation, id="current"), pytest.param("python", id="python")], +) +def test_discover_ok( + tmp_path: Path, + suffix: str, + impl: str, + version: str, + *, + arch: int | str, + into: str, + caplog: pytest.LogCaptureFixture, + session_cache: DiskCache, +) -> None: + caplog.set_level(logging.DEBUG) + folder = tmp_path / into + folder.mkdir(parents=True, exist_ok=True) + dest = folder / f"{impl}{version}{_DISCOVERY_THREADED}{f'-{arch}' * bool(arch)}{suffix}" + Path(str(dest)).symlink_to(_DISCOVERY_CURRENT.executable) + pyvenv = Path(_DISCOVERY_CURRENT.executable).parents[1] / "pyvenv.cfg" + with contextlib.suppress(FileNotFoundError): + (folder / pyvenv.name).write_text(pyvenv.read_text(encoding="utf-8"), encoding="utf-8") + inside_folder = str(tmp_path) + found = _DISCOVERY_CURRENT.discover_exe(session_cache, inside_folder).executable + dest_str = str(dest) + assert os.path.normcase(found) == os.path.normcase(dest_str) + assert len(caplog.messages) >= 1, caplog.text + assert "get interpreter info via cmd: " in caplog.text + + dest.rename(dest.parent / (f"{dest.name}-1")) + PythonInfo.clear_cache(session_cache) + with pytest.raises(RuntimeError): + _DISCOVERY_CURRENT.discover_exe(session_cache, inside_folder) diff --git a/tests/test_specifier.py b/tests/python_discovery/test_specifier.py similarity index 95% rename from tests/test_specifier.py rename to tests/python_discovery/test_specifier.py index 29f987b..d636f08 100644 --- a/tests/test_specifier.py +++ b/tests/python_discovery/test_specifier.py @@ -40,7 +40,7 @@ def test_version_eq_different() -> None: def test_version_eq_not_implemented() -> None: - result = SimpleVersion.from_string("3.11").__eq__("3.11") # ruff:ignore[unnecessary-dunder-call] the test checks the dunder method itself + result = SimpleVersion.from_string("3.11").__eq__("3.11") # ruff:ignore[unnecessary-dunder-call] test dunder assert result is NotImplemented @@ -65,7 +65,7 @@ def test_version_lt(left: str, right: str, expected: bool) -> None: def test_version_lt_not_implemented() -> None: - result = SimpleVersion.from_string("3.11").__lt__("3.12") # ruff:ignore[unnecessary-dunder-call] the test checks the dunder method itself + result = SimpleVersion.from_string("3.11").__lt__("3.12") # ruff:ignore[unnecessary-dunder-call] test dunder assert result is NotImplemented @@ -79,7 +79,7 @@ def test_version_gt() -> None: def test_version_gt_not_implemented() -> None: - result = SimpleVersion.from_string("3.11").__gt__("3.11") # ruff:ignore[unnecessary-dunder-call] the test checks the dunder method itself + result = SimpleVersion.from_string("3.11").__gt__("3.11") # ruff:ignore[unnecessary-dunder-call] test dunder assert result is NotImplemented @@ -193,7 +193,7 @@ def test_specifier_compatible_release_version_none() -> None: wildcard_precision=None, version=None, ) - assert spec._check_compatible_release(SimpleVersion.from_string("3.12")) is False + assert spec.contains("3.12") is False def test_specifier_eq() -> None: @@ -201,7 +201,7 @@ def test_specifier_eq() -> None: def test_specifier_eq_not_implemented() -> None: - result = SimpleSpecifier.from_string(">=3.12").__eq__(">=3.12") # ruff:ignore[unnecessary-dunder-call] the test checks the dunder method itself + result = SimpleSpecifier.from_string(">=3.12").__eq__(">=3.12") # ruff:ignore[unnecessary-dunder-call] test dunder assert result is NotImplemented @@ -261,7 +261,7 @@ def test_specifier_set_eq() -> None: def test_specifier_set_eq_not_implemented() -> None: - result = SimpleSpecifierSet.from_string(">=3.12").__eq__(">=3.12") # ruff:ignore[unnecessary-dunder-call] the test checks the dunder method itself + result = SimpleSpecifierSet.from_string(">=3.12").__eq__(">=3.12") # ruff:ignore[unnecessary-dunder-call] dunder assert result is NotImplemented diff --git a/tests/test_discovery_extra.py b/tests/test_discovery_extra.py deleted file mode 100644 index d42d64f..0000000 --- a/tests/test_discovery_extra.py +++ /dev/null @@ -1,221 +0,0 @@ -from __future__ import annotations - -import os -import sys -from pathlib import Path -from typing import TYPE_CHECKING, Final - -import pytest - -from python_discovery import DiskCache, get_interpreter -from python_discovery._discovery import ( - IS_WIN, - LazyPathDump, - _active_versions, - _read_python_version_file, - _resolve_shim, - propose_interpreters, -) -from python_discovery._py_spec import PythonSpec - -_PYTHON_NAME: Final[str] = "python.exe" if IS_WIN else "python" - -if TYPE_CHECKING: - from pytest_mock import MockerFixture - - -def test_propose_interpreters_abs_path_oserror(tmp_path: Path) -> None: - results = list(propose_interpreters(PythonSpec.from_string_spec(str(tmp_path / "nonexistent")), [])) - assert results == [] - - -def test_propose_interpreters_try_first_with_valid(session_cache: DiskCache) -> None: - results = list(propose_interpreters(PythonSpec.from_string_spec("python"), [sys.executable], session_cache)) - assert len(results) >= 1 - - -def test_propose_interpreters_try_first_with_missing(tmp_path: Path) -> None: - gen = propose_interpreters(PythonSpec.from_string_spec("python"), [str(tmp_path / "nonexistent")]) - results = [] - for result in gen: # pragma: no branch, the loop runs zero or more times depending on the host - results.append(result) - break - assert len(results) >= 0 - - -def test_propose_interpreters_try_first_with_duplicate(session_cache: DiskCache) -> None: - spec = PythonSpec.from_string_spec("python") - results = list(propose_interpreters(spec, [sys.executable, sys.executable], session_cache)) - set().update([r[0].executable for r in results if r[0] is not None][:2]) - - -def test_propose_interpreters_relative_path( - tmp_path: Path, - monkeypatch: pytest.MonkeyPatch, - session_cache: DiskCache, -) -> None: - Path(sys.executable) - link = tmp_path / _PYTHON_NAME - Path(str(link)).symlink_to(sys.executable) - spec = PythonSpec.from_string_spec(link.name) - spec.path = link.name - monkeypatch.setenv("PATH", str(tmp_path)) - results = list(propose_interpreters(spec, [], session_cache)) - assert len(results) >= 0 - - -def test_lazy_path_dump_basic(tmp_path: Path) -> None: - result = repr(LazyPathDump(0, tmp_path, {})) - assert "PATH[0]" in result - assert "with =>" not in result - - -def test_lazy_path_dump_debug_with_dir(tmp_path: Path) -> None: - (tmp_path / "subdir").mkdir() - result = repr(LazyPathDump(0, tmp_path, {"_VIRTUALENV_DEBUG": "1"})) - assert "subdir" not in result - - -@pytest.mark.skipif(IS_WIN, reason="POSIX test") -def test_lazy_path_dump_debug_non_executable(tmp_path: Path) -> None: - (tmp_path / "not_executable").touch(mode=0o644) - result = repr(LazyPathDump(0, tmp_path, {"_VIRTUALENV_DEBUG": "1"})) - assert "not_executable" not in result - - -def test_lazy_path_dump_debug_oserror(tmp_path: Path, mocker: MockerFixture) -> None: - (tmp_path / "bad_file").touch() - mocker.patch("pathlib.Path.is_dir", side_effect=[False, False], autospec=True) - mocker.patch("pathlib.Path.stat", side_effect=OSError("permission denied"), autospec=True) - repr(LazyPathDump(0, tmp_path, {"_VIRTUALENV_DEBUG": "1"})) - - -def test_active_versions_pyenv_version(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setenv("PYENV_VERSION", "3.12.0:3.11.0") - versions = list(_active_versions(dict(os.environ))) - assert versions == ["3.12.0", "3.11.0"] - - -def test_active_versions_python_version_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.delenv("PYENV_VERSION", raising=False) - (tmp_path / ".python-version").write_text("3.12.0\n", encoding="utf-8") - monkeypatch.chdir(tmp_path) - versions = list(_active_versions(dict(os.environ))) - assert versions == ["3.12.0"] - - -def test_active_versions_global_version_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.delenv("PYENV_VERSION", raising=False) - monkeypatch.setenv("PYENV_ROOT", str(tmp_path)) - workdir = tmp_path / "workdir" - workdir.mkdir() - monkeypatch.chdir(workdir) - (tmp_path / "version").write_text("3.11.0\n", encoding="utf-8") - versions = list(_active_versions(dict(os.environ))) - assert versions == ["3.11.0"] - - -def test_active_versions_no_source(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.delenv("PYENV_VERSION", raising=False) - monkeypatch.delenv("PYENV_ROOT", raising=False) - monkeypatch.chdir(tmp_path) - versions = list(_active_versions(dict(os.environ))) - assert versions == [] - - -def test_read_python_version_file_found(tmp_path: Path) -> None: - (tmp_path / ".python-version").write_text("3.12.0\n# comment\n", encoding="utf-8") - result = _read_python_version_file(str(tmp_path)) - assert result == ["3.12.0"] - - -def test_read_python_version_file_not_found(tmp_path: Path) -> None: - result = _read_python_version_file(str(tmp_path), search_parents=False) - assert result is None - - -def test_read_python_version_file_search_parents(tmp_path: Path) -> None: - (tmp_path / ".python-version").write_text("3.11.0\n", encoding="utf-8") - child = tmp_path / "child" - child.mkdir() - result = _read_python_version_file(str(child)) - assert result == ["3.11.0"] - - -def test_read_python_version_file_direct_path(tmp_path: Path) -> None: - version_file = tmp_path / "version" - version_file.write_text("3.12.0\n", encoding="utf-8") - result = _read_python_version_file(str(version_file), search_parents=False) - assert result == ["3.12.0"] - - -def test_resolve_shim_no_match() -> None: - result = _resolve_shim("/some/random/path", dict(os.environ)) - assert result is None - - -def test_path_exe_finder_returns_callable(tmp_path: Path) -> None: - from python_discovery._discovery import path_exe_finder - - finder = path_exe_finder(PythonSpec.from_string_spec("python3.12")) - assert callable(finder) - results = list(finder(tmp_path)) - assert results == [] - - -def test_get_paths_no_path_env() -> None: - from python_discovery._discovery import get_paths - - paths = list(get_paths({})) - assert isinstance(paths, list) - - -def test_propose_interpreters_abs_path_exists(session_cache: DiskCache) -> None: - results = list(propose_interpreters(PythonSpec.from_string_spec(sys.executable), [], session_cache)) - assert len(results) >= 1 - - -def test_propose_interpreters_relative_spec_is_abs(tmp_path: Path, session_cache: DiskCache) -> None: - link = tmp_path / _PYTHON_NAME - Path(str(link)).symlink_to(sys.executable) - spec = PythonSpec.from_string_spec(str(link)) - spec.path = str(link) - results = list(propose_interpreters(spec, [], session_cache)) - assert len(results) >= 1 - - -def test_resolve_shim_match_no_binary(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - shims = tmp_path / "shims" - shims.mkdir() - (tmp_path / "versions").mkdir() - monkeypatch.setenv("PYENV_ROOT", str(tmp_path)) - result = _resolve_shim(str(shims / "python3"), dict(os.environ)) - assert result is None - - -def test_resolve_shim_dir_no_match(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setenv("PYENV_ROOT", str(tmp_path)) - result = _resolve_shim("/other/dir/python3", dict(os.environ)) - assert result is None - - -def test_read_python_version_file_reaches_root() -> None: - result = _read_python_version_file("/nonexistent/deep/path/that/does/not/exist") - assert result is None - - -def test_read_python_version_file_empty_versions(tmp_path: Path) -> None: - (tmp_path / ".python-version").write_text("# only comments\n\n", encoding="utf-8") - result = _read_python_version_file(str(tmp_path), search_parents=False) - assert result is None - - -def test_get_interpreter_multi_spec_all_fail(session_cache: DiskCache) -> None: - result = get_interpreter(["magic-one", "magic-two"], cache=session_cache) - assert result is None - - -def test_get_interpreter_multi_spec_fallback(session_cache: DiskCache) -> None: - result = get_interpreter(["magic-one", sys.executable], cache=session_cache) - assert result is not None - assert result.executable == sys.executable diff --git a/tests/test_iter_interpreters.py b/tests/test_iter_interpreters.py index 1758b92..677f8f3 100644 --- a/tests/test_iter_interpreters.py +++ b/tests/test_iter_interpreters.py @@ -2,6 +2,7 @@ import contextlib import os +import subprocess import sys from pathlib import Path from typing import TYPE_CHECKING, Final, Protocol @@ -10,9 +11,9 @@ import pytest from python_discovery import DiskCache, PythonInfo, iter_interpreters -from python_discovery._discovery import IS_WIN -_EXE_SUFFIX: Final[str] = ".exe" if IS_WIN else "" +_EXE_SUFFIX: Final[str] = ".exe" if sys.platform == "win32" else "" + if TYPE_CHECKING: from collections.abc import Callable, Iterator @@ -222,7 +223,7 @@ def test_iter_interpreters_skips_when_no_executable( session_cache: DiskCache, mocker: MockerFixture, ) -> None: - bogus = mocker.MagicMock(spec=PythonInfo, system_executable=None, executable=None) + bogus = mocker.create_autospec(PythonInfo, instance=True, system_executable=None, executable=None) mocker.patch( "python_discovery._discovery.propose_interpreters", return_value=iter([(bogus, True)]), @@ -242,7 +243,9 @@ def test_iter_interpreters_uv_yields_when_interrogation_succeeds( bin_path.mkdir(parents=True) (bin_path / "python").touch() - fake_info = mocker.MagicMock(spec=PythonInfo, system_executable=str(uv_dir / "fake-unique" / "python")) + fake_info = mocker.create_autospec( + PythonInfo, instance=True, system_executable=str(uv_dir / "fake-unique" / "python") + ) fake_info.executable = fake_info.system_executable fake_info.satisfies.return_value = True mocker.patch("python_discovery._discovery.PathPythonInfo.from_exe", return_value=fake_info, autospec=True) @@ -508,10 +511,12 @@ def test_iter_interpreters_uv_minor_version_junction_loses_to_its_target( @pytest.fixture def uv_junction(uv_dir: Path, mocker: MockerFixture) -> Callable[[str, str], None]: def _junction(target: str, link: str) -> None: - if IS_WIN: # pragma: win32 cover - import _winapi - - _winapi.CreateJunction(str(uv_dir / target), str(uv_dir / link)) + if sys.platform == "win32": # pragma: win32 cover + subprocess.run( + [os.environ["COMSPEC"], "/c", "mklink", "/J", str(uv_dir / link), str(uv_dir / target)], + check=True, + capture_output=True, + ) else: # pragma: win32 no cover # stand in for the junction with a symlink that does not report as one (uv_dir / link).symlink_to(uv_dir / target) diff --git a/tests/test_py_info_collect.py b/tests/test_py_info_collect.py index c6c4e65..8e26a3b 100644 --- a/tests/test_py_info_collect.py +++ b/tests/test_py_info_collect.py @@ -9,14 +9,13 @@ from pathlib import Path from subprocess import run from types import ModuleType -from typing import TYPE_CHECKING, Final +from typing import TYPE_CHECKING, Final, NamedTuple import pytest import vermin import python_discovery from python_discovery import PythonInfo -from python_discovery._py_info import VersionInfo if TYPE_CHECKING: from collections.abc import Callable @@ -26,6 +25,14 @@ _SCRIPT: Final[Path] = Path(python_discovery.__file__).parent / "_py_info_collect.py" +class _VersionInfo(NamedTuple): + major: int + minor: int + micro: int + releaselevel: str + serial: int + + def test_script_parses_down_to_python27() -> None: py2, py3 = vermin.detect(_SCRIPT.read_text(encoding="utf-8")) assert py2 is not None, "script no longer parses on Python 2.7, the version gate cannot run there" @@ -98,7 +105,8 @@ def no_framework(mocker: MockerFixture) -> None: mocker.patch.object( sysconfig, "get_config_var", - side_effect=lambda name: "" if name == "PYTHONFRAMEWORK" else get_config_var(name), # ty: ignore[deprecated] # resolves the deprecated Literal["SO"] overload even though name is a generic str + # ty: ignore[deprecated] generic keys may include deprecated SO + side_effect=lambda name: "" if name == "PYTHONFRAMEWORK" else get_config_var(name), autospec=True, ) @@ -207,7 +215,8 @@ def test_system_executable_framework_symlink_kept(tmp_path: Path, mocker: Mocker mocker.patch.object( sysconfig, "get_config_var", - side_effect=lambda name: "Python" if name == "PYTHONFRAMEWORK" else get_config_var(name), # ty: ignore[deprecated] # resolves the deprecated Literal["SO"] overload even though name is a generic str + # ty: ignore[deprecated] generic keys may include deprecated SO + side_effect=lambda name: "Python" if name == "PYTHONFRAMEWORK" else get_config_var(name), autospec=True, ) mocker.patch.object(sys, "executable", str(link)) @@ -231,13 +240,13 @@ def _venv_base_executable_is_self(mocker: MockerFixture, _tmp_path: Path) -> Non def _venv_missing_base_before_311(mocker: MockerFixture, tmp_path: Path) -> None: _venv(mocker) mocker.patch.object(sys, "_base_executable", str(tmp_path / "python"), create=True) - mocker.patch.object(sys, "version_info", VersionInfo(3, 9, 0, "final", 0)) + mocker.patch.object(sys, "version_info", _VersionInfo(3, 9, 0, "final", 0)) def _venv_missing_base_no_candidates(mocker: MockerFixture, tmp_path: Path) -> None: _venv(mocker) mocker.patch.object(sys, "_base_executable", str(tmp_path / "python"), create=True) - mocker.patch.object(sys, "version_info", VersionInfo(3, 12, 0, "final", 0)) + mocker.patch.object(sys, "version_info", _VersionInfo(3, 12, 0, "final", 0)) def _venv(mocker: MockerFixture) -> None: @@ -281,7 +290,7 @@ def test_system_executable_prefers_exact_version_for_distinct_file(mocker: Mocke generic.touch() _venv(mocker) mocker.patch.object(sys, "_base_executable", str(generic), create=True) - mocker.patch.object(sys, "version_info", VersionInfo(3, 13, 0, "final", 0)) + mocker.patch.object(sys, "version_info", _VersionInfo(3, 13, 0, "final", 0)) assert PythonInfo().system_executable == str(versioned) @@ -299,7 +308,7 @@ def test_system_executable_prefers_generic_alias_of_exact_version(mocker: Mocker landmark.touch() _venv(mocker) mocker.patch.object(sys, "_base_executable", str(generic), create=True) - mocker.patch.object(sys, "version_info", VersionInfo(3, 13, 0, "final", 0)) + mocker.patch.object(sys, "version_info", _VersionInfo(3, 13, 0, "final", 0)) assert PythonInfo().system_executable == str(generic) @@ -339,16 +348,24 @@ def test_tcl_tk_libs_queried_with_env(tmp_path: Path, mocker: MockerFixture) -> def _fake_tkinter(mocker: MockerFixture, eval_side_effect: Callable[[str], str] | Exception) -> MockType: - evaluate = mocker.MagicMock(spec=lambda *_: None, side_effect=eval_side_effect) - profile = mocker.MagicMock(spec=lambda *_: None, return_value=None) + evaluate = mocker.create_autospec(lambda *_: None, side_effect=eval_side_effect) + profile = mocker.create_autospec(lambda *_: None, return_value=None) interpreter = type( - "Tk", (), {"__init__": lambda self, **_: self.readprofile("Tk", "Tk"), "readprofile": profile, "eval": evaluate} + "Tk", + (), + { + "__init__": lambda self, **_: self.readprofile("Tk", "Tk"), + "readprofile": staticmethod(profile), + "eval": staticmethod(evaluate), + }, ) - module = mocker.MagicMock( - spec=ModuleType, + module = mocker.create_autospec( + ModuleType, + instance=True, TclError=_TclError, + TkVersion=8.6, Tk=interpreter, - Tcl=mocker.MagicMock(spec=lambda: None, side_effect=lambda: interpreter(useTk=False)), + Tcl=mocker.create_autospec(lambda: None, side_effect=lambda: interpreter(useTk=False)), ) mocker.patch.dict(sys.modules, {"tkinter": module}) return profile @@ -359,8 +376,17 @@ class _TclError(Exception): ... -@pytest.mark.parametrize("folder", [pytest.param("tk8.6", id="minor"), pytest.param("tk8", id="major")]) -def test_tk_lib_resolved_without_loading_tk(tmp_path: Path, mocker: MockerFixture, folder: str) -> None: +@pytest.mark.parametrize( + ("folder", "tk_version"), + [ + pytest.param("tk8.6", "8.6", id="minor"), + pytest.param("tk8", "8.6", id="major"), + pytest.param("tk8.10", "8.10", id="two-digit-minor"), + ], +) +def test_tk_lib_resolved_without_loading_tk( + tmp_path: Path, mocker: MockerFixture, folder: str, tk_version: str +) -> None: tcl_dir, tk_dir = tmp_path / "tcl8.6", tmp_path / folder tk_dir.mkdir() (tk_dir / "tk.tcl").touch() @@ -373,7 +399,9 @@ def evaluate(script: str) -> str: raise AssertionError _fake_tkinter(mocker, evaluate) - mocker.patch.dict(sys.modules, {"_tkinter": mocker.MagicMock(spec=ModuleType, TK_VERSION="8.6")}) + mocker.patch.dict( + sys.modules, {"_tkinter": mocker.create_autospec(ModuleType, instance=True, TK_VERSION=tk_version)} + ) mocker.patch.dict(os.environ, {"TCL_LIBRARY": str(tcl_dir)}) info = PythonInfo() diff --git a/tests/test_py_info_extra.py b/tests/test_py_info_extra.py deleted file mode 100644 index 4ec0515..0000000 --- a/tests/test_py_info_extra.py +++ /dev/null @@ -1,593 +0,0 @@ -from __future__ import annotations - -import copy -import logging -import os -import sys -from pathlib import Path -from typing import TYPE_CHECKING, Final - -import pytest - -from python_discovery import DiskCache, PyInfoCache, PythonInfo, PythonSpec, SimpleSpecifierSet -from python_discovery._py_info import VersionInfo - -if TYPE_CHECKING: - from pytest_mock import MockerFixture - -_CURRENT: Final[PythonInfo] = PythonInfo.current_system() - - -def test_py_info_pypy_version(mocker: MockerFixture) -> None: - mocker.patch("platform.python_implementation", return_value="PyPy", autospec=True) - mocker.patch.object(sys, "pypy_version_info", (7, 3, 11, "final", 0), create=True) - info = PythonInfo() - assert (info.implementation, info.pypy_version_info) == ("PyPy", (7, 3, 11, "final", 0)) - - -def test_py_info_canonicalizes_graalvm(mocker: MockerFixture) -> None: - mocker.patch("platform.python_implementation", return_value="GraalVM", autospec=True) - info = PythonInfo() - assert info.implementation == "GraalPy" - - -def test_has_venv_attribute() -> None: - info = PythonInfo() - assert isinstance(info.has_venv, bool) - - -@pytest.mark.skipif(sys.platform == "win32", reason="POSIX only") -@pytest.mark.skipif(bool(_CURRENT.sysconfig_vars.get("PYTHONFRAMEWORK")), reason="framework builds keep recorded path") -def test_from_exe_resolves_executable_only_symlink( # pragma: no cover # skipped on framework interpreter hosts - tmp_path: Path, - session_cache: DiskCache, -) -> None: - system_exe = _CURRENT.system_executable - assert system_exe is not None - link = tmp_path / "python3" - link.symlink_to(system_exe) - info = PythonInfo.from_exe(str(link), session_cache, ignore_cache=True) - assert info is not None - assert info.system_executable is not None - assert Path(info.system_executable).samefile(system_exe) - assert Path(info.system_executable).parent != tmp_path - - -def test_version_str() -> None: - assert _CURRENT.version_str == ".".join(str(i) for i in sys.version_info[:3]) - - -def test_version_release_str() -> None: - assert _CURRENT.version_release_str == ".".join(str(i) for i in sys.version_info[:2]) - - -def test_python_name() -> None: - assert _CURRENT.python_name == f"python{sys.version_info.major}.{sys.version_info.minor}" - - -def test_is_old_virtualenv() -> None: - info = copy.deepcopy(_CURRENT) - info.real_prefix = "/some/prefix" - assert info.is_old_virtualenv is True - info.real_prefix = None - assert info.is_old_virtualenv is False - - -def test_is_venv() -> None: - assert _CURRENT.is_venv == (_CURRENT.base_prefix is not None) - - -def test_system_prefix() -> None: - info = copy.deepcopy(_CURRENT) - info.real_prefix = "/real" - assert info.system_prefix == "/real" - info.real_prefix = None - info.base_prefix = "/base" - assert info.system_prefix == "/base" - - -def test_system_exec_prefix() -> None: - info = copy.deepcopy(_CURRENT) - info.real_prefix = "/real" - assert info.system_exec_prefix == "/real" - info.real_prefix = None - assert info.system_exec_prefix == info.base_exec_prefix or info.exec_prefix - - -def test_repr() -> None: - result = repr(_CURRENT) - assert "PythonInfo" in result - - -def test_str() -> None: - result = str(_CURRENT) - assert "PythonInfo" in result - assert "spec=" in result - - -def test_machine_none_platform() -> None: - info = copy.deepcopy(_CURRENT) - info.sysconfig_platform = None - assert info.machine == "unknown" - - -def test_from_json_round_trip() -> None: - restored = PythonInfo.from_json(_CURRENT.to_json()) - assert (restored.version_info, restored.implementation) == (_CURRENT.version_info, _CURRENT.implementation) - - -def test_from_dict() -> None: - restored = PythonInfo.from_dict(_CURRENT.to_dict()) - assert restored.version_info == _CURRENT.version_info - - -def test_resolve_to_system_circle(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: - caplog.set_level(logging.DEBUG) - target = copy.deepcopy(_CURRENT) - target.system_executable = None - target.real_prefix = None - target.base_prefix = "/prefix_a" - - second = copy.deepcopy(_CURRENT) - second.system_executable = None - second.real_prefix = None - second.base_prefix = "/prefix_b" - - third = copy.deepcopy(_CURRENT) - third.system_executable = None - third.real_prefix = None - third.base_prefix = "/prefix_a" - - mocker.patch.object(PythonInfo, "discover_exe", side_effect=[second, third], autospec=True) - - with pytest.raises(RuntimeError, match="prefixes are causing a circle"): - PythonInfo.resolve_to_system(None, target) - - -def test_resolve_to_system_single_prefix_self_link(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: - caplog.set_level(logging.INFO) - target = copy.deepcopy(_CURRENT) - target.system_executable = None - target.real_prefix = None - target.base_prefix = "/prefix_a" - - second = copy.deepcopy(_CURRENT) - second.system_executable = None - second.real_prefix = None - second.base_prefix = "/prefix_a" - - mocker.patch.object(PythonInfo, "discover_exe", return_value=second, autospec=True) - - result = PythonInfo.resolve_to_system(None, target) - assert result.system_executable is not None - assert any("links back to itself" in r.message for r in caplog.records) - - -def test_resolve_to_system_repeated_prefix_with_parent_segments(tmp_path: Path, mocker: MockerFixture) -> None: - (tmp_path / "child").mkdir() - target = PythonInfo() - target.system_executable = target.real_prefix = None - target.base_prefix = str(tmp_path) - repeated = PythonInfo() - repeated.system_executable = repeated.real_prefix = None - repeated.base_prefix = str(tmp_path / "child" / "..") - mocker.patch.object(PythonInfo, "discover_exe", return_value=repeated, autospec=True) - - result = PythonInfo.resolve_to_system(None, target) - - assert result.system_executable == repeated.executable - - -def test_discover_exe_cache_hit(mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - cached = copy.deepcopy(_CURRENT) - PythonInfo._cache_exe_discovery["/some/prefix", True, info.version_info] = cached - try: - result = info.discover_exe(mocker.MagicMock(spec=PyInfoCache), prefix="/some/prefix", exact=True) - assert result is cached - finally: - del PythonInfo._cache_exe_discovery["/some/prefix", True, info.version_info] - - -def test_discover_exe_cache_keeps_requested_version(tmp_path: Path) -> None: - prefix = _CURRENT.prefix - assert prefix is not None - cache = DiskCache(tmp_path) - PythonInfo.clear_cache(cache) - try: - found = _CURRENT.discover_exe(cache, prefix) - assert found.version_info == _CURRENT.version_info - other = copy.deepcopy(_CURRENT) - other.version_info = other.version_info._replace(major=0) - with pytest.raises(RuntimeError): - other.discover_exe(cache, prefix) - finally: - PythonInfo.clear_cache(cache) - - -def test_check_exe_none_path(tmp_path: Path, mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - result = info._check_exe( - mocker.MagicMock(spec=PyInfoCache), str(tmp_path), "nonexistent", [], dict(os.environ), exact=True - ) - assert result is None - - -def test_satisfies_version_specifier() -> None: - spec = PythonSpec.from_string_spec(f">={sys.version_info.major}.{sys.version_info.minor}") - assert _CURRENT.satisfies(spec, impl_must_match=False) is True - - -def test_satisfies_version_specifier_fails() -> None: - spec = PythonSpec.from_string_spec(f">{sys.version_info.major + 1}") - assert _CURRENT.satisfies(spec, impl_must_match=False) is False - - -@pytest.mark.parametrize( - ("suffix", "expected"), - [ - pytest.param("t", _CURRENT.free_threaded, id="free-threaded"), - pytest.param("", True, id="any-build"), - ], -) -def test_satisfies_version_specifier_free_threaded(suffix: str, expected: bool) -> None: - spec = PythonSpec.from_string_spec(f">={sys.version_info.major}.{sys.version_info.minor}{suffix}") - assert _CURRENT.satisfies(spec, impl_must_match=False) is expected - - -def test_satisfies_version_specifier_unparsable_version() -> None: - specifier = SimpleSpecifierSet.from_string(">=3.x") - spec = PythonSpec(">=3.x", None, None, None, None, None, None, version_specifier=specifier) - assert _CURRENT.satisfies(spec, impl_must_match=False) is False - - -@pytest.mark.parametrize( - ("version_info", "spec_str", "expected"), - [ - pytest.param(VersionInfo(3, 14, 0, "alpha", 1), ">=3.14.0a1", True, id="alpha_match_exact"), - pytest.param(VersionInfo(3, 14, 0, "beta", 1), ">=3.14.0b1", True, id="beta_match_exact"), - pytest.param(VersionInfo(3, 14, 0, "candidate", 1), ">=3.14.0rc1", True, id="rc_match_exact"), - pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15", True, id="prerelease_match_major_minor"), - pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15.0", False, id="prerelease_not_match_full_precision"), - pytest.param(VersionInfo(3, 15, 0, "alpha", 5), "<3.15.0a6", True, id="earlier_prerelease_less_than"), - pytest.param(VersionInfo(3, 15, 0, "alpha", 6), "<3.15.0a6", False, id="prerelease_not_less_than_itself"), - pytest.param(VersionInfo(3, 12, 0, "candidate", 1), "<3.12.0", False, id="prerelease_not_below_final_bound"), - pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15.0a6", True, id="prerelease_match_itself"), - pytest.param(VersionInfo(3, 15, 0, "alpha", 6), ">=3.15.0a7", False, id="prerelease_not_match_later"), - pytest.param(VersionInfo(3, 15, 0, "final", 0), ">=3.15.0a6", True, id="final_greater_than_prerelease"), - pytest.param(VersionInfo(3, 15, 0, "final", 0), "<3.15.0a6", False, id="final_not_less_than_prerelease"), - pytest.param(VersionInfo(3, 15, 0, "final", 0), ">=3.15", True, id="final_match_major_minor"), - pytest.param(VersionInfo(3, 15, 1, "alpha", 1), ">=3.15.0", True, id="later_micro_prerelease_match"), - pytest.param(VersionInfo(3, 13, 7, "final", 0), "~=3.10", True, id="compatible_release_later_minor"), - pytest.param(VersionInfo(4, 0, 0, "final", 0), "~=3.10", False, id="compatible_release_next_major"), - pytest.param(VersionInfo(3, 13, 0, "alpha", 1), "~=3.12.0", False, id="compatible_release_next_minor_alpha"), - ], -) -def test_satisfies_version_specifier_prerelease(version_info: VersionInfo, spec_str: str, expected: bool) -> None: - info = copy.deepcopy(_CURRENT) - info.version_info = version_info - spec = PythonSpec.from_string_spec(spec_str) - assert info.satisfies(spec, impl_must_match=False) is expected - - -def test_satisfies_path_not_abs_basename_match() -> None: - info = copy.deepcopy(_CURRENT) - # stem drops everything from the first dot, so python3.15t arrives as python3 and stops naming a free-threaded - # build; only the Windows .exe belongs off the end. - spec = PythonSpec.from_string_spec(Path(info.original_executable).name.removesuffix(".exe")) - assert info.satisfies(spec, impl_must_match=False) is True - - -def test_satisfies_path_not_abs_basename_no_match() -> None: - info = copy.deepcopy(_CURRENT) - spec = PythonSpec.from_string_spec("completely_different_name") - assert info.satisfies(spec, impl_must_match=False) is False - - -@pytest.mark.skipif(sys.platform == "win32", reason="win32 tested separately") -def test_satisfies_path_win32(mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - mocker.patch.object(sys, "platform", "win32") - info.executable = "/some/path/python.exe" - spec = PythonSpec.from_string_spec("python") - spec.path = "python" - assert info.satisfies(spec, impl_must_match=False) is True - - -def test_install_path() -> None: - assert isinstance(_CURRENT.install_path("purelib"), str) - - -def test_install_path_rejects_format_width() -> None: - info = copy.deepcopy(_CURRENT) - info.distutils_install = {} - info.sysconfig_paths["purelib"] = "{base:>1000000}/lib" - with pytest.raises(ValueError, match="format specifiers"): - info.install_path("purelib") - - -def test_system_include() -> None: - result = _CURRENT.system_include - assert isinstance(result, str) - - -@pytest.mark.parametrize( - ("distutils_install", "existing", "expected"), - [ - pytest.param({}, [], "include", id="no-headers-path"), - pytest.param({"headers": "headers/UNKNOWN"}, ["headers"], "headers", id="headers"), - pytest.param({"headers": "headers/UNKNOWN"}, [], "include", id="headers-missing"), - pytest.param({"headers": "headers/UNKNOWN"}, ["include", "headers"], "include", id="include-present"), - ], -) -def test_system_include_fallback( - tmp_path: Path, distutils_install: dict[str, str], existing: list[str], expected: str -) -> None: - info = copy.deepcopy(_CURRENT) - info.prefix, info.base_prefix, info.real_prefix = str(tmp_path), None, None - info.sysconfig_vars = {"base": str(tmp_path)} - info.sysconfig_paths = {"include": "{base}/include"} - info.distutils_install = distutils_install - for name in existing: - (tmp_path / name).mkdir() - assert info.system_include == str(tmp_path / expected) - - -def test_sysconfig_path_missing_key() -> None: - assert not _CURRENT.sysconfig_path("nonexistent_key") - - -def test_sysconfig_path_with_config_var() -> None: - result = _CURRENT.sysconfig_path("stdlib", {}) - assert isinstance(result, str) - - -def test_current_system_cached(session_cache: DiskCache) -> None: - PythonInfo._current_system = None - result1 = PythonInfo.current_system(session_cache) - result2 = PythonInfo.current_system(session_cache) - assert result1 is result2 - - -def test_current_cached(session_cache: DiskCache) -> None: - PythonInfo._current = None - result1 = PythonInfo.current(session_cache) - result2 = PythonInfo.current(session_cache) - assert result1 is result2 - - -def test_from_exe_resolve_error(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: - caplog.set_level(logging.INFO) - fake_info = PythonInfo() - fake_info.original_executable = "/fake/python" - mocker.patch( - "python_discovery._cached_py_info.from_exe", - return_value=fake_info, - autospec=True, - ) - mocker.patch.object(PythonInfo, "resolve_to_system", side_effect=RuntimeError("test error"), autospec=True) - result = PythonInfo.from_exe(sys.executable, raise_on_error=False, resolve_to_host=True) - assert result is None - assert any("cannot resolve system" in r.message for r in caplog.records) - - -def test_sysconfig_path_no_config_var() -> None: - result = _CURRENT.sysconfig_path("stdlib") - assert isinstance(result, str) - assert len(result) > 0 - - -def test_satisfies_abs_spec_path_falls_through(tmp_path: Path) -> None: - info = copy.deepcopy(_CURRENT) - spec = PythonSpec("", None, None, None, None, None, str(tmp_path / "other" / "python")) - assert spec.is_abs is True - assert info.satisfies(spec, impl_must_match=False) is True - - -def test_satisfies_abs_spec_path_match() -> None: - info = copy.deepcopy(_CURRENT) - spec = PythonSpec("", None, None, None, None, None, info.executable) - assert info.satisfies(spec, impl_must_match=False) is True - - -def test_current_returns_none_raises(mocker: MockerFixture) -> None: - PythonInfo._current = None - mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) - with pytest.raises(RuntimeError, match="failed to query current Python interpreter"): - PythonInfo.current() - PythonInfo._current = None - - -def test_current_system_returns_none_raises(mocker: MockerFixture) -> None: - PythonInfo._current_system = None - mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) - with pytest.raises(RuntimeError, match="failed to query current system Python interpreter"): - PythonInfo.current_system() - PythonInfo._current_system = None - - -def test_check_exe_from_exe_returns_none(tmp_path: Path, mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - (tmp_path / "python").touch() - mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) - result = info._check_exe( - mocker.MagicMock(spec=PyInfoCache), str(tmp_path), "python", [], dict(os.environ), exact=True - ) - assert result is None - - -def test_check_exe_mismatch_not_exact(tmp_path: Path, mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - (tmp_path / "python").touch() - other = copy.deepcopy(_CURRENT) - other.architecture = {32: 64, 64: 32}[info.architecture] - mocker.patch.object(PythonInfo, "from_exe", return_value=other, autospec=True) - discovered: list[PythonInfo] = [] - result = info._check_exe( - mocker.MagicMock(spec=PyInfoCache), str(tmp_path), "python", discovered, dict(os.environ), exact=False - ) - assert result is None - assert len(discovered) == 1 - - -def test_check_exe_mismatch_exact(tmp_path: Path, mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - (tmp_path / "python").touch() - other = copy.deepcopy(_CURRENT) - other.architecture = {32: 64, 64: 32}[info.architecture] - mocker.patch.object(PythonInfo, "from_exe", return_value=other, autospec=True) - discovered: list[PythonInfo] = [] - result = info._check_exe( - mocker.MagicMock(spec=PyInfoCache), str(tmp_path), "python", discovered, dict(os.environ), exact=True - ) - assert result is None - assert len(discovered) == 0 - - -def test_find_possible_exe_names_free_threaded() -> None: - info = copy.deepcopy(_CURRENT) - info.free_threaded = True - names = info._find_possible_exe_names() - assert any("t" in n for n in names) - - -def test_possible_base_python_basename() -> None: - info = copy.deepcopy(_CURRENT) - info.executable = "/usr/bin/python" - info.implementation = "CPython" - names = list(info._possible_base()) - assert "python" in names - assert "cpython" in names - - -def test_possible_base_case_sensitive(mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - info.executable = "/usr/bin/CPython3.12" - info.implementation = "CPython" - mocker.patch("python_discovery._compat.fs_is_case_sensitive", return_value=True, autospec=True) - names = list(info._possible_base()) - lower_names = [n for n in names if n.islower()] - upper_names = [n for n in names if n.isupper()] - assert len(lower_names) >= 1 - assert len(upper_names) >= 1 - - -def test_possible_base_case_sensitive_upper_equals_base(mocker: MockerFixture) -> None: - info = copy.deepcopy(_CURRENT) - info.executable = "/usr/bin/JYTHON" - info.implementation = "JYTHON" - mocker.patch("python_discovery._compat.fs_is_case_sensitive", return_value=True, autospec=True) - names = list(info._possible_base()) - assert "jython" in names - assert "JYTHON" in names - - -def test_resolve_to_system_resolved_from_exe(mocker: MockerFixture, caplog: pytest.LogCaptureFixture) -> None: - caplog.set_level(logging.DEBUG) - target = copy.deepcopy(_CURRENT) - target.system_executable = "/some/system/python" - target.executable = "/some/venv/python" - - resolved = copy.deepcopy(_CURRENT) - resolved.system_executable = "/some/system/python" - resolved.executable = "/some/system/python" - - mocker.patch.object(PythonInfo, "from_exe", return_value=resolved, autospec=True) - result = PythonInfo.resolve_to_system(None, target) - assert result.executable == "/some/venv/python" - - -def test_from_exe_uses_supplied_environment_for_base_probe( - mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setenv("PYTHONPATH", "process-only") - target = copy.deepcopy(_CURRENT) - target.executable = "venv" - target.system_executable = "system" - mapped = copy.deepcopy(_CURRENT) - mapped.executable = mapped.system_executable = "system" - mapped.path = ["mapped"] - process_only = copy.deepcopy(mapped) - process_only.path = ["process-only"] - mocker.patch( - "python_discovery._cached_py_info.from_exe", - side_effect=lambda _cls, _cache, exe, **kwargs: { - "venv": target, - "system": mapped if kwargs["env"] == {"PYTHONPATH": "mapped"} else process_only, - }[exe], - autospec=True, - ) - - info = PythonInfo.from_exe("venv", env={"PYTHONPATH": "mapped"}) - - assert info is not None - assert info.path == ["mapped"] - - -def test_resolve_to_system_uses_supplied_environment_for_prefix_probe(mocker: MockerFixture) -> None: - target = copy.deepcopy(_CURRENT) - target.system_executable = None - target.real_prefix = None - target.base_prefix = "/prefix" - - mapped = copy.deepcopy(_CURRENT) - mapped.path = ["mapped"] - mapped.system_executable = mapped.executable - fallback = copy.deepcopy(_CURRENT) - fallback.path = ["process"] - fallback.system_executable = fallback.executable - mocker.patch.object( - PythonInfo, - "discover_exe", - side_effect=lambda _cls, _cache, **kwargs: ( - mapped if kwargs.get("env") == {"PYTHONPATH": "mapped"} else fallback - ), - autospec=True, - ) - - resolved = PythonInfo.resolve_to_system(None, target, env={"PYTHONPATH": "mapped"}) - - assert resolved.path == ["mapped"] - - -def test_resolve_to_system_from_exe_returns_none(mocker: MockerFixture) -> None: - target = copy.deepcopy(_CURRENT) - target.system_executable = "/some/system/python" - target.executable = "/some/venv/python" - - mocker.patch.object(PythonInfo, "from_exe", return_value=None, autospec=True) - result = PythonInfo.resolve_to_system(None, target) - assert result.executable == "/some/venv/python" - - -@pytest.mark.parametrize( - "system_executable", - [ - pytest.param("/some/system/python", id="resolved"), - pytest.param(None, id="discovered"), - ], -) -def test_resolve_to_system_leaves_shared_info_untouched(mocker: MockerFixture, system_executable: str | None) -> None: - shared = copy.deepcopy(_CURRENT) - shared.system_executable = shared.executable = "/some/system/python" - mocker.patch.object(PythonInfo, "from_exe", return_value=shared, autospec=True) - mocker.patch.object(PythonInfo, "discover_exe", return_value=shared, autospec=True) - - results: list[PythonInfo] = [] - for venv in ("/some/venv-a", "/some/venv-b"): - target = copy.deepcopy(_CURRENT) - target.system_executable = system_executable - target.real_prefix, target.base_prefix, target.executable = None, venv, f"{venv}/python" - results.append(PythonInfo.resolve_to_system(None, target)) - - assert [(info.executable, info.system_executable) for info in results] == [ - ("/some/venv-a/python", "/some/system/python"), - ("/some/venv-b/python", "/some/system/python"), - ] - assert shared.executable == "/some/system/python" - - -def test_resolve_to_system_returns_target_when_executable_unchanged() -> None: - target = copy.deepcopy(_CURRENT) - target.system_executable = target.executable = "/some/system/python" - assert PythonInfo.resolve_to_system(None, target) is target diff --git a/tests/test_py_spec.py b/tests/test_py_spec.py index 5208797..5ef94c6 100644 --- a/tests/test_py_spec.py +++ b/tests/test_py_spec.py @@ -6,9 +6,8 @@ import pytest -from python_discovery import PythonSpec -from python_discovery._py_info import normalize_isa -from python_discovery._specifier import SimpleSpecifierSet as SpecifierSet +from python_discovery import PythonSpec, normalize_isa +from python_discovery import SimpleSpecifierSet as SpecifierSet if TYPE_CHECKING: from pathlib import Path diff --git a/tests/test_py_spec_extra.py b/tests/test_py_spec_extra.py index b4ce4eb..cef7cdc 100644 --- a/tests/test_py_spec_extra.py +++ b/tests/test_py_spec_extra.py @@ -54,7 +54,7 @@ def test_check_version_specifier_none() -> None: def test_get_required_precision_none() -> None: - from python_discovery._specifier import SimpleSpecifier + from python_discovery import SimpleSpecifier specifier = SimpleSpecifier( spec_str=">=3.12", @@ -68,7 +68,7 @@ def test_get_required_precision_none() -> None: def test_get_required_precision_normal() -> None: - from python_discovery._specifier import SimpleSpecifier + from python_discovery import SimpleSpecifier specifier = SimpleSpecifier.from_string(">=3.12.0") assert PythonSpec._get_required_precision(specifier) == 3 diff --git a/tests/test_query_timeout.py b/tests/test_query_timeout.py deleted file mode 100644 index 565f545..0000000 --- a/tests/test_query_timeout.py +++ /dev/null @@ -1,118 +0,0 @@ -from __future__ import annotations - -import logging -import os -import sys -from time import monotonic -from typing import TYPE_CHECKING - -import pytest - -from python_discovery import PythonInfo -from python_discovery._cached_py_info import _collect_query_output - -if TYPE_CHECKING: - from pathlib import Path - - from pytest_mock import MockerFixture - - -@pytest.mark.parametrize( - ("raw", "expected"), - [ - pytest.param(None, 15.0, id="default"), - pytest.param("30", 30.0, id="seconds"), - pytest.param(" 0.5 ", 0.5, id="padded-fraction"), - pytest.param("inf", None, id="inf"), - pytest.param("1e30", None, id="beyond-platform-wait"), - pytest.param("2147483.647", 2147483.647, id="wait-limit"), - pytest.param("2147483.648", None, id="above-wait-limit"), - ], -) -def test_from_exe_query_timeout( - mocker: MockerFixture, caplog: pytest.LogCaptureFixture, raw: str | None, expected: float | None -) -> None: - collect = mocker.patch("python_discovery._cached_py_info._collect_query_output", wraps=_collect_query_output) - env = {key: value for key, value in {**os.environ, "PY_DISCOVERY_TIMEOUT": raw}.items() if value is not None} - result = PythonInfo.from_exe(sys.executable, env=env, ignore_cache=True, resolve_to_host=False) - assert result is not None - assert result.version_info == sys.version_info - assert collect.call_args_list[0].args[1] == expected - assert not caplog.records - - -@pytest.mark.parametrize( - "raw", - [ - pytest.param("", id="empty"), - pytest.param("abc", id="words"), - pytest.param("0x10", id="hex"), - pytest.param("nan", id="nan"), - pytest.param("0", id="zero"), - pytest.param("-1", id="negative"), - pytest.param("-inf", id="negative-inf"), - pytest.param("1e-999", id="underflow"), - ], -) -def test_from_exe_invalid_timeout_warns_once(mocker: MockerFixture, caplog: pytest.LogCaptureFixture, raw: str) -> None: - collect = mocker.patch("python_discovery._cached_py_info._collect_query_output", wraps=_collect_query_output) - env = {**os.environ, "PY_DISCOVERY_TIMEOUT": raw} - for _ in range(2): - result = PythonInfo.from_exe(sys.executable, env=env, ignore_cache=True, resolve_to_host=False) - assert result is not None - assert result.version_info == sys.version_info - assert [call.args[1] for call in collect.call_args_list] == [15.0] * 2 - assert [(record.levelno, record.getMessage()) for record in caplog.records] == [ - (logging.WARNING, f"ignoring PY_DISCOVERY_TIMEOUT={raw!r}, not a positive number of seconds; using 15.0") - ] - - -def test_from_exe_timeout_warning_cache_is_bounded(caplog: pytest.LogCaptureFixture) -> None: - for index in range(129): - result = PythonInfo.from_exe( - sys.executable, - env={**os.environ, "PY_DISCOVERY_TIMEOUT": f"invalid-timeout-{index}"}, - ignore_cache=True, - resolve_to_host=False, - ) - assert result is not None - assert result.version_info == sys.version_info - caplog.clear() - result = PythonInfo.from_exe( - sys.executable, - env={**os.environ, "PY_DISCOVERY_TIMEOUT": "invalid-timeout-128"}, - ignore_cache=True, - resolve_to_host=False, - ) - assert result is not None - assert result.version_info == sys.version_info - result = PythonInfo.from_exe( - sys.executable, - env={**os.environ, "PY_DISCOVERY_TIMEOUT": "invalid-timeout-0"}, - ignore_cache=True, - resolve_to_host=False, - ) - assert result is not None - assert result.version_info == sys.version_info - assert [record.getMessage() for record in caplog.records] == [ - "ignoring PY_DISCOVERY_TIMEOUT='invalid-timeout-0', not a positive number of seconds; using 15.0" - ] - - -@pytest.mark.skipif(sys.platform == "win32", reason="POSIX shell launcher") -def test_from_exe_timeout_with_inherited_pipes(tmp_path: Path) -> None: - executable = tmp_path / "python3.11" - executable.write_text("#!/bin/sh\n/bin/sleep 3 &\nexit 0\n") - executable.chmod(0o755) - - started = monotonic() - result = PythonInfo.from_exe( - str(executable), - env={**os.environ, "PY_DISCOVERY_TIMEOUT": "0.1"}, - ignore_cache=True, - raise_on_error=False, - resolve_to_host=False, - ) - - assert result is None - assert monotonic() - started < 2.5 diff --git a/tests/windows/test_windows.py b/tests/windows/test_windows.py deleted file mode 100644 index 15dd58a..0000000 --- a/tests/windows/test_windows.py +++ /dev/null @@ -1,62 +0,0 @@ -from __future__ import annotations - -import sys -from pathlib import Path - -import pytest - -from python_discovery import PythonSpec, get_interpreter - - -@pytest.mark.skipif(sys.platform != "win32", reason="Windows executable alias") -def test_discovery_via_path_literal_symlink(tmp_path: Path) -> None: - link = tmp_path / "python_custom.exe" - link.symlink_to(Path(sys.base_prefix) / "python.exe") - - interpreter = get_interpreter("python_custom", [], env={"PATH": f"{tmp_path};{sys.base_prefix}"}) - - assert interpreter is not None - assert interpreter.executable == str(link) - - -@pytest.mark.skipif(sys.platform != "win32", reason="propose_interpreters calls from_exe with Windows paths") -@pytest.mark.usefixtures("_mock_registry") -@pytest.mark.usefixtures("_populate_pyinfo_cache") -@pytest.mark.parametrize( - ("string_spec", "expected_exe"), - [ - pytest.param("python3.10", "C:\\Users\\user\\Miniconda3-64\\python.exe", id="python3.10"), - pytest.param("cpython3.10", "C:\\Users\\user\\Miniconda3-64\\python.exe", id="cpython3.10"), - pytest.param( - "python3.12", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="python3.12" - ), - pytest.param( - "cpython3.12", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="cpython3.12" - ), - pytest.param( - "python", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", id="python" - ), - pytest.param( - "cpython", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", id="cpython" - ), - pytest.param( - "python3", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="python3" - ), - pytest.param( - "cpython3", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python312\\python.exe", id="cpython3" - ), - pytest.param("python3.6", "Z:\\CompanyA\\Python\\3.6\\python.exe", id="python3.6"), - pytest.param("cpython3.6", "Z:\\CompanyA\\Python\\3.6\\python.exe", id="cpython3.6"), - pytest.param("3t", "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", id="3t"), - pytest.param( - "python3.13t", - "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python313\\python3.13t.exe", - id="python3.13t", - ), - ], -) -def test_propose_interpreters(string_spec: str, expected_exe: str) -> None: - from python_discovery._windows._propose import propose_interpreters - - interpreter = next(propose_interpreters(spec=PythonSpec.from_string_spec(string_spec), cache=None, env={})) - assert interpreter.executable == expected_exe