|
10 | 10 | from platformdirs import user_data_path |
11 | 11 |
|
12 | 12 | from ._compat import IS_WIN, fs_path_id |
13 | | -from ._discover import Discover |
14 | 13 | from ._py_info import PythonInfo |
15 | 14 | from ._py_spec import PythonSpec |
16 | 15 |
|
|
22 | 21 | LOGGER = logging.getLogger(__name__) |
23 | 22 |
|
24 | 23 |
|
25 | | -class Builtin(Discover): |
26 | | - python_spec: Sequence[str] |
27 | | - cache: PyInfoCache | None |
28 | | - try_first_with: Sequence[str] |
29 | | - |
30 | | - def __init__( |
31 | | - self, |
32 | | - python_spec: Sequence[str] | None = None, |
33 | | - cache: PyInfoCache | None = None, |
34 | | - try_first_with: Sequence[str] | None = None, |
35 | | - env: Mapping[str, str] | None = None, |
36 | | - ) -> None: |
37 | | - super().__init__(env=env) |
38 | | - self.python_spec = python_spec or [sys.executable] |
39 | | - self.cache = cache |
40 | | - self.try_first_with = try_first_with or [] |
41 | | - |
42 | | - def run(self) -> PythonInfo | None: |
43 | | - for python_spec in self.python_spec: |
44 | | - result = get_interpreter(python_spec, self.try_first_with, self.cache, self._env) |
45 | | - if result is not None: |
46 | | - return result |
47 | | - return None |
48 | | - |
49 | | - def __repr__(self) -> str: |
50 | | - spec = self.python_spec[0] if len(self.python_spec) == 1 else self.python_spec |
51 | | - return f"{self.__class__.__name__} discover of python_spec={spec!r}" |
| 24 | +def get_interpreter( |
| 25 | + key: str | Sequence[str], |
| 26 | + try_first_with: Iterable[str] | None = None, |
| 27 | + cache: PyInfoCache | None = None, |
| 28 | + env: Mapping[str, str] | None = None, |
| 29 | +) -> PythonInfo | None: |
| 30 | + specs = [key] if isinstance(key, str) else key |
| 31 | + for spec_str in specs: |
| 32 | + if result := _find_interpreter(spec_str, try_first_with or (), cache, env): |
| 33 | + return result |
| 34 | + return None |
52 | 35 |
|
53 | 36 |
|
54 | | -def get_interpreter( |
55 | | - key, try_first_with: Iterable[str], cache: PyInfoCache | None = None, env: Mapping[str, str] | None = None |
| 37 | +def _find_interpreter( |
| 38 | + key: str, |
| 39 | + try_first_with: Iterable[str], |
| 40 | + cache: PyInfoCache | None = None, |
| 41 | + env: Mapping[str, str] | None = None, |
56 | 42 | ) -> PythonInfo | None: |
57 | 43 | spec = PythonSpec.from_string_spec(key) |
58 | 44 | LOGGER.info("find interpreter for spec %r", spec) |
59 | | - proposed_paths = set() |
| 45 | + proposed_paths: set[tuple[str, bool]] = set() |
60 | 46 | env = os.environ if env is None else env |
61 | 47 | for interpreter, impl_must_match in propose_interpreters(spec, try_first_with, cache, env): |
62 | 48 | if interpreter is None: # pragma: no cover |
63 | 49 | continue |
64 | | - key = interpreter.system_executable, impl_must_match |
65 | | - if key in proposed_paths: |
| 50 | + proposed_key = interpreter.system_executable, impl_must_match |
| 51 | + if proposed_key in proposed_paths: |
66 | 52 | continue |
67 | 53 | LOGGER.info("proposed %s", interpreter) |
68 | 54 | if interpreter.satisfies(spec, impl_must_match): |
69 | 55 | LOGGER.debug("accepted %s", interpreter) |
70 | 56 | return interpreter |
71 | | - proposed_paths.add(key) |
| 57 | + proposed_paths.add(proposed_key) |
72 | 58 | return None |
73 | 59 |
|
74 | 60 |
|
@@ -292,7 +278,6 @@ class PathPythonInfo(PythonInfo): |
292 | 278 |
|
293 | 279 |
|
294 | 280 | __all__ = [ |
295 | | - "Builtin", |
296 | 281 | "LazyPathDump", |
297 | 282 | "PathPythonInfo", |
298 | 283 | "get_interpreter", |
|
0 commit comments