python-discovery 1.9.0__py3-none-any.whl → 1.9.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_discovery/_cache.py +86 -14
- python_discovery/_cached_py_info.py +175 -49
- python_discovery/_discovery.py +58 -29
- python_discovery/_py_info.py +25 -13
- python_discovery/_py_info_collect.py +12 -4
- python_discovery/_py_spec.py +6 -3
- python_discovery/_specifier.py +8 -0
- python_discovery/_windows/_cancel_io.py +51 -0
- python_discovery/_windows/_pep514.py +10 -5
- {python_discovery-1.9.0.dist-info → python_discovery-1.9.2.dist-info}/METADATA +3 -1
- python_discovery-1.9.2.dist-info/RECORD +18 -0
- python_discovery-1.9.0.dist-info/RECORD +0 -17
- {python_discovery-1.9.0.dist-info → python_discovery-1.9.2.dist-info}/WHEEL +0 -0
- {python_discovery-1.9.0.dist-info → python_discovery-1.9.2.dist-info}/licenses/LICENSE +0 -0
python_discovery/_cache.py
CHANGED
|
@@ -4,13 +4,17 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import json
|
|
6
6
|
import logging
|
|
7
|
+
import os
|
|
8
|
+
import stat
|
|
7
9
|
from contextlib import contextmanager, suppress
|
|
8
10
|
from hashlib import sha256
|
|
11
|
+
from os import fsencode
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from tempfile import NamedTemporaryFile
|
|
9
14
|
from typing import TYPE_CHECKING, Final, Protocol, runtime_checkable
|
|
10
15
|
|
|
11
16
|
if TYPE_CHECKING:
|
|
12
17
|
from collections.abc import Generator
|
|
13
|
-
from pathlib import Path
|
|
14
18
|
|
|
15
19
|
_LOGGER: Final[logging.Logger] = logging.getLogger(__name__)
|
|
16
20
|
|
|
@@ -79,23 +83,58 @@ class DiskContentStore:
|
|
|
79
83
|
def read(self) -> dict | None:
|
|
80
84
|
data, bad_format = None, False
|
|
81
85
|
try:
|
|
82
|
-
data =
|
|
83
|
-
except ValueError:
|
|
86
|
+
data, bad_format = self._read_entry()
|
|
87
|
+
except (ValueError, RecursionError):
|
|
84
88
|
bad_format = True
|
|
85
89
|
except OSError:
|
|
86
90
|
_LOGGER.debug("failed to read %s", self._file, exc_info=True)
|
|
91
|
+
bad_format = self._file.is_symlink()
|
|
87
92
|
else:
|
|
88
|
-
|
|
89
|
-
|
|
93
|
+
if not bad_format:
|
|
94
|
+
_LOGGER.debug("got python info from %s", self._file)
|
|
95
|
+
return data
|
|
90
96
|
if bad_format:
|
|
91
97
|
with suppress(OSError):
|
|
92
98
|
self.remove()
|
|
93
99
|
return None
|
|
94
100
|
|
|
101
|
+
def _read_entry(self) -> tuple[dict | None, bool]:
|
|
102
|
+
with open(self._file, encoding="utf-8", opener=self._open_entry) as file:
|
|
103
|
+
status = os.fstat(file.fileno())
|
|
104
|
+
if not stat.S_ISREG(status.st_mode) or (
|
|
105
|
+
hasattr(os, "geteuid") and (status.st_uid not in {os.geteuid(), 0} or status.st_mode & 0o022)
|
|
106
|
+
):
|
|
107
|
+
_LOGGER.warning("ignoring untrusted cache entry at %s", self._file)
|
|
108
|
+
return None, True
|
|
109
|
+
# A 1 MiB query can expand to 12 MiB when supplementary Unicode is escaped.
|
|
110
|
+
if status.st_size > 16 << 20:
|
|
111
|
+
_LOGGER.warning("ignoring oversized cache entry at %s", self._file)
|
|
112
|
+
return None, True
|
|
113
|
+
return json.load(file), False
|
|
114
|
+
|
|
115
|
+
@staticmethod
|
|
116
|
+
def _open_entry(path: str, flags: int) -> int:
|
|
117
|
+
return os.open(path, flags | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0))
|
|
118
|
+
|
|
95
119
|
def write(self, content: dict) -> None:
|
|
96
120
|
self._folder.mkdir(parents=True, exist_ok=True)
|
|
97
|
-
|
|
98
|
-
|
|
121
|
+
temporary: Path | None = None
|
|
122
|
+
try:
|
|
123
|
+
with NamedTemporaryFile(
|
|
124
|
+
mode="w", encoding="utf-8", dir=self._folder, prefix=f".{self._key}.", delete=False
|
|
125
|
+
) as file:
|
|
126
|
+
temporary = Path(file.name)
|
|
127
|
+
# Compact encoding avoids indentation growth and the streaming encoder's recursion limit.
|
|
128
|
+
file.write(json.dumps(content, sort_keys=True))
|
|
129
|
+
temporary.replace(self._file)
|
|
130
|
+
except OSError:
|
|
131
|
+
_LOGGER.debug("failed to write %s", self._file, exc_info=True)
|
|
132
|
+
else:
|
|
133
|
+
_LOGGER.debug("wrote python info at %s", self._file)
|
|
134
|
+
finally:
|
|
135
|
+
if temporary is not None:
|
|
136
|
+
with suppress(OSError):
|
|
137
|
+
temporary.unlink(missing_ok=True)
|
|
99
138
|
|
|
100
139
|
def remove(self) -> None:
|
|
101
140
|
with suppress(OSError):
|
|
@@ -106,9 +145,15 @@ class DiskContentStore:
|
|
|
106
145
|
def locked(self) -> Generator[None]:
|
|
107
146
|
from filelock import FileLock # ruff:ignore[import-outside-top-level]
|
|
108
147
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
148
|
+
self._folder.mkdir(parents=True, exist_ok=True)
|
|
149
|
+
if os.name == "posix":
|
|
150
|
+
group_shared = bool(self._folder.stat().st_mode & stat.S_IWGRP)
|
|
151
|
+
lock_folder = self._folder / "locks-v2"
|
|
152
|
+
lock_folder.mkdir(mode=0o770 if group_shared else 0o700, exist_ok=True)
|
|
153
|
+
lock = FileLock(str(lock_folder / f"{self._key}.lock"), mode=0o660 if group_shared else 0o600)
|
|
154
|
+
else:
|
|
155
|
+
lock = FileLock(str(self._folder / f"{self._key}.lock"))
|
|
156
|
+
with lock:
|
|
112
157
|
yield
|
|
113
158
|
|
|
114
159
|
|
|
@@ -120,25 +165,52 @@ class DiskCache:
|
|
|
120
165
|
"""
|
|
121
166
|
|
|
122
167
|
def __init__(self, root: Path) -> None:
|
|
123
|
-
self._root = root
|
|
168
|
+
self._root = Path(os.path.abspath(root)) # ruff:ignore[os-path-abspath] # resolve() follows untrusted links
|
|
124
169
|
|
|
125
170
|
@property
|
|
126
171
|
def _py_info_dir(self) -> Path:
|
|
127
172
|
return self._root / "py_info" / "4"
|
|
128
173
|
|
|
129
|
-
def py_info(self, path: Path) ->
|
|
174
|
+
def py_info(self, path: Path) -> ContentStore:
|
|
130
175
|
"""
|
|
131
176
|
Return the content store for the interpreter at *path*.
|
|
132
177
|
|
|
133
178
|
:param path: absolute path to a Python executable.
|
|
134
179
|
"""
|
|
135
|
-
|
|
180
|
+
if not self._trusted_folder():
|
|
181
|
+
return NoOpContentStore()
|
|
182
|
+
key = sha256(fsencode(path)).hexdigest()
|
|
136
183
|
return DiskContentStore(self._py_info_dir, key)
|
|
137
184
|
|
|
185
|
+
def _trusted_folder(self) -> bool:
|
|
186
|
+
if not hasattr(os, "geteuid"):
|
|
187
|
+
return True
|
|
188
|
+
user_id = os.geteuid()
|
|
189
|
+
folder = self._py_info_dir
|
|
190
|
+
private = {self._root, folder.parent, folder}
|
|
191
|
+
for candidate in (*reversed(folder.parents), folder):
|
|
192
|
+
try:
|
|
193
|
+
try:
|
|
194
|
+
status = candidate.lstat()
|
|
195
|
+
except FileNotFoundError:
|
|
196
|
+
candidate.mkdir(exist_ok=True)
|
|
197
|
+
status = candidate.lstat()
|
|
198
|
+
except OSError:
|
|
199
|
+
_LOGGER.warning("not caching in %s", candidate, exc_info=True)
|
|
200
|
+
return False
|
|
201
|
+
if (
|
|
202
|
+
not stat.S_ISDIR(status.st_mode)
|
|
203
|
+
or status.st_uid not in {user_id, 0}
|
|
204
|
+
or (status.st_mode & 0o022 and (candidate in private or not status.st_mode & stat.S_ISVTX))
|
|
205
|
+
):
|
|
206
|
+
_LOGGER.warning("not caching in untrusted directory %s", candidate)
|
|
207
|
+
return False
|
|
208
|
+
return True
|
|
209
|
+
|
|
138
210
|
def py_info_clear(self) -> None:
|
|
139
211
|
"""Remove all cached interpreter information."""
|
|
140
212
|
folder = self._py_info_dir
|
|
141
|
-
if folder.exists():
|
|
213
|
+
if folder.exists() and self._trusted_folder():
|
|
142
214
|
for entry in folder.iterdir():
|
|
143
215
|
if entry.suffix == ".json":
|
|
144
216
|
with suppress(OSError):
|
|
@@ -3,29 +3,36 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import hashlib
|
|
6
|
-
import json
|
|
7
6
|
import logging
|
|
8
7
|
import os
|
|
9
8
|
import pkgutil
|
|
10
9
|
import secrets
|
|
11
|
-
import subprocess # ruff:ignore[suspicious-subprocess-import]
|
|
12
10
|
import sys
|
|
13
11
|
import tempfile
|
|
12
|
+
from codecs import getincrementaldecoder
|
|
14
13
|
from collections import OrderedDict
|
|
15
|
-
from contextlib import contextmanager
|
|
14
|
+
from contextlib import contextmanager, suppress
|
|
15
|
+
from dataclasses import dataclass
|
|
16
16
|
from functools import lru_cache
|
|
17
|
+
from io import DEFAULT_BUFFER_SIZE, IncrementalNewlineDecoder
|
|
17
18
|
from pathlib import Path
|
|
19
|
+
from select import select
|
|
18
20
|
from shlex import quote
|
|
19
|
-
from subprocess import Popen, TimeoutExpired # ruff:ignore[suspicious-subprocess-import]
|
|
21
|
+
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired, list2cmdline # ruff:ignore[suspicious-subprocess-import]
|
|
22
|
+
from threading import Event, Thread
|
|
23
|
+
from time import monotonic
|
|
20
24
|
from typing import TYPE_CHECKING, Final
|
|
21
25
|
|
|
22
26
|
from ._cache import NoOpCache
|
|
23
27
|
from ._py_info import PythonInfo
|
|
24
28
|
from ._py_info_collect import MIN_INTERROGATE_VERSION, UNSUPPORTED_EXIT_CODE, UNSUPPORTED_MARKER
|
|
25
29
|
|
|
30
|
+
if sys.platform == "win32": # pragma: win32 cover
|
|
31
|
+
from ._windows._cancel_io import stop_query_reader
|
|
32
|
+
|
|
26
33
|
if TYPE_CHECKING:
|
|
27
34
|
from collections.abc import Generator, Mapping
|
|
28
|
-
from typing import TypedDict
|
|
35
|
+
from typing import IO, TypedDict
|
|
29
36
|
|
|
30
37
|
from ._cache import ContentStore, PyInfoCache
|
|
31
38
|
|
|
@@ -45,11 +52,16 @@ _PY_INFO_SCRIPT: Final[Path] = Path(__file__).resolve().parent / "_py_info_colle
|
|
|
45
52
|
_DEFAULT_QUERY_TIMEOUT: Final[float] = 15.0
|
|
46
53
|
# poll(2) limits milliseconds to a signed C int.
|
|
47
54
|
_MAX_QUERY_TIMEOUT: Final[float] = (2**31 - 1) / 1000
|
|
55
|
+
_MAX_QUERY_OUTPUT: Final[int] = 1 << 20
|
|
48
56
|
|
|
49
57
|
|
|
50
58
|
class _UnsupportedInterpreterError(RuntimeError):
|
|
51
59
|
"""The interpreter is older than the interrogation script supports; retrying cannot help."""
|
|
52
60
|
|
|
61
|
+
def __init__(self, message: str, executable: str | None = None) -> None:
|
|
62
|
+
super().__init__(message)
|
|
63
|
+
self.executable = executable
|
|
64
|
+
|
|
53
65
|
|
|
54
66
|
def from_exe( # ruff:ignore[too-many-arguments]
|
|
55
67
|
cls: type[PythonInfo],
|
|
@@ -62,7 +74,14 @@ def from_exe( # ruff:ignore[too-many-arguments]
|
|
|
62
74
|
executable_args: str | None = None,
|
|
63
75
|
) -> PythonInfo | None:
|
|
64
76
|
env = os.environ if env is None else env
|
|
65
|
-
|
|
77
|
+
if cache is None:
|
|
78
|
+
result = _get_from_cache(cls, cache, exe, env, ignore_cache=ignore_cache, executable_args=executable_args)
|
|
79
|
+
else:
|
|
80
|
+
try:
|
|
81
|
+
result = _get_from_cache(cls, cache, exe, env, ignore_cache=ignore_cache, executable_args=executable_args)
|
|
82
|
+
except OSError:
|
|
83
|
+
_LOGGER.debug("failed to use cache for %s", exe, exc_info=True)
|
|
84
|
+
result = _get_from_cache(cls, None, exe, env, ignore_cache=True, executable_args=executable_args)
|
|
66
85
|
if isinstance(result, Exception):
|
|
67
86
|
if raise_on_error:
|
|
68
87
|
raise result
|
|
@@ -84,13 +103,32 @@ def _get_from_cache( # ruff:ignore[too-many-arguments]
|
|
|
84
103
|
if not ignore_cache and key in _CACHE:
|
|
85
104
|
result = _CACHE[key]
|
|
86
105
|
else:
|
|
87
|
-
result =
|
|
106
|
+
result = _get_via_file_cache(cls, cache, key[0], exe, env, executable_args=executable_args)
|
|
107
|
+
if _cacheable_result(key[0], result):
|
|
108
|
+
_CACHE[key] = result
|
|
109
|
+
else:
|
|
110
|
+
_CACHE.pop(key, None)
|
|
88
111
|
# with launcher arguments the queried binary is not the interpreter, so keep the one the query reported
|
|
89
112
|
if isinstance(result, PythonInfo) and executable_args is None:
|
|
90
113
|
result.executable = exe
|
|
91
114
|
return result
|
|
92
115
|
|
|
93
116
|
|
|
117
|
+
def _cacheable_result(path: Path, result: PythonInfo | Exception) -> bool:
|
|
118
|
+
if isinstance(result, PythonInfo):
|
|
119
|
+
executable = result.original_executable
|
|
120
|
+
elif isinstance(result, _UnsupportedInterpreterError):
|
|
121
|
+
executable = result.executable
|
|
122
|
+
else:
|
|
123
|
+
return False
|
|
124
|
+
if executable is None:
|
|
125
|
+
return False
|
|
126
|
+
try:
|
|
127
|
+
return path.samefile(executable)
|
|
128
|
+
except (OSError, TypeError, ValueError):
|
|
129
|
+
return False
|
|
130
|
+
|
|
131
|
+
|
|
94
132
|
def _get_via_file_cache( # ruff:ignore[too-many-arguments]
|
|
95
133
|
cls: type[PythonInfo],
|
|
96
134
|
cache: PyInfoCache | None,
|
|
@@ -120,26 +158,25 @@ def _get_via_file_cache( # ruff:ignore[too-many-arguments]
|
|
|
120
158
|
}
|
|
121
159
|
with py_info_store.locked():
|
|
122
160
|
cached = _read_cache_entry(cls, py_info_store, entry_meta)
|
|
123
|
-
if
|
|
161
|
+
if cached is not None:
|
|
124
162
|
return cached
|
|
125
|
-
py_info =
|
|
126
|
-
if
|
|
163
|
+
failure, py_info = _run_subprocess(cls, exe, env, executable_args)
|
|
164
|
+
if failure is not None and not isinstance(failure, _UnsupportedInterpreterError):
|
|
165
|
+
_LOGGER.debug("first subprocess attempt failed for %s (%s), retrying", exe, failure)
|
|
127
166
|
failure, py_info = _run_subprocess(cls, exe, env, executable_args)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if isinstance(failure, _UnsupportedInterpreterError):
|
|
132
|
-
_LOGGER.warning("%s", failure) # the verdict is permanent, warn once and remember it
|
|
167
|
+
if isinstance(failure, _UnsupportedInterpreterError):
|
|
168
|
+
_LOGGER.warning("%s", failure)
|
|
169
|
+
if _cacheable_result(path, failure):
|
|
133
170
|
py_info_store.write({**entry_meta, "unsupported": str(failure)})
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
171
|
+
return failure
|
|
172
|
+
if failure is not None:
|
|
173
|
+
return failure
|
|
174
|
+
if py_info is None:
|
|
175
|
+
msg = f"{exe} failed to produce interpreter info"
|
|
176
|
+
return RuntimeError(msg)
|
|
177
|
+
if _cacheable_result(path, py_info):
|
|
178
|
+
py_info_store.write({**entry_meta, "content": py_info.to_dict()})
|
|
179
|
+
return py_info
|
|
143
180
|
|
|
144
181
|
|
|
145
182
|
def _read_cache_entry(
|
|
@@ -151,7 +188,7 @@ def _read_cache_entry(
|
|
|
151
188
|
return None
|
|
152
189
|
if isinstance(data, dict) and all(data.get(key) == value for key, value in entry_meta.items()):
|
|
153
190
|
if isinstance(unsupported := data.get("unsupported"), str):
|
|
154
|
-
return _UnsupportedInterpreterError(unsupported)
|
|
191
|
+
return _UnsupportedInterpreterError(unsupported, entry_meta["path"])
|
|
155
192
|
if isinstance(content := data.get("content"), dict):
|
|
156
193
|
return _load_cached_py_info(cls, py_info_store, content)
|
|
157
194
|
py_info_store.remove()
|
|
@@ -166,7 +203,7 @@ def _load_cached_py_info(
|
|
|
166
203
|
try:
|
|
167
204
|
py_info = cls.from_dict(content.copy())
|
|
168
205
|
stale = (sys_exe := py_info.system_executable) is not None and not Path(sys_exe).is_file()
|
|
169
|
-
except (AttributeError, KeyError, OSError, TypeError):
|
|
206
|
+
except (AttributeError, KeyError, OSError, TypeError, ValueError):
|
|
170
207
|
stale = True
|
|
171
208
|
if stale:
|
|
172
209
|
py_info_store.remove()
|
|
@@ -195,13 +232,11 @@ def _resolve_py_info_script() -> Generator[Path]:
|
|
|
195
232
|
if data is None:
|
|
196
233
|
msg = f"cannot locate {_PY_INFO_SCRIPT.name} for subprocess interrogation"
|
|
197
234
|
raise FileNotFoundError(msg)
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
yield
|
|
203
|
-
finally:
|
|
204
|
-
Path(tmp).unlink()
|
|
235
|
+
# Python prepends the probe script's directory to sys.path, so keep that directory private.
|
|
236
|
+
with tempfile.TemporaryDirectory() as private_dir:
|
|
237
|
+
script: Final[Path] = Path(private_dir) / _PY_INFO_SCRIPT.name
|
|
238
|
+
script.write_bytes(data)
|
|
239
|
+
yield script
|
|
205
240
|
|
|
206
241
|
|
|
207
242
|
def _extract_between_cookies(out: str, start_cookie: str, end_cookie: str) -> tuple[str, str, int, int]:
|
|
@@ -233,7 +268,7 @@ def _run_subprocess(
|
|
|
233
268
|
cmd: list[str] | str = [exe, str(py_info_script), start_cookie, end_cookie]
|
|
234
269
|
if executable_args is not None:
|
|
235
270
|
# PEP 514 ExecutableArguments is raw command line text, placed before the script per the PEP
|
|
236
|
-
cmd = f"{
|
|
271
|
+
cmd = f"{list2cmdline(cmd[:1])} {executable_args} {list2cmdline(cmd[1:])}"
|
|
237
272
|
env = dict(env)
|
|
238
273
|
env.pop("__PYVENV_LAUNCHER__", None)
|
|
239
274
|
env["PYTHONUTF8"] = "1"
|
|
@@ -242,31 +277,31 @@ def _run_subprocess(
|
|
|
242
277
|
process = Popen( # ruff:ignore[subprocess-without-shell-equals-true]
|
|
243
278
|
cmd,
|
|
244
279
|
universal_newlines=True,
|
|
245
|
-
stdin=
|
|
246
|
-
stderr=
|
|
247
|
-
stdout=
|
|
280
|
+
stdin=DEVNULL,
|
|
281
|
+
stderr=PIPE,
|
|
282
|
+
stdout=PIPE,
|
|
248
283
|
env=env,
|
|
249
284
|
encoding="utf-8",
|
|
250
285
|
errors="backslashreplace",
|
|
251
286
|
)
|
|
252
|
-
out, err = process
|
|
253
|
-
code = process.returncode
|
|
254
|
-
except TimeoutExpired:
|
|
255
|
-
process.kill()
|
|
256
|
-
process.communicate()
|
|
257
|
-
out, err, code = "", "timed out", -1
|
|
287
|
+
out, err, code = _collect_query_output(process, timeout)
|
|
258
288
|
except OSError as os_error:
|
|
259
289
|
out, err, code = "", os_error.strerror, os_error.errno
|
|
260
290
|
if code != 0:
|
|
261
291
|
return _query_failure(exe, out, err, code), None
|
|
262
292
|
out, raw_out, out_starts, out_ends = _extract_between_cookies(out, start_cookie, end_cookie)
|
|
293
|
+
if out_starts < 0 or out_ends < 0:
|
|
294
|
+
_LOGGER.warning("subprocess %s omitted query markers; stdout %d chars", exe, len(raw_out))
|
|
295
|
+
msg = f"{exe} returned output without query markers"
|
|
296
|
+
return RuntimeError(msg), None
|
|
263
297
|
try:
|
|
264
298
|
result = cls.from_json(out)
|
|
265
299
|
if executable_args is None:
|
|
266
300
|
result.executable = exe
|
|
267
|
-
except
|
|
301
|
+
except (AttributeError, KeyError, TypeError, ValueError) as exc:
|
|
268
302
|
_LOGGER.warning(
|
|
269
|
-
"subprocess %s returned invalid
|
|
303
|
+
"subprocess %s returned invalid interpreter information; "
|
|
304
|
+
"raw stdout %d chars, start cookie %s, end cookie %s, "
|
|
270
305
|
"parsed output %d chars: %r",
|
|
271
306
|
exe,
|
|
272
307
|
len(raw_out),
|
|
@@ -275,13 +310,102 @@ def _run_subprocess(
|
|
|
275
310
|
len(out),
|
|
276
311
|
out[:200] if out else "<empty>",
|
|
277
312
|
)
|
|
278
|
-
msg = f"{exe} returned invalid
|
|
313
|
+
msg = f"{exe} returned invalid interpreter information (exit code {code}){f', stderr: {err!r}' if err else ''}"
|
|
279
314
|
failure = RuntimeError(msg)
|
|
280
315
|
failure.__cause__ = exc
|
|
281
316
|
return failure, None
|
|
282
317
|
return None, result
|
|
283
318
|
|
|
284
319
|
|
|
320
|
+
def _collect_query_output(process: Popen[str], timeout: float | None) -> tuple[str, str, int]:
|
|
321
|
+
stdout, stderr = process.stdout, process.stderr
|
|
322
|
+
if stdout is None or stderr is None:
|
|
323
|
+
msg = "query output pipes are required"
|
|
324
|
+
raise ValueError(msg)
|
|
325
|
+
out_chunks: list[str] = []
|
|
326
|
+
err_chunks: list[str] = []
|
|
327
|
+
state = _QueryReadState(process=process, overflow=Event(), stop=Event(), read_errors=[])
|
|
328
|
+
|
|
329
|
+
readers = (
|
|
330
|
+
Thread(target=_drain_query_stream, args=(state, stdout, out_chunks), daemon=True),
|
|
331
|
+
Thread(target=_drain_query_stream, args=(state, stderr, err_chunks), daemon=True),
|
|
332
|
+
)
|
|
333
|
+
for reader in readers:
|
|
334
|
+
reader.start()
|
|
335
|
+
if not _query_completed(process, readers, timeout):
|
|
336
|
+
state.stop.set()
|
|
337
|
+
with suppress(OSError):
|
|
338
|
+
process.kill()
|
|
339
|
+
# A descendant may keep the pipes open after the candidate exits.
|
|
340
|
+
process.wait()
|
|
341
|
+
_stop_query_readers(readers)
|
|
342
|
+
return "", "timed out", -1
|
|
343
|
+
if state.read_errors:
|
|
344
|
+
raise state.read_errors[0]
|
|
345
|
+
if state.overflow.is_set():
|
|
346
|
+
return "", f"produced more than {_MAX_QUERY_OUTPUT} characters of output", -1
|
|
347
|
+
return "".join(out_chunks), "".join(err_chunks), process.wait()
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
@dataclass(frozen=True)
|
|
351
|
+
class _QueryReadState:
|
|
352
|
+
process: Popen[str]
|
|
353
|
+
overflow: Event
|
|
354
|
+
stop: Event
|
|
355
|
+
read_errors: list[OSError]
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _stop_query_readers(readers: tuple[Thread, Thread]) -> None:
|
|
359
|
+
for reader in readers:
|
|
360
|
+
if sys.platform == "win32": # pragma: win32 cover
|
|
361
|
+
stop_query_reader(reader)
|
|
362
|
+
else:
|
|
363
|
+
reader.join()
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def _query_completed(process: Popen[str], readers: tuple[Thread, Thread], timeout: float | None) -> bool:
|
|
367
|
+
deadline = None if timeout is None else monotonic() + timeout
|
|
368
|
+
for reader in readers:
|
|
369
|
+
reader.join(None if deadline is None else max(deadline - monotonic(), 0))
|
|
370
|
+
if any(reader.is_alive() for reader in readers):
|
|
371
|
+
return False
|
|
372
|
+
try:
|
|
373
|
+
process.wait(timeout=None if deadline is None else max(deadline - monotonic(), 0))
|
|
374
|
+
except TimeoutExpired:
|
|
375
|
+
return False
|
|
376
|
+
return True
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def _drain_query_stream(state: _QueryReadState, stream: IO[str], chunks: list[str]) -> None:
|
|
380
|
+
size = 0
|
|
381
|
+
failed = False
|
|
382
|
+
decoder = IncrementalNewlineDecoder(getincrementaldecoder("utf-8")("backslashreplace"), translate=True)
|
|
383
|
+
with stream:
|
|
384
|
+
while not state.stop.is_set():
|
|
385
|
+
try:
|
|
386
|
+
if sys.platform != "win32" and not select([stream], [], [], 0.05)[0]: # pragma: win32 cover
|
|
387
|
+
continue
|
|
388
|
+
data = os.read(stream.fileno(), DEFAULT_BUFFER_SIZE)
|
|
389
|
+
except OSError as error:
|
|
390
|
+
if not state.stop.is_set(): # pragma: win32 cover # canceled ReadFile raises on Windows
|
|
391
|
+
state.read_errors.append(error)
|
|
392
|
+
failed = True
|
|
393
|
+
break
|
|
394
|
+
chunk = decoder.decode(data, final=not data)
|
|
395
|
+
if chunk:
|
|
396
|
+
chunks.append(chunk[: _MAX_QUERY_OUTPUT + 1 - size])
|
|
397
|
+
size += len(chunk)
|
|
398
|
+
if size > _MAX_QUERY_OUTPUT:
|
|
399
|
+
state.overflow.set()
|
|
400
|
+
failed = True
|
|
401
|
+
break
|
|
402
|
+
if not data:
|
|
403
|
+
break
|
|
404
|
+
if failed:
|
|
405
|
+
with suppress(OSError):
|
|
406
|
+
state.process.kill()
|
|
407
|
+
|
|
408
|
+
|
|
285
409
|
@lru_cache(maxsize=128)
|
|
286
410
|
def _query_timeout(raw: str | None) -> float | None:
|
|
287
411
|
if raw is None:
|
|
@@ -302,10 +426,12 @@ def _query_failure(exe: str, out: str, err: str | None, code: int | None) -> Run
|
|
|
302
426
|
# the gate's exit code and stderr marker must both be present: either alone can collide, an errno of 79 from a
|
|
303
427
|
# failed exec or a shim that echoes the marker phrase, but only the gate produces the combination
|
|
304
428
|
if code == UNSUPPORTED_EXIT_CODE and err is not None and (at := err.find(UNSUPPORTED_MARKER)) != -1:
|
|
305
|
-
|
|
429
|
+
details = err[at + len(UNSUPPORTED_MARKER) :].splitlines()
|
|
430
|
+
version = details[0].split() if details else []
|
|
306
431
|
floor = ".".join(str(i) for i in MIN_INTERROGATE_VERSION)
|
|
307
|
-
|
|
308
|
-
|
|
432
|
+
found = version[0] if version else "unknown"
|
|
433
|
+
msg = f"{exe} is Python {found}, older than the minimum {floor} python-discovery can query"
|
|
434
|
+
return _UnsupportedInterpreterError(msg, details[1].strip() if len(details) > 1 else None)
|
|
309
435
|
msg = f"{exe} with code {code}{f' out: {out!r}' if out else ''}{f' err: {err!r}' if err else ''}"
|
|
310
436
|
return RuntimeError(f"failed to query {msg}")
|
|
311
437
|
|
python_discovery/_discovery.py
CHANGED
|
@@ -5,12 +5,14 @@ import os
|
|
|
5
5
|
import sys
|
|
6
6
|
from contextlib import suppress
|
|
7
7
|
from dataclasses import dataclass
|
|
8
|
+
from itertools import tee
|
|
9
|
+
from os.path import basename
|
|
8
10
|
from pathlib import Path
|
|
9
11
|
from typing import TYPE_CHECKING, Final
|
|
10
12
|
|
|
11
13
|
from ._compat import fs_path_id
|
|
12
14
|
from ._py_info import PythonInfo
|
|
13
|
-
from ._py_spec import PythonSpec
|
|
15
|
+
from ._py_spec import KNOWN_IMPLEMENTATIONS, PythonSpec
|
|
14
16
|
from ._specifier import DC_KW, SimpleVersion
|
|
15
17
|
|
|
16
18
|
if TYPE_CHECKING:
|
|
@@ -41,14 +43,14 @@ def get_interpreter(
|
|
|
41
43
|
(``>=3.10``). When a sequence is given each entry is tried in order.
|
|
42
44
|
:param try_first_with: executables to probe before the normal discovery search.
|
|
43
45
|
:param cache: interpreter metadata cache; when ``None`` results are not cached.
|
|
44
|
-
:param env: environment mapping for ``PATH`` lookup; defaults to :data:`os.environ`.
|
|
46
|
+
:param env: environment mapping for ``PATH`` and uv store lookup; defaults to :data:`os.environ`.
|
|
45
47
|
:param predicate: optional callback applied after an interpreter matches the spec. Return ``True`` to accept the
|
|
46
48
|
interpreter, ``False`` to skip it and continue searching.
|
|
47
49
|
:return: the first matching interpreter, or ``None`` if no match is found.
|
|
48
50
|
"""
|
|
49
51
|
specs = [key] if isinstance(key, str) else key
|
|
50
|
-
for spec_str in specs:
|
|
51
|
-
if result := _find_interpreter(spec_str,
|
|
52
|
+
for spec_str, preferred_paths in zip(specs, tee(try_first_with or (), len(specs))):
|
|
53
|
+
if result := _find_interpreter(spec_str, preferred_paths, cache, env, predicate):
|
|
52
54
|
return result
|
|
53
55
|
return None
|
|
54
56
|
|
|
@@ -73,7 +75,7 @@ def iter_interpreters(
|
|
|
73
75
|
:param try_first_with: executables to probe before the normal discovery search.
|
|
74
76
|
:param cache: interpreter metadata cache; when ``None`` results are not cached. Strongly recommended for
|
|
75
77
|
enumeration, which interrogates every candidate as a subprocess on a cold cache.
|
|
76
|
-
:param env: environment mapping for ``PATH`` lookup; defaults to :data:`os.environ`.
|
|
78
|
+
:param env: environment mapping for ``PATH`` and uv store lookup; defaults to :data:`os.environ`.
|
|
77
79
|
:param predicate: optional filter applied after the spec match; return ``True`` to include the interpreter.
|
|
78
80
|
"""
|
|
79
81
|
if key is None:
|
|
@@ -185,7 +187,7 @@ def propose_interpreters(
|
|
|
185
187
|
:param spec: the parsed interpreter specification to match against.
|
|
186
188
|
:param try_first_with: executable paths to probe before the standard search.
|
|
187
189
|
:param cache: interpreter metadata cache; when ``None`` results are not cached.
|
|
188
|
-
:param env: environment mapping for ``PATH`` lookup; defaults to :data:`os.environ`.
|
|
190
|
+
:param env: environment mapping for ``PATH`` and uv store lookup; defaults to :data:`os.environ`.
|
|
189
191
|
:param all_implementations: when ``True`` and *spec* does not constrain the implementation, also surface
|
|
190
192
|
non-CPython binaries on ``PATH`` and under UV's install directory. Used by enumeration APIs.
|
|
191
193
|
"""
|
|
@@ -211,11 +213,12 @@ def _propose_explicit(
|
|
|
211
213
|
tested_exes: set[str],
|
|
212
214
|
) -> Generator[tuple[PythonInfo | None, bool], None, None]:
|
|
213
215
|
for py_exe in try_first_with:
|
|
214
|
-
if exe_raw := _check_exe(
|
|
216
|
+
if Path(py_exe).exists() and (exe_raw := _check_exe(py_exe, tested_exes)):
|
|
215
217
|
yield PythonInfo.from_exe(exe_raw, cache, env=env), True
|
|
216
218
|
|
|
217
219
|
if spec.path is not None:
|
|
218
|
-
|
|
220
|
+
# Unparsed bare names reach spec.path; only an explicit directory should search the working directory.
|
|
221
|
+
if os.path.dirname(spec.path) and (exe_raw := _check_exe(spec.path, tested_exes)):
|
|
219
222
|
yield PythonInfo.from_exe(exe_raw, cache, env=env), True
|
|
220
223
|
else:
|
|
221
224
|
yield from _propose_current_and_windows(spec, cache, env, tested_exes)
|
|
@@ -287,24 +290,28 @@ def _uv_python_roots(env: Mapping[str, str]) -> Generator[Path, None, None]:
|
|
|
287
290
|
yield Path(install_dir).expanduser()
|
|
288
291
|
return
|
|
289
292
|
for state_dir in _uv_state_dirs(env):
|
|
290
|
-
|
|
293
|
+
if state_dir.is_absolute(): # a relative profile path would search the caller's working directory
|
|
294
|
+
yield state_dir / "python"
|
|
291
295
|
|
|
292
296
|
|
|
293
297
|
def _uv_state_dirs(env: Mapping[str, str]) -> Generator[Path, None, None]:
|
|
294
298
|
"""Yield uv's legacy state directory then its current one, the order uv prefers them in."""
|
|
295
|
-
windows = sys.platform == "win32"
|
|
296
|
-
home = Path(env.get("USERPROFILE" if windows else "HOME")
|
|
299
|
+
windows: Final = sys.platform == "win32"
|
|
300
|
+
home: Final = Path(profile) if (profile := env.get("USERPROFILE" if windows else "HOME")) else None
|
|
297
301
|
if windows:
|
|
298
|
-
app_data = Path(
|
|
299
|
-
|
|
300
|
-
|
|
302
|
+
app_data = Path(roaming) if (roaming := env.get("APPDATA")) else None
|
|
303
|
+
if app_data is None or not app_data.is_absolute():
|
|
304
|
+
app_data = home / "AppData" / "Roaming" if home is not None else None
|
|
305
|
+
if app_data is not None:
|
|
306
|
+
yield app_data / "uv" / "data"
|
|
307
|
+
yield app_data / "uv"
|
|
301
308
|
return
|
|
302
|
-
if sys.platform == "darwin":
|
|
309
|
+
if sys.platform == "darwin" and home is not None:
|
|
303
310
|
yield home / "Library" / "Application Support" / "uv"
|
|
304
311
|
# uv ignores a relative XDG_DATA_HOME, as the XDG specification requires
|
|
305
312
|
if (xdg_data_home := env.get("XDG_DATA_HOME")) and (xdg := Path(xdg_data_home)).is_absolute():
|
|
306
313
|
yield xdg / "uv"
|
|
307
|
-
|
|
314
|
+
elif home is not None:
|
|
308
315
|
yield home / ".local" / "share" / "uv"
|
|
309
316
|
|
|
310
317
|
|
|
@@ -376,8 +383,24 @@ def _uv_version_matches(key: _UvKey, spec: PythonSpec) -> bool:
|
|
|
376
383
|
# GraalPy keys carry the GraalVM version in older uv releases, so only CPython and PyPy state a Python version
|
|
377
384
|
if key.implementation not in {"cpython", "pypy"}:
|
|
378
385
|
return True
|
|
379
|
-
if spec.version_specifier is not None
|
|
380
|
-
|
|
386
|
+
if spec.version_specifier is not None:
|
|
387
|
+
for specifier in spec.version_specifier:
|
|
388
|
+
if specifier.version is None:
|
|
389
|
+
return False
|
|
390
|
+
# Match at the requested precision, as PythonInfo.satisfies does after probing.
|
|
391
|
+
numeric_version = specifier.version_str
|
|
392
|
+
for prefix in ("rc", "b", "a"):
|
|
393
|
+
if prefix in numeric_version:
|
|
394
|
+
numeric_version = numeric_version.split(prefix)[0]
|
|
395
|
+
break
|
|
396
|
+
precision = numeric_version.count(".") + 1
|
|
397
|
+
release = ".".join(str(part) for part in key.version.release[:precision])
|
|
398
|
+
if key.version.pre_type is not None and (
|
|
399
|
+
precision == 3 or specifier.version.pre_type is not None # ruff:ignore[magic-value-comparison]
|
|
400
|
+
):
|
|
401
|
+
release = f"{release}{key.version.pre_type}{key.version.pre_num}"
|
|
402
|
+
if not specifier.contains(release):
|
|
403
|
+
return False
|
|
381
404
|
return all(req is None or our == req for our, req in zip(key.version.release, (spec.major, spec.minor, spec.micro)))
|
|
382
405
|
|
|
383
406
|
|
|
@@ -425,14 +448,10 @@ def get_paths(env: Mapping[str, str]) -> Generator[Path, None, None]:
|
|
|
425
448
|
except (AttributeError, ValueError): # pragma: no cover # Windows only (no confstr)
|
|
426
449
|
path = os.defpath
|
|
427
450
|
if path:
|
|
428
|
-
#
|
|
429
|
-
# same footgun a shell has when PATH is misconfigured - Path("") resolves to it. Since every
|
|
430
|
-
# yielded path here is later searched for interpreters to execute and interrogate, silently
|
|
431
|
-
# including the caller's CWD would let whatever directory virtualenv happens to run from smuggle
|
|
432
|
-
# in a candidate binary.
|
|
451
|
+
# Relative entries can execute an interpreter supplied by the working directory.
|
|
433
452
|
for entry in map(Path, filter(None, path.split(os.pathsep))):
|
|
434
453
|
with suppress(OSError):
|
|
435
|
-
if entry.is_dir() and next(entry.iterdir(), None):
|
|
454
|
+
if entry.is_absolute() and entry.is_dir() and next(entry.iterdir(), None):
|
|
436
455
|
yield entry
|
|
437
456
|
|
|
438
457
|
|
|
@@ -471,18 +490,19 @@ def path_exe_finder(
|
|
|
471
490
|
"""Given a spec, return a function that can be called on a path to find all matching files in it."""
|
|
472
491
|
pat = spec.generate_re(windows=sys.platform == "win32", all_implementations=all_implementations)
|
|
473
492
|
direct = spec.str_spec
|
|
493
|
+
implementation = spec.implementation.lower() if spec.implementation is not None else None
|
|
494
|
+
impl_must_match = implementation in KNOWN_IMPLEMENTATIONS and implementation != "python"
|
|
474
495
|
if sys.platform == "win32": # pragma: win32 cover
|
|
475
496
|
direct = f"{direct}.exe"
|
|
476
497
|
|
|
477
498
|
def path_exes(path: Path) -> Generator[tuple[Path, bool], None, None]:
|
|
478
499
|
direct_path = path / direct
|
|
479
500
|
if direct_path.exists():
|
|
480
|
-
yield direct_path,
|
|
501
|
+
yield direct_path, impl_must_match
|
|
481
502
|
|
|
482
503
|
for exe in path.iterdir():
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
yield exe.absolute(), match["impl"] == "python"
|
|
504
|
+
if pat.fullmatch(exe.name):
|
|
505
|
+
yield exe.absolute(), impl_must_match
|
|
486
506
|
|
|
487
507
|
return path_exes
|
|
488
508
|
|
|
@@ -508,6 +528,9 @@ _VERSION_MANAGER_LAYOUTS: list[tuple[str, tuple[str, ...]]] = [
|
|
|
508
528
|
|
|
509
529
|
def _resolve_shim_to_binary(exe_name: str, versions_dir: str, env: Mapping[str, str]) -> str | None:
|
|
510
530
|
for version in _active_versions(env):
|
|
531
|
+
if version in {"", ".", ".."} or basename(version) != version:
|
|
532
|
+
_LOGGER.debug("ignoring invalid version name %r", version)
|
|
533
|
+
continue
|
|
511
534
|
resolved = os.path.join(versions_dir, version, "bin", exe_name)
|
|
512
535
|
if Path(resolved).is_file() and os.access(resolved, os.X_OK):
|
|
513
536
|
return resolved
|
|
@@ -548,7 +571,13 @@ def _versions_in_file(path: str | Path) -> list[str]:
|
|
|
548
571
|
try:
|
|
549
572
|
# pyenv reads raw bytes, so a byte that is not UTF-8 (a Latin-1 comment, say) must not hide the versions
|
|
550
573
|
with Path(path).open(encoding="utf-8", errors="replace") as fh:
|
|
551
|
-
|
|
574
|
+
# Version files can sit in shared parent directories; bound the in-process read.
|
|
575
|
+
max_chars = 4096
|
|
576
|
+
content = fh.read(max_chars + 1)
|
|
577
|
+
if len(content) > max_chars:
|
|
578
|
+
_LOGGER.debug("ignored oversized version file %s", path)
|
|
579
|
+
return []
|
|
580
|
+
return [v for line in content.split("\n") if (v := line.strip()) and not v.startswith("#")]
|
|
552
581
|
except OSError: # skip a file we cannot read, the same as one that names no version
|
|
553
582
|
_LOGGER.debug("failed to read %s", path, exc_info=True)
|
|
554
583
|
return []
|
python_discovery/_py_info.py
CHANGED
|
@@ -10,7 +10,8 @@ import platform
|
|
|
10
10
|
import sys
|
|
11
11
|
from collections import OrderedDict
|
|
12
12
|
from itertools import product
|
|
13
|
-
from
|
|
13
|
+
from os.path import normcase, realpath
|
|
14
|
+
from string import Formatter, digits
|
|
14
15
|
from typing import TYPE_CHECKING, ClassVar, Final, NamedTuple, TypedDict
|
|
15
16
|
|
|
16
17
|
from ._py_info_collect import PythonInfoCollector
|
|
@@ -135,6 +136,9 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
135
136
|
pattern = self.sysconfig_paths.get(key)
|
|
136
137
|
if pattern is None:
|
|
137
138
|
return ""
|
|
139
|
+
if any(format_spec for _, _, format_spec, _ in Formatter().parse(pattern)):
|
|
140
|
+
msg = f"sysconfig path {key!r} contains format specifiers"
|
|
141
|
+
raise ValueError(msg)
|
|
138
142
|
if config_var is None:
|
|
139
143
|
config_var = self.sysconfig_vars
|
|
140
144
|
else:
|
|
@@ -153,8 +157,9 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
153
157
|
for k, v in self.sysconfig_vars.items()
|
|
154
158
|
},
|
|
155
159
|
)
|
|
156
|
-
|
|
157
|
-
|
|
160
|
+
# An empty headers path would make the fallback resolve to the existing prefix.
|
|
161
|
+
if not os.path.exists(path) and (headers := os.path.dirname(self.install_path("headers"))):
|
|
162
|
+
fallback = os.path.join(self.prefix, headers)
|
|
158
163
|
if os.path.exists(fallback):
|
|
159
164
|
path = fallback
|
|
160
165
|
return path
|
|
@@ -282,7 +287,7 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
282
287
|
return True
|
|
283
288
|
if spec.is_abs:
|
|
284
289
|
return True
|
|
285
|
-
basename = os.path.basename(self.
|
|
290
|
+
basename = os.path.basename(self.executable)
|
|
286
291
|
spec_path = spec.path
|
|
287
292
|
if sys.platform == "win32":
|
|
288
293
|
basename, suffix = os.path.splitext(basename)
|
|
@@ -355,7 +360,7 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
355
360
|
|
|
356
361
|
def to_dict(self) -> dict[str, InfoValue]:
|
|
357
362
|
"""Convert this interpreter information to a plain dictionary."""
|
|
358
|
-
data = {
|
|
363
|
+
data = {name: None if name == "_creators" else value for name, value in vars(self).items()}
|
|
359
364
|
version_info = data["version_info"]
|
|
360
365
|
data["version_info"] = version_info._asdict() if hasattr(version_info, "_asdict") else version_info
|
|
361
366
|
return data
|
|
@@ -419,7 +424,7 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
419
424
|
|
|
420
425
|
if isinstance(proposed, PythonInfo) and resolve_to_host:
|
|
421
426
|
try:
|
|
422
|
-
proposed = proposed.resolve_to_system(cache, proposed)
|
|
427
|
+
proposed = proposed.resolve_to_system(cache, proposed, env=env)
|
|
423
428
|
except Exception as exception:
|
|
424
429
|
if raise_on_error:
|
|
425
430
|
raise
|
|
@@ -444,24 +449,31 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
444
449
|
|
|
445
450
|
:param data: dictionary produced by :meth:`to_dict`.
|
|
446
451
|
"""
|
|
452
|
+
if missing := PythonInfo.__annotations__.keys() - data.keys() - {"_cache_exe_discovery", "pypy_version_info"}:
|
|
453
|
+
msg = f"missing interpreter information: {', '.join(sorted(missing))}"
|
|
454
|
+
raise ValueError(msg)
|
|
447
455
|
data["version_info"] = VersionInfo(**data["version_info"]) # restore this to a named tuple structure
|
|
448
456
|
result = cls.__new__(cls) # skip __init__, data replaces the full state
|
|
449
457
|
result.__dict__ = data.copy()
|
|
450
458
|
return result
|
|
451
459
|
|
|
452
460
|
@classmethod
|
|
453
|
-
def resolve_to_system(
|
|
461
|
+
def resolve_to_system(
|
|
462
|
+
cls, cache: PyInfoCache | None, target: PythonInfo, *, env: Mapping[str, str] | None = None
|
|
463
|
+
) -> PythonInfo:
|
|
454
464
|
"""
|
|
455
465
|
Walk virtualenv/venv prefix chains to find the underlying system interpreter.
|
|
456
466
|
|
|
457
467
|
:param cache: interpreter metadata cache; when ``None`` results are not cached.
|
|
458
468
|
:param target: the interpreter to resolve.
|
|
469
|
+
:param env: environment mapping; defaults to :data:`os.environ`.
|
|
459
470
|
"""
|
|
460
471
|
start_executable = target.executable
|
|
461
472
|
prefixes = OrderedDict()
|
|
462
473
|
while target.system_executable is None:
|
|
463
474
|
prefix = target.real_prefix or target.base_prefix or target.prefix
|
|
464
|
-
|
|
475
|
+
prefix_key = normcase(realpath(prefix))
|
|
476
|
+
if prefix_key in prefixes:
|
|
465
477
|
if len(prefixes) == 1:
|
|
466
478
|
_LOGGER.info("%r links back to itself via prefixes", target)
|
|
467
479
|
target.system_executable = target.executable
|
|
@@ -471,10 +483,10 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
471
483
|
_LOGGER.error("%d: prefix=%s, info=%r", len(prefixes) + 1, prefix, target)
|
|
472
484
|
msg = "prefixes are causing a circle {}".format("|".join(prefixes.keys()))
|
|
473
485
|
raise RuntimeError(msg)
|
|
474
|
-
prefixes[
|
|
475
|
-
target = target.discover_exe(cache, prefix=prefix, exact=False)
|
|
486
|
+
prefixes[prefix_key] = target
|
|
487
|
+
target = target.discover_exe(cache, prefix=prefix, exact=False, env=env)
|
|
476
488
|
if target.executable != target.system_executable:
|
|
477
|
-
resolved = cls.from_exe(target.system_executable, cache)
|
|
489
|
+
resolved = cls.from_exe(target.system_executable, cache, env=env)
|
|
478
490
|
if resolved is not None:
|
|
479
491
|
target = resolved
|
|
480
492
|
if target.executable != start_executable:
|
|
@@ -483,7 +495,7 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
483
495
|
target.executable = start_executable
|
|
484
496
|
return target
|
|
485
497
|
|
|
486
|
-
_cache_exe_discovery: ClassVar[dict[tuple[str, bool], PythonInfo]] = {}
|
|
498
|
+
_cache_exe_discovery: ClassVar[dict[tuple[str, bool, VersionInfo], PythonInfo]] = {}
|
|
487
499
|
|
|
488
500
|
def discover_exe(
|
|
489
501
|
self,
|
|
@@ -501,7 +513,7 @@ class PythonInfo: # ruff:ignore[too-many-public-methods]
|
|
|
501
513
|
:param exact: when ``True``, require an exact version match.
|
|
502
514
|
:param env: environment mapping; defaults to :data:`os.environ`.
|
|
503
515
|
"""
|
|
504
|
-
key = prefix, exact
|
|
516
|
+
key = prefix, exact, self.version_info
|
|
505
517
|
if key in self._cache_exe_discovery and prefix:
|
|
506
518
|
_LOGGER.debug("discover exe from cache %s - exact %s: %r", prefix, exact, self._cache_exe_discovery[key])
|
|
507
519
|
return self._cache_exe_discovery[key]
|
|
@@ -14,7 +14,7 @@ UNSUPPORTED_EXIT_CODE = 79 # first value past the sysexits.h range, so no stdli
|
|
|
14
14
|
UNSUPPORTED_MARKER = "unsupported Python version "
|
|
15
15
|
|
|
16
16
|
if __name__ == "__main__" and sys.version_info[:2] < MIN_INTERROGATE_VERSION: # pragma: no cover # needs <3.6
|
|
17
|
-
sys.stderr.write(UNSUPPORTED_MARKER + ".".join(str(i) for i in sys.version_info[:3]))
|
|
17
|
+
sys.stderr.write(UNSUPPORTED_MARKER + ".".join(str(i) for i in sys.version_info[:3]) + "\n" + sys.executable)
|
|
18
18
|
raise SystemExit(UNSUPPORTED_EXIT_CODE)
|
|
19
19
|
|
|
20
20
|
import json
|
|
@@ -184,8 +184,13 @@ class PythonInfoCollector:
|
|
|
184
184
|
except ImportError:
|
|
185
185
|
pass
|
|
186
186
|
else:
|
|
187
|
+
# Tk.__init__ reads profile scripts from HOME or the working directory; probing needs only library metadata.
|
|
188
|
+
class _TclWithoutProfile(tk.Tk):
|
|
189
|
+
def readprofile(self, *_args, **_kwargs): # ruff:ignore[no-self-use] # Tk override must be an instance method.
|
|
190
|
+
return None
|
|
191
|
+
|
|
187
192
|
try:
|
|
188
|
-
tcl =
|
|
193
|
+
tcl = _TclWithoutProfile(useTk=False)
|
|
189
194
|
tcl_lib = tcl.eval("info library")
|
|
190
195
|
tk_lib = PythonInfoCollector._resolve_tk_lib(tcl, tcl_lib)
|
|
191
196
|
except tk.TclError:
|
|
@@ -212,9 +217,12 @@ class PythonInfoCollector:
|
|
|
212
217
|
tk_lib = PythonInfoCollector._query_tk_library(tcl)
|
|
213
218
|
if tk_lib is not None:
|
|
214
219
|
return tk_lib
|
|
215
|
-
|
|
220
|
+
import _tkinter # ruff:ignore[import-outside-top-level,import-private-name] # novermin # linked Tk version
|
|
221
|
+
|
|
222
|
+
# Loading Tk can block on DISPLAY; the linked version is enough to locate its library.
|
|
223
|
+
tk_version = _tkinter.TK_VERSION
|
|
216
224
|
tcl_parent = os.path.dirname(tcl_lib)
|
|
217
|
-
for version in (tk_version,
|
|
225
|
+
for version in (tk_version, tk_version.split(".")[0]):
|
|
218
226
|
tk_lib_path = os.path.join(tcl_parent, "tk{}".format(version))
|
|
219
227
|
if os.path.isdir(tk_lib_path) and os.path.exists(os.path.join(tk_lib_path, "tk.tcl")):
|
|
220
228
|
return tk_lib_path
|
python_discovery/_py_spec.py
CHANGED
|
@@ -197,7 +197,9 @@ class PythonSpec:
|
|
|
197
197
|
|
|
198
198
|
:param windows: if ``True``, require a ``.exe`` suffix.
|
|
199
199
|
:param all_implementations: when ``True`` and the spec does not constrain the implementation, match every
|
|
200
|
-
filename in :data:`KNOWN_IMPLEMENTATIONS` instead of only ``python``.
|
|
200
|
+
filename in :data:`KNOWN_IMPLEMENTATIONS` instead of only ``python``. A free-threaded or debug build the
|
|
201
|
+
spec leaves open also matches its marked name, such as ``python3.13t`` or ``python3.13-dbg``. Used by
|
|
202
|
+
enumeration APIs.
|
|
201
203
|
"""
|
|
202
204
|
version = r"{}(\.{}(\.{})?)?".format(
|
|
203
205
|
*(r"\d+" if v is None else v for v in (self.major, self.minor, self.micro)),
|
|
@@ -208,8 +210,9 @@ class PythonSpec:
|
|
|
208
210
|
impl = "|".join(re.escape(i) for i in KNOWN_IMPLEMENTATIONS)
|
|
209
211
|
else:
|
|
210
212
|
impl = "python"
|
|
211
|
-
|
|
212
|
-
|
|
213
|
+
# An unconstrained single lookup must not depend on directory order to choose a variant.
|
|
214
|
+
mod = "t?" if self.free_threaded or (all_implementations and self.free_threaded is None) else ""
|
|
215
|
+
dbg = "(?:d|-dbg|-debug)?" if self.debug or (all_implementations and self.debug is None) else ""
|
|
213
216
|
suffix = r"\.exe" if windows else ""
|
|
214
217
|
version_conditional = "?" if windows or self.major is None else ""
|
|
215
218
|
return re.compile(
|
python_discovery/_specifier.py
CHANGED
|
@@ -210,6 +210,14 @@ class SimpleSpecifier:
|
|
|
210
210
|
return str(candidate) == str(self.version)
|
|
211
211
|
if self.operator == "~=":
|
|
212
212
|
return self._check_compatible_release(candidate)
|
|
213
|
+
# PEP 440 excludes prereleases of a final release named by an exclusive upper bound.
|
|
214
|
+
if (
|
|
215
|
+
self.operator == "<"
|
|
216
|
+
and candidate.pre_type is not None
|
|
217
|
+
and self.version.pre_type is None
|
|
218
|
+
and candidate.release == self.version.release
|
|
219
|
+
):
|
|
220
|
+
return False
|
|
213
221
|
cmp_ops = {
|
|
214
222
|
"==": operator.eq,
|
|
215
223
|
"!=": operator.ne,
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Cancel a blocked query pipe read on Windows."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from ctypes import WinDLL, WinError, get_last_error, wintypes # ty: ignore[unresolved-import] # Windows-only APIs
|
|
6
|
+
from time import monotonic
|
|
7
|
+
from typing import TYPE_CHECKING, Final
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from threading import Thread
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def stop_query_reader(reader: Thread) -> None:
|
|
14
|
+
deadline = monotonic() + 1
|
|
15
|
+
while reader.is_alive() and monotonic() < deadline:
|
|
16
|
+
if (thread_id := reader.native_id) is not None:
|
|
17
|
+
try:
|
|
18
|
+
_cancel_synchronous_io(thread_id)
|
|
19
|
+
except OSError:
|
|
20
|
+
if reader.is_alive():
|
|
21
|
+
raise
|
|
22
|
+
reader.join(0.05)
|
|
23
|
+
if reader.is_alive():
|
|
24
|
+
msg = "query output reader did not stop"
|
|
25
|
+
raise TimeoutError(msg)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _cancel_synchronous_io(thread_id: int) -> None:
|
|
29
|
+
error_not_found: Final[int] = 1168
|
|
30
|
+
kernel = WinDLL("kernel32", use_last_error=True)
|
|
31
|
+
open_thread = kernel.OpenThread
|
|
32
|
+
open_thread.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD)
|
|
33
|
+
open_thread.restype = wintypes.HANDLE
|
|
34
|
+
cancel = kernel.CancelSynchronousIo
|
|
35
|
+
cancel.argtypes = (wintypes.HANDLE,)
|
|
36
|
+
cancel.restype = wintypes.BOOL
|
|
37
|
+
close = kernel.CloseHandle
|
|
38
|
+
close.argtypes = (wintypes.HANDLE,)
|
|
39
|
+
close.restype = wintypes.BOOL
|
|
40
|
+
|
|
41
|
+
if not (handle := open_thread(0x0001, wintypes.BOOL(0), thread_id)):
|
|
42
|
+
raise WinError(get_last_error())
|
|
43
|
+
try:
|
|
44
|
+
if not cancel(handle) and (error := get_last_error()) != error_not_found:
|
|
45
|
+
raise WinError(error)
|
|
46
|
+
finally:
|
|
47
|
+
if not close(handle):
|
|
48
|
+
raise WinError(get_last_error())
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
__all__ = ["stop_query_reader"]
|
|
@@ -8,6 +8,7 @@ import re
|
|
|
8
8
|
import sys
|
|
9
9
|
import winreg
|
|
10
10
|
from logging import basicConfig, getLogger
|
|
11
|
+
from pathlib import PureWindowsPath
|
|
11
12
|
from typing import TYPE_CHECKING, Any, Final
|
|
12
13
|
|
|
13
14
|
if TYPE_CHECKING:
|
|
@@ -136,12 +137,16 @@ def load_exe(hive_name: str, company: str, company_key: Any, tag: str) -> tuple[
|
|
|
136
137
|
|
|
137
138
|
|
|
138
139
|
def _resolve_exe(ip_key: Any, key_path: str) -> str | None: # ruff:ignore[any-type]
|
|
139
|
-
if (exe := get_value(ip_key, "ExecutablePath")) is
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
if (exe := get_value(ip_key, "ExecutablePath")) is None:
|
|
141
|
+
if (exe := get_value(ip_key, None)) is None:
|
|
142
|
+
msg(key_path, "no ExecutablePath or default for it")
|
|
143
|
+
return None
|
|
144
|
+
if isinstance(exe, str):
|
|
145
|
+
exe = os.path.join(exe, "python.exe")
|
|
146
|
+
if not isinstance(exe, str) or not PureWindowsPath(exe).is_absolute():
|
|
147
|
+
msg(key_path, f"expected an absolute executable path, got {exe!r}")
|
|
143
148
|
return None
|
|
144
|
-
return
|
|
149
|
+
return exe
|
|
145
150
|
|
|
146
151
|
|
|
147
152
|
def load_arch_data(hive_name: str, company: str, tag: str, tag_key: Any, default_arch: int) -> int | None: # ruff:ignore[any-type]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: python-discovery
|
|
3
|
-
Version: 1.9.
|
|
3
|
+
Version: 1.9.2
|
|
4
4
|
Import-Name: python_discovery
|
|
5
5
|
Summary: Python interpreter discovery
|
|
6
6
|
Project-URL: Changelog, https://github.com/tox-dev/python-discovery/blob/main/docs/changelog.rst
|
|
@@ -39,6 +39,8 @@ Description-Content-Type: text/markdown
|
|
|
39
39
|
[](https://pepy.tech/project/python-discovery)
|
|
40
40
|
[](https://github.com/tox-dev/python-discovery/actions/workflows/check.yaml)
|
|
41
41
|
[](https://python-discovery.readthedocs.io/en/latest/?badge=latest)
|
|
42
|
+
[](https://scorecard.dev/viewer/?uri=github.com/tox-dev/python-discovery)
|
|
43
|
+
[](https://www.bestpractices.dev/projects/16262)
|
|
42
44
|
|
|
43
45
|
## What is python-discovery?
|
|
44
46
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
python_discovery/__init__.py,sha256=BV4pKMWDAK_3pU8o2hYvtL30WKs_wTzIBRuFV7nUaug,852
|
|
2
|
+
python_discovery/_cache.py,sha256=HVGs7H0p-iee2TESXUUn-L5Sv4Sdb0b3r-1JQdGNchQ,8446
|
|
3
|
+
python_discovery/_cached_py_info.py,sha256=F9lKdba0pxJB6Pczuh-MtVdLrklvqrKwzLovnPijJd8,17117
|
|
4
|
+
python_discovery/_compat.py,sha256=iqxGuhK9rPaiui80G1H1KrweoMJ16gyLYEmUrSXrp5c,709
|
|
5
|
+
python_discovery/_discovery.py,sha256=VXw4ZTSM0GIUOIPEDFcOrurjqRF8HC6eRxB36PNB8_o,25030
|
|
6
|
+
python_discovery/_py_info.py,sha256=jci7XqbmcPsfZitkLBIMYbC9iDz48sXky1or-q8L6qo,28158
|
|
7
|
+
python_discovery/_py_info_collect.py,sha256=DEUo5hNXwjxchbYppyQEuNZ627jCukB9XmtCdIIRN7Y,17928
|
|
8
|
+
python_discovery/_py_spec.py,sha256=s76yM2zbrKdvwhaDlPyUo6ERKkn2MldRNydPZucNvOM,12154
|
|
9
|
+
python_discovery/_specifier.py,sha256=bv2o_ft2Ak2tKjQblncWjKgCW3Hg4RocTebz9Jyovac,10690
|
|
10
|
+
python_discovery/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
python_discovery/_windows/__init__.py,sha256=qthcyGh36b3WkeSbZ4ECRRYLbCOCaQu5X_DRZ98tXfk,315
|
|
12
|
+
python_discovery/_windows/_cancel_io.py,sha256=aL1FxCqv01fYb56SI_TvFyRDcb_zpCcU6oMj330IRio,1689
|
|
13
|
+
python_discovery/_windows/_pep514.py,sha256=VLXB7LXaKfqOKRz2IklZOf4tW-NE0F9cEKRQYV4eyRM,8279
|
|
14
|
+
python_discovery/_windows/_propose.py,sha256=EE-N-BiNgrFU9mQ34SkoF5gRaNjtspghssQxT9uRQi0,1756
|
|
15
|
+
python_discovery-1.9.2.dist-info/METADATA,sha256=hjQSjjgj_wiXHip7765qcZeoAjY9wjh30gxiYgv5lPw,4235
|
|
16
|
+
python_discovery-1.9.2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
17
|
+
python_discovery-1.9.2.dist-info/licenses/LICENSE,sha256=kOJeH68qSq6o2V7o5_VziwUqGswMnFgGgQH5Mahr1Yg,1023
|
|
18
|
+
python_discovery-1.9.2.dist-info/RECORD,,
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
python_discovery/__init__.py,sha256=BV4pKMWDAK_3pU8o2hYvtL30WKs_wTzIBRuFV7nUaug,852
|
|
2
|
-
python_discovery/_cache.py,sha256=svmG2GzUQzd2xTjAbm2eX1CKxxGAglfv4kxaPuxwPIA,5147
|
|
3
|
-
python_discovery/_cached_py_info.py,sha256=15P5_TODxkZCkNfD2Uk5dtPxGf0Nea_aWm2N7uY9zbY,12059
|
|
4
|
-
python_discovery/_compat.py,sha256=iqxGuhK9rPaiui80G1H1KrweoMJ16gyLYEmUrSXrp5c,709
|
|
5
|
-
python_discovery/_discovery.py,sha256=Ulq3gbOrCvVULengzFF20yrDwGYCj3xs86CGqF2LThc,23212
|
|
6
|
-
python_discovery/_py_info.py,sha256=AzL53BYCDB8nxVBbXyOh4YsFtZft-HoTG7EnCJdp7B8,27377
|
|
7
|
-
python_discovery/_py_info_collect.py,sha256=k9P4JzNkvW9iw37teeu7L6Y27ytWTnvWiK47B3gm5xg,17390
|
|
8
|
-
python_discovery/_py_spec.py,sha256=14WRyDOLEQt7COevcUU5Rb6sgy99l9XI5XEQRUtCHf8,11800
|
|
9
|
-
python_discovery/_specifier.py,sha256=9sMnJs7FTfIIduTCKKTA7Lk7onARqwkG_zzFIqgj0xs,10364
|
|
10
|
-
python_discovery/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
-
python_discovery/_windows/__init__.py,sha256=qthcyGh36b3WkeSbZ4ECRRYLbCOCaQu5X_DRZ98tXfk,315
|
|
12
|
-
python_discovery/_windows/_pep514.py,sha256=Vhxe-3hETFIXQrekLqOHmzG5eq5moeR8b0dCS80zbBg,8026
|
|
13
|
-
python_discovery/_windows/_propose.py,sha256=EE-N-BiNgrFU9mQ34SkoF5gRaNjtspghssQxT9uRQi0,1756
|
|
14
|
-
python_discovery-1.9.0.dist-info/METADATA,sha256=Bho3HCmi0jKsr1vRJbe9CU2J0PctXyaX6MsWsT_uk8A,3937
|
|
15
|
-
python_discovery-1.9.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
16
|
-
python_discovery-1.9.0.dist-info/licenses/LICENSE,sha256=kOJeH68qSq6o2V7o5_VziwUqGswMnFgGgQH5Mahr1Yg,1023
|
|
17
|
-
python_discovery-1.9.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|