|
|
99a46d |
From 71de653a714958d6649501b874009bafec74d3f4 Mon Sep 17 00:00:00 2001
|
|
|
99a46d |
From: Philipp A <flying-sheep@web.de>
|
|
|
99a46d |
Date: Tue, 23 Apr 2024 18:46:41 +0200
|
|
|
99a46d |
Subject: [PATCH 1/2] Allow builtin interpreter discovery to find specific
|
|
|
99a46d |
Python versions given a general spec (#2709)
|
|
|
99a46d |
MIME-Version: 1.0
|
|
|
99a46d |
Content-Type: text/plain; charset=UTF-8
|
|
|
99a46d |
Content-Transfer-Encoding: 8bit
|
|
|
99a46d |
|
|
|
99a46d |
Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
|
|
|
99a46d |
|
|
|
99a46d |
Backported to 20.21.1 (without typing improvements).
|
|
|
99a46d |
---
|
|
|
99a46d |
docs/changelog/2709.bugfix.rst | 1 +
|
|
|
99a46d |
src/virtualenv/discovery/builtin.py | 78 +++++++++++++-------------
|
|
|
99a46d |
src/virtualenv/discovery/py_spec.py | 42 ++++++--------
|
|
|
99a46d |
tests/unit/discovery/test_discovery.py | 15 ++++-
|
|
|
99a46d |
4 files changed, 67 insertions(+), 69 deletions(-)
|
|
|
99a46d |
create mode 100644 docs/changelog/2709.bugfix.rst
|
|
|
99a46d |
|
|
|
99a46d |
diff --git a/docs/changelog/2709.bugfix.rst b/docs/changelog/2709.bugfix.rst
|
|
|
99a46d |
new file mode 100644
|
|
|
99a46d |
index 0000000..904da84
|
|
|
99a46d |
--- /dev/null
|
|
|
99a46d |
+++ b/docs/changelog/2709.bugfix.rst
|
|
|
99a46d |
@@ -0,0 +1 @@
|
|
|
99a46d |
+allow builtin discovery to discover specific interpreters (e.g. ``python3.12``) given an unspecific spec (e.g. ``python3``) - by :user:`flying-sheep`.
|
|
|
99a46d |
diff --git a/src/virtualenv/discovery/builtin.py b/src/virtualenv/discovery/builtin.py
|
|
|
99a46d |
index 40320d3..f79b5a5 100644
|
|
|
99a46d |
--- a/src/virtualenv/discovery/builtin.py
|
|
|
99a46d |
+++ b/src/virtualenv/discovery/builtin.py
|
|
|
99a46d |
@@ -1,6 +1,7 @@
|
|
|
99a46d |
import logging
|
|
|
99a46d |
import os
|
|
|
99a46d |
import sys
|
|
|
99a46d |
+from pathlib import Path
|
|
|
99a46d |
|
|
|
99a46d |
from virtualenv.info import IS_WIN
|
|
|
99a46d |
|
|
|
99a46d |
@@ -67,7 +68,7 @@ def get_interpreter(key, try_first_with, app_data=None, env=None):
|
|
|
99a46d |
proposed_paths.add(key)
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
-def propose_interpreters(spec, try_first_with, app_data, env=None):
|
|
|
99a46d |
+def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa: C901, PLR0912
|
|
|
99a46d |
# 0. try with first
|
|
|
99a46d |
env = os.environ if env is None else env
|
|
|
99a46d |
for py_exe in try_first_with:
|
|
|
99a46d |
@@ -101,20 +102,17 @@ def propose_interpreters(spec, try_first_with, app_data, env=None):
|
|
|
99a46d |
for interpreter in propose_interpreters(spec, app_data, env):
|
|
|
99a46d |
yield interpreter, True
|
|
|
99a46d |
# finally just find on path, the path order matters (as the candidates are less easy to control by end user)
|
|
|
99a46d |
- paths = get_paths(env)
|
|
|
99a46d |
tested_exes = set()
|
|
|
99a46d |
- for pos, path in enumerate(paths):
|
|
|
99a46d |
- path_str = str(path)
|
|
|
99a46d |
- logging.debug(LazyPathDump(pos, path_str, env))
|
|
|
99a46d |
- for candidate, match in possible_specs(spec):
|
|
|
99a46d |
- found = check_path(candidate, path_str)
|
|
|
99a46d |
- if found is not None:
|
|
|
99a46d |
- exe = os.path.abspath(found)
|
|
|
99a46d |
- if exe not in tested_exes:
|
|
|
99a46d |
- tested_exes.add(exe)
|
|
|
99a46d |
- interpreter = PathPythonInfo.from_exe(exe, app_data, raise_on_error=False, env=env)
|
|
|
99a46d |
- if interpreter is not None:
|
|
|
99a46d |
- yield interpreter, match
|
|
|
99a46d |
+ find_candidates = path_exe_finder(spec)
|
|
|
99a46d |
+ for pos, path in enumerate(get_paths(env)):
|
|
|
99a46d |
+ logging.debug(LazyPathDump(pos, path, env))
|
|
|
99a46d |
+ for exe, impl_must_match in find_candidates(path):
|
|
|
99a46d |
+ if exe in tested_exes:
|
|
|
99a46d |
+ continue
|
|
|
99a46d |
+ tested_exes.add(exe)
|
|
|
99a46d |
+ interpreter = PathPythonInfo.from_exe(str(exe), app_data, raise_on_error=False, env=env)
|
|
|
99a46d |
+ if interpreter is not None:
|
|
|
99a46d |
+ yield interpreter, impl_must_match
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
def get_paths(env):
|
|
|
99a46d |
@@ -125,14 +123,14 @@ def get_paths(env):
|
|
|
99a46d |
except (AttributeError, ValueError):
|
|
|
99a46d |
path = os.defpath
|
|
|
99a46d |
if not path:
|
|
|
99a46d |
- paths = []
|
|
|
99a46d |
- else:
|
|
|
99a46d |
- paths = [p for p in path.split(os.pathsep) if os.path.exists(p)]
|
|
|
99a46d |
- return paths
|
|
|
99a46d |
+ return None
|
|
|
99a46d |
+ for p in map(Path, path.split(os.pathsep)):
|
|
|
99a46d |
+ if p.exists():
|
|
|
99a46d |
+ yield p
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
class LazyPathDump:
|
|
|
99a46d |
- def __init__(self, pos, path, env):
|
|
|
99a46d |
+ def __init__(self, pos, path, env) -> None:
|
|
|
99a46d |
self.pos = pos
|
|
|
99a46d |
self.path = path
|
|
|
99a46d |
self.env = env
|
|
|
99a46d |
@@ -141,35 +139,35 @@ class LazyPathDump:
|
|
|
99a46d |
content = f"discover PATH[{self.pos}]={self.path}"
|
|
|
99a46d |
if self.env.get("_VIRTUALENV_DEBUG"): # this is the over the board debug
|
|
|
99a46d |
content += " with =>"
|
|
|
99a46d |
- for file_name in os.listdir(self.path):
|
|
|
99a46d |
+ for file_path in self.path.iterdir():
|
|
|
99a46d |
try:
|
|
|
99a46d |
- file_path = os.path.join(self.path, file_name)
|
|
|
99a46d |
- if os.path.isdir(file_path) or not os.access(file_path, os.X_OK):
|
|
|
99a46d |
+ if file_path.is_dir() or not (file_path.stat().st_mode & os.X_OK):
|
|
|
99a46d |
continue
|
|
|
99a46d |
except OSError:
|
|
|
99a46d |
pass
|
|
|
99a46d |
content += " "
|
|
|
99a46d |
- content += file_name
|
|
|
99a46d |
+ content += file_path.name
|
|
|
99a46d |
return content
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
-def check_path(candidate, path):
|
|
|
99a46d |
- _, ext = os.path.splitext(candidate)
|
|
|
99a46d |
- if sys.platform == "win32" and ext != ".exe":
|
|
|
99a46d |
- candidate = candidate + ".exe"
|
|
|
99a46d |
- if os.path.isfile(candidate):
|
|
|
99a46d |
- return candidate
|
|
|
99a46d |
- candidate = os.path.join(path, candidate)
|
|
|
99a46d |
- if os.path.isfile(candidate):
|
|
|
99a46d |
- return candidate
|
|
|
99a46d |
- return None
|
|
|
99a46d |
-
|
|
|
99a46d |
-
|
|
|
99a46d |
-def possible_specs(spec):
|
|
|
99a46d |
- # 4. then maybe it's something exact on PATH - if it was direct lookup implementation no longer counts
|
|
|
99a46d |
- yield spec.str_spec, False
|
|
|
99a46d |
- # 5. or from the spec we can deduce a name on path that matches
|
|
|
99a46d |
- yield from spec.generate_names()
|
|
|
99a46d |
+def path_exe_finder(spec):
|
|
|
99a46d |
+ """Given a spec, return a function that can be called on a path to find all matching files in it."""
|
|
|
99a46d |
+ pat = spec.generate_re(windows=sys.platform == "win32")
|
|
|
99a46d |
+ direct = spec.str_spec
|
|
|
99a46d |
+ if sys.platform == "win32":
|
|
|
99a46d |
+ direct = f"{direct}.exe"
|
|
|
99a46d |
+
|
|
|
99a46d |
+ def path_exes(path):
|
|
|
99a46d |
+ # 4. then maybe it's something exact on PATH - if it was direct lookup implementation no longer counts
|
|
|
99a46d |
+ yield (path / direct), False
|
|
|
99a46d |
+ # 5. or from the spec we can deduce if a name on path matches
|
|
|
99a46d |
+ for exe in path.iterdir():
|
|
|
99a46d |
+ match = pat.fullmatch(exe.name)
|
|
|
99a46d |
+ if match:
|
|
|
99a46d |
+ # the implementation must match when we find “python[ver]”
|
|
|
99a46d |
+ yield exe.absolute(), match["impl"] == "python"
|
|
|
99a46d |
+
|
|
|
99a46d |
+ return path_exes
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
class PathPythonInfo(PythonInfo):
|
|
|
99a46d |
diff --git a/src/virtualenv/discovery/py_spec.py b/src/virtualenv/discovery/py_spec.py
|
|
|
99a46d |
index 103c7ae..cbfdfb8 100644
|
|
|
99a46d |
--- a/src/virtualenv/discovery/py_spec.py
|
|
|
99a46d |
+++ b/src/virtualenv/discovery/py_spec.py
|
|
|
99a46d |
@@ -1,10 +1,9 @@
|
|
|
99a46d |
"""A Python specification is an abstract requirement definition of an interpreter"""
|
|
|
99a46d |
|
|
|
99a46d |
+from __future__ import annotations
|
|
|
99a46d |
+
|
|
|
99a46d |
import os
|
|
|
99a46d |
import re
|
|
|
99a46d |
-from collections import OrderedDict
|
|
|
99a46d |
-
|
|
|
99a46d |
-from virtualenv.info import fs_is_case_sensitive
|
|
|
99a46d |
|
|
|
99a46d |
PATTERN = re.compile(r"^(?P<impl>[a-zA-Z]+)?(?P<version>[0-9.]+)?(?:-(?P<arch>32|64))?$")
|
|
|
99a46d |
|
|
|
99a46d |
@@ -12,7 +11,7 @@ PATTERN = re.compile(r"^(?P<impl>[a-zA-Z]+)?(?P<version>[0-9.]+)?(?:-(?P<arch>32
|
|
|
99a46d |
class PythonSpec:
|
|
|
99a46d |
"""Contains specification about a Python Interpreter"""
|
|
|
99a46d |
|
|
|
99a46d |
- def __init__(self, str_spec, implementation, major, minor, micro, architecture, path):
|
|
|
99a46d |
+ def __init__(self, str_spec, implementation, major, minor, micro, architecture, path) -> None: # noqa: PLR0913
|
|
|
99a46d |
self.str_spec = str_spec
|
|
|
99a46d |
self.implementation = implementation
|
|
|
99a46d |
self.major = major
|
|
|
99a46d |
@@ -22,7 +21,7 @@ class PythonSpec:
|
|
|
99a46d |
self.path = path
|
|
|
99a46d |
|
|
|
99a46d |
@classmethod
|
|
|
99a46d |
- def from_string_spec(cls, string_spec):
|
|
|
99a46d |
+ def from_string_spec(cls, string_spec): # noqa: C901, PLR0912
|
|
|
99a46d |
impl, major, minor, micro, arch, path = None, None, None, None, None, None
|
|
|
99a46d |
if os.path.isabs(string_spec):
|
|
|
99a46d |
path = string_spec
|
|
|
99a46d |
@@ -64,27 +63,18 @@ class PythonSpec:
|
|
|
99a46d |
|
|
|
99a46d |
return cls(string_spec, impl, major, minor, micro, arch, path)
|
|
|
99a46d |
|
|
|
99a46d |
- def generate_names(self):
|
|
|
99a46d |
- impls = OrderedDict()
|
|
|
99a46d |
- if self.implementation:
|
|
|
99a46d |
- # first consider implementation as it is
|
|
|
99a46d |
- impls[self.implementation] = False
|
|
|
99a46d |
- if fs_is_case_sensitive():
|
|
|
99a46d |
- # for case sensitive file systems consider lower and upper case versions too
|
|
|
99a46d |
- # trivia: MacBooks and all pre 2018 Windows-es were case insensitive by default
|
|
|
99a46d |
- impls[self.implementation.lower()] = False
|
|
|
99a46d |
- impls[self.implementation.upper()] = False
|
|
|
99a46d |
- impls["python"] = True # finally consider python as alias, implementation must match now
|
|
|
99a46d |
- version = self.major, self.minor, self.micro
|
|
|
99a46d |
- try:
|
|
|
99a46d |
- version = version[: version.index(None)]
|
|
|
99a46d |
- except ValueError:
|
|
|
99a46d |
- pass
|
|
|
99a46d |
- for impl, match in impls.items():
|
|
|
99a46d |
- for at in range(len(version), -1, -1):
|
|
|
99a46d |
- cur_ver = version[0:at]
|
|
|
99a46d |
- spec = f"{impl}{'.'.join(str(i) for i in cur_ver)}"
|
|
|
99a46d |
- yield spec, match
|
|
|
99a46d |
+ def generate_re(self, *, windows):
|
|
|
99a46d |
+ """Generate a regular expression for matching against a filename."""
|
|
|
99a46d |
+ version = r"{}(\.{}(\.{})?)?".format(
|
|
|
99a46d |
+ *(r"\d+" if v is None else v for v in (self.major, self.minor, self.micro))
|
|
|
99a46d |
+ )
|
|
|
99a46d |
+ impl = "python" if self.implementation is None else f"python|{re.escape(self.implementation)}"
|
|
|
99a46d |
+ suffix = r"\.exe" if windows else ""
|
|
|
99a46d |
+ # Try matching `direct` first, so the `direct` group is filled when possible.
|
|
|
99a46d |
+ return re.compile(
|
|
|
99a46d |
+ rf"(?P<impl>{impl})(?P<v>{version}){suffix}$",
|
|
|
99a46d |
+ flags=re.IGNORECASE,
|
|
|
99a46d |
+ )
|
|
|
99a46d |
|
|
|
99a46d |
@property
|
|
|
99a46d |
def is_abs(self):
|
|
|
99a46d |
diff --git a/tests/unit/discovery/test_discovery.py b/tests/unit/discovery/test_discovery.py
|
|
|
99a46d |
index c4a2cc7..51bae24 100644
|
|
|
99a46d |
--- a/tests/unit/discovery/test_discovery.py
|
|
|
99a46d |
+++ b/tests/unit/discovery/test_discovery.py
|
|
|
99a46d |
@@ -14,16 +14,25 @@ from virtualenv.info import fs_supports_symlink
|
|
|
99a46d |
|
|
|
99a46d |
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
|
|
|
99a46d |
@pytest.mark.parametrize("case", ["mixed", "lower", "upper"])
|
|
|
99a46d |
-def test_discovery_via_path(monkeypatch, case, tmp_path, caplog, session_app_data):
|
|
|
99a46d |
+@pytest.mark.parametrize("specificity", ["more", "less"])
|
|
|
99a46d |
+def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, session_app_data): # noqa: PLR0913
|
|
|
99a46d |
caplog.set_level(logging.DEBUG)
|
|
|
99a46d |
current = PythonInfo.current_system(session_app_data)
|
|
|
99a46d |
- core = f"somethingVeryCryptic{'.'.join(str(i) for i in current.version_info[0:3])}"
|
|
|
99a46d |
name = "somethingVeryCryptic"
|
|
|
99a46d |
if case == "lower":
|
|
|
99a46d |
name = name.lower()
|
|
|
99a46d |
elif case == "upper":
|
|
|
99a46d |
name = name.upper()
|
|
|
99a46d |
- exe_name = f"{name}{current.version_info.major}{'.exe' if sys.platform == 'win32' else ''}"
|
|
|
99a46d |
+ if specificity == "more":
|
|
|
99a46d |
+ # e.g. spec: python3, exe: /bin/python3.12
|
|
|
99a46d |
+ core_ver = current.version_info.major
|
|
|
99a46d |
+ exe_ver = ".".join(str(i) for i in current.version_info[0:2])
|
|
|
99a46d |
+ elif specificity == "less":
|
|
|
99a46d |
+ # e.g. spec: python3.12.1, exe: /bin/python3
|
|
|
99a46d |
+ core_ver = ".".join(str(i) for i in current.version_info[0:3])
|
|
|
99a46d |
+ exe_ver = current.version_info.major
|
|
|
99a46d |
+ core = f"somethingVeryCryptic{core_ver}"
|
|
|
99a46d |
+ exe_name = f"{name}{exe_ver}{'.exe' if sys.platform == 'win32' else ''}"
|
|
|
99a46d |
target = tmp_path / current.install_path("scripts")
|
|
|
99a46d |
target.mkdir(parents=True)
|
|
|
99a46d |
executable = target / exe_name
|
|
|
99a46d |
--
|
|
|
99a46d |
2.45.2
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
From 4bb73d175614aa6041b1e9e57d7302b9c253b5ef Mon Sep 17 00:00:00 2001
|
|
|
99a46d |
From: Ofek Lev <ofekmeister@gmail.com>
|
|
|
99a46d |
Date: Sat, 27 Apr 2024 14:43:00 -0400
|
|
|
99a46d |
Subject: [PATCH 2/2] Fix PATH-based Python discovery on Windows (#2712)
|
|
|
99a46d |
|
|
|
99a46d |
---
|
|
|
99a46d |
docs/changelog/2712.bugfix.rst | 1 +
|
|
|
99a46d |
src/virtualenv/discovery/builtin.py | 44 ++++++++++++++++++++------
|
|
|
99a46d |
src/virtualenv/discovery/py_spec.py | 10 +++++-
|
|
|
99a46d |
src/virtualenv/info.py | 5 +++
|
|
|
99a46d |
tests/unit/discovery/test_discovery.py | 8 +++--
|
|
|
99a46d |
5 files changed, 55 insertions(+), 13 deletions(-)
|
|
|
99a46d |
create mode 100644 docs/changelog/2712.bugfix.rst
|
|
|
99a46d |
|
|
|
99a46d |
diff --git a/docs/changelog/2712.bugfix.rst b/docs/changelog/2712.bugfix.rst
|
|
|
99a46d |
new file mode 100644
|
|
|
99a46d |
index 0000000..fee5436
|
|
|
99a46d |
--- /dev/null
|
|
|
99a46d |
+++ b/docs/changelog/2712.bugfix.rst
|
|
|
99a46d |
@@ -0,0 +1 @@
|
|
|
99a46d |
+fix PATH-based Python discovery on Windows - by :user:`ofek`.
|
|
|
99a46d |
diff --git a/src/virtualenv/discovery/builtin.py b/src/virtualenv/discovery/builtin.py
|
|
|
99a46d |
index f79b5a5..456c68b 100644
|
|
|
99a46d |
--- a/src/virtualenv/discovery/builtin.py
|
|
|
99a46d |
+++ b/src/virtualenv/discovery/builtin.py
|
|
|
99a46d |
@@ -3,7 +3,7 @@ import os
|
|
|
99a46d |
import sys
|
|
|
99a46d |
from pathlib import Path
|
|
|
99a46d |
|
|
|
99a46d |
-from virtualenv.info import IS_WIN
|
|
|
99a46d |
+from virtualenv.info import IS_WIN, fs_path_id
|
|
|
99a46d |
|
|
|
99a46d |
from .discover import Discover
|
|
|
99a46d |
from .py_info import PythonInfo
|
|
|
99a46d |
@@ -68,9 +68,10 @@ def get_interpreter(key, try_first_with, app_data=None, env=None):
|
|
|
99a46d |
proposed_paths.add(key)
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
-def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa: C901, PLR0912
|
|
|
99a46d |
+def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa: C901, PLR0912, PLR0915
|
|
|
99a46d |
# 0. try with first
|
|
|
99a46d |
env = os.environ if env is None else env
|
|
|
99a46d |
+ tested_exes: set[str] = set()
|
|
|
99a46d |
for py_exe in try_first_with:
|
|
|
99a46d |
path = os.path.abspath(py_exe)
|
|
|
99a46d |
try:
|
|
|
99a46d |
@@ -78,7 +79,12 @@ def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa
|
|
|
99a46d |
except OSError:
|
|
|
99a46d |
pass
|
|
|
99a46d |
else:
|
|
|
99a46d |
- yield PythonInfo.from_exe(os.path.abspath(path), app_data, env=env), True
|
|
|
99a46d |
+ exe_raw = os.path.abspath(path)
|
|
|
99a46d |
+ exe_id = fs_path_id(exe_raw)
|
|
|
99a46d |
+ if exe_id in tested_exes:
|
|
|
99a46d |
+ continue
|
|
|
99a46d |
+ tested_exes.add(exe_id)
|
|
|
99a46d |
+ yield PythonInfo.from_exe(exe_raw, app_data, env=env), True
|
|
|
99a46d |
|
|
|
99a46d |
# 1. if it's a path and exists
|
|
|
99a46d |
if spec.path is not None:
|
|
|
99a46d |
@@ -88,29 +94,44 @@ def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa
|
|
|
99a46d |
if spec.is_abs:
|
|
|
99a46d |
raise
|
|
|
99a46d |
else:
|
|
|
99a46d |
- yield PythonInfo.from_exe(os.path.abspath(spec.path), app_data, env=env), True
|
|
|
99a46d |
+ exe_raw = os.path.abspath(spec.path)
|
|
|
99a46d |
+ exe_id = fs_path_id(exe_raw)
|
|
|
99a46d |
+ if exe_id not in tested_exes:
|
|
|
99a46d |
+ tested_exes.add(exe_id)
|
|
|
99a46d |
+ yield PythonInfo.from_exe(exe_raw, app_data, env=env), True
|
|
|
99a46d |
if spec.is_abs:
|
|
|
99a46d |
return
|
|
|
99a46d |
else:
|
|
|
99a46d |
# 2. otherwise try with the current
|
|
|
99a46d |
- yield PythonInfo.current_system(app_data), True
|
|
|
99a46d |
+ current_python = PythonInfo.current_system(app_data)
|
|
|
99a46d |
+ exe_raw = str(current_python.executable)
|
|
|
99a46d |
+ exe_id = fs_path_id(exe_raw)
|
|
|
99a46d |
+ if exe_id not in tested_exes:
|
|
|
99a46d |
+ tested_exes.add(exe_id)
|
|
|
99a46d |
+ yield current_python, True
|
|
|
99a46d |
|
|
|
99a46d |
# 3. otherwise fallback to platform default logic
|
|
|
99a46d |
if IS_WIN:
|
|
|
99a46d |
from .windows import propose_interpreters
|
|
|
99a46d |
|
|
|
99a46d |
for interpreter in propose_interpreters(spec, app_data, env):
|
|
|
99a46d |
+ exe_raw = str(interpreter.executable)
|
|
|
99a46d |
+ exe_id = fs_path_id(exe_raw)
|
|
|
99a46d |
+ if exe_id in tested_exes:
|
|
|
99a46d |
+ continue
|
|
|
99a46d |
+ tested_exes.add(exe_id)
|
|
|
99a46d |
yield interpreter, True
|
|
|
99a46d |
# finally just find on path, the path order matters (as the candidates are less easy to control by end user)
|
|
|
99a46d |
- tested_exes = set()
|
|
|
99a46d |
find_candidates = path_exe_finder(spec)
|
|
|
99a46d |
for pos, path in enumerate(get_paths(env)):
|
|
|
99a46d |
logging.debug(LazyPathDump(pos, path, env))
|
|
|
99a46d |
for exe, impl_must_match in find_candidates(path):
|
|
|
99a46d |
- if exe in tested_exes:
|
|
|
99a46d |
+ exe_raw = str(exe)
|
|
|
99a46d |
+ exe_id = fs_path_id(exe_raw)
|
|
|
99a46d |
+ if exe_id in tested_exes:
|
|
|
99a46d |
continue
|
|
|
99a46d |
- tested_exes.add(exe)
|
|
|
99a46d |
- interpreter = PathPythonInfo.from_exe(str(exe), app_data, raise_on_error=False, env=env)
|
|
|
99a46d |
+ tested_exes.add(exe_id)
|
|
|
99a46d |
+ interpreter = PathPythonInfo.from_exe(exe_raw, app_data, raise_on_error=False, env=env)
|
|
|
99a46d |
if interpreter is not None:
|
|
|
99a46d |
yield interpreter, impl_must_match
|
|
|
99a46d |
|
|
|
99a46d |
@@ -159,7 +180,10 @@ def path_exe_finder(spec):
|
|
|
99a46d |
|
|
|
99a46d |
def path_exes(path):
|
|
|
99a46d |
# 4. then maybe it's something exact on PATH - if it was direct lookup implementation no longer counts
|
|
|
99a46d |
- yield (path / direct), False
|
|
|
99a46d |
+ direct_path = path / direct
|
|
|
99a46d |
+ if direct_path.exists():
|
|
|
99a46d |
+ yield direct_path, False
|
|
|
99a46d |
+
|
|
|
99a46d |
# 5. or from the spec we can deduce if a name on path matches
|
|
|
99a46d |
for exe in path.iterdir():
|
|
|
99a46d |
match = pat.fullmatch(exe.name)
|
|
|
99a46d |
diff --git a/src/virtualenv/discovery/py_spec.py b/src/virtualenv/discovery/py_spec.py
|
|
|
99a46d |
index cbfdfb8..04b63b8 100644
|
|
|
99a46d |
--- a/src/virtualenv/discovery/py_spec.py
|
|
|
99a46d |
+++ b/src/virtualenv/discovery/py_spec.py
|
|
|
99a46d |
@@ -70,9 +70,17 @@ class PythonSpec:
|
|
|
99a46d |
)
|
|
|
99a46d |
impl = "python" if self.implementation is None else f"python|{re.escape(self.implementation)}"
|
|
|
99a46d |
suffix = r"\.exe" if windows else ""
|
|
|
99a46d |
+ version_conditional = (
|
|
|
99a46d |
+ "?"
|
|
|
99a46d |
+ # Windows Python executables are almost always unversioned
|
|
|
99a46d |
+ if windows
|
|
|
99a46d |
+ # Spec is an empty string
|
|
|
99a46d |
+ or self.major is None
|
|
|
99a46d |
+ else ""
|
|
|
99a46d |
+ )
|
|
|
99a46d |
# Try matching `direct` first, so the `direct` group is filled when possible.
|
|
|
99a46d |
return re.compile(
|
|
|
99a46d |
- rf"(?P<impl>{impl})(?P<v>{version}){suffix}$",
|
|
|
99a46d |
+ rf"(?P<impl>{impl})(?P<v>{version}){version_conditional}{suffix}$",
|
|
|
99a46d |
flags=re.IGNORECASE,
|
|
|
99a46d |
)
|
|
|
99a46d |
|
|
|
99a46d |
diff --git a/src/virtualenv/info.py b/src/virtualenv/info.py
|
|
|
99a46d |
index a4fc4bf..dd96f10 100644
|
|
|
99a46d |
--- a/src/virtualenv/info.py
|
|
|
99a46d |
+++ b/src/virtualenv/info.py
|
|
|
99a46d |
@@ -47,11 +47,16 @@ def fs_supports_symlink():
|
|
|
99a46d |
return _CAN_SYMLINK
|
|
|
99a46d |
|
|
|
99a46d |
|
|
|
99a46d |
+def fs_path_id(path: str) -> str:
|
|
|
99a46d |
+ return path.casefold() if fs_is_case_sensitive() else path
|
|
|
99a46d |
+
|
|
|
99a46d |
+
|
|
|
99a46d |
__all__ = (
|
|
|
99a46d |
"IS_PYPY",
|
|
|
99a46d |
"IS_CPYTHON",
|
|
|
99a46d |
"IS_WIN",
|
|
|
99a46d |
"fs_is_case_sensitive",
|
|
|
99a46d |
+ "fs_path_id",
|
|
|
99a46d |
"fs_supports_symlink",
|
|
|
99a46d |
"ROOT",
|
|
|
99a46d |
"IS_ZIPAPP",
|
|
|
99a46d |
diff --git a/tests/unit/discovery/test_discovery.py b/tests/unit/discovery/test_discovery.py
|
|
|
99a46d |
index 51bae24..6c64b40 100644
|
|
|
99a46d |
--- a/tests/unit/discovery/test_discovery.py
|
|
|
99a46d |
+++ b/tests/unit/discovery/test_discovery.py
|
|
|
99a46d |
@@ -14,7 +14,7 @@ from virtualenv.info import fs_supports_symlink
|
|
|
99a46d |
|
|
|
99a46d |
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
|
|
|
99a46d |
@pytest.mark.parametrize("case", ["mixed", "lower", "upper"])
|
|
|
99a46d |
-@pytest.mark.parametrize("specificity", ["more", "less"])
|
|
|
99a46d |
+@pytest.mark.parametrize("specificity", ["more", "less", "none"])
|
|
|
99a46d |
def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, session_app_data): # noqa: PLR0913
|
|
|
99a46d |
caplog.set_level(logging.DEBUG)
|
|
|
99a46d |
current = PythonInfo.current_system(session_app_data)
|
|
|
99a46d |
@@ -31,7 +31,11 @@ def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, se
|
|
|
99a46d |
# e.g. spec: python3.12.1, exe: /bin/python3
|
|
|
99a46d |
core_ver = ".".join(str(i) for i in current.version_info[0:3])
|
|
|
99a46d |
exe_ver = current.version_info.major
|
|
|
99a46d |
- core = f"somethingVeryCryptic{core_ver}"
|
|
|
99a46d |
+ elif specificity == "none":
|
|
|
99a46d |
+ # e.g. spec: python3.12.1, exe: /bin/python
|
|
|
99a46d |
+ core_ver = ".".join(str(i) for i in current.version_info[0:3])
|
|
|
99a46d |
+ exe_ver = ""
|
|
|
99a46d |
+ core = "" if specificity == "none" else f"{name}{core_ver}"
|
|
|
99a46d |
exe_name = f"{name}{exe_ver}{'.exe' if sys.platform == 'win32' else ''}"
|
|
|
99a46d |
target = tmp_path / current.install_path("scripts")
|
|
|
99a46d |
target.mkdir(parents=True)
|
|
|
99a46d |
--
|
|
|
99a46d |
2.45.2
|
|
|
99a46d |
|