python-discovery 1.6.7__py3-none-any.whl → 1.7.0__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.
@@ -532,11 +532,10 @@ def _read_python_version_file(start: str | Path, *, search_parents: bool = True)
532
532
  """Read a ``.python-version`` file, optionally searching parent directories."""
533
533
  current = start
534
534
  while True:
535
- candidate = os.path.join(current, ".python-version") if Path(current).is_dir() else current
536
- if Path(candidate).is_file():
537
- with Path(candidate).open(encoding="utf-8") as fh:
538
- if versions := [v for line in fh if (v := line.strip()) and not v.startswith("#")]:
539
- return versions
535
+ # unlike pathlib before 3.14, os.path answers False when a directory on the way can't be searched
536
+ candidate = os.path.join(current, ".python-version") if os.path.isdir(current) else current
537
+ if os.path.isfile(candidate) and (versions := _versions_in_file(candidate)):
538
+ return versions
540
539
  if not search_parents:
541
540
  return None
542
541
  parent = Path(current).parent
@@ -545,6 +544,16 @@ def _read_python_version_file(start: str | Path, *, search_parents: bool = True)
545
544
  current = parent
546
545
 
547
546
 
547
+ def _versions_in_file(path: str | Path) -> list[str]:
548
+ try:
549
+ # pyenv reads raw bytes, so a byte that is not UTF-8 (a Latin-1 comment, say) must not hide the versions
550
+ with Path(path).open(encoding="utf-8", errors="replace") as fh:
551
+ return [v for line in fh if (v := line.strip()) and not v.startswith("#")]
552
+ except OSError: # skip a file we cannot read, the same as one that names no version
553
+ _LOGGER.debug("failed to read %s", path, exc_info=True)
554
+ return []
555
+
556
+
548
557
  class PathPythonInfo(PythonInfo):
549
558
  """python info from path."""
550
559
 
@@ -10,26 +10,29 @@ from typing import Final
10
10
  from ._py_info import normalize_isa
11
11
  from ._specifier import SimpleSpecifier, SimpleSpecifierSet, SimpleVersion
12
12
 
13
- PATTERN = re.compile(
14
- r"""
15
- ^
16
- (?P<impl>[a-zA-Z]+)? # implementation (e.g. cpython, pypy)
17
- (?P<version>[0-9.]+)? # version (e.g. 3.12, 3.12.1)
13
+ _MODIFIERS: Final[str] = r"""
18
14
  (?P<threaded>t)? # free-threaded flag
19
15
  (?P<debug>d|(?:-dbg|-debug)(?=-|$))? # debug build flag (d, -dbg or -debug)
20
16
  (?:-(?P<arch>32|64))? # architecture bitness
21
17
  (?:-(?P<machine>[a-zA-Z0-9_.]+))? # ISA (e.g. arm64, x86_64, i86pc.64bit)
22
18
  $
23
- """,
19
+ """
20
+ PATTERN = re.compile(
21
+ r"""
22
+ ^
23
+ (?P<impl>[a-zA-Z]+)? # implementation (e.g. cpython, pypy)
24
+ (?P<version>[0-9.]+)? # version (e.g. 3.12, 3.12.1)
25
+ """
26
+ + _MODIFIERS,
24
27
  re.VERBOSE,
25
28
  )
26
29
  SPECIFIER_PATTERN = re.compile(
27
30
  r"""
28
31
  ^
29
32
  (?:(?P<impl>[A-Za-z]+)\s*)? # optional implementation prefix
30
- (?P<spec>(?:===|==|~=|!=|<=|>=|<|>).+) # PEP 440 version specifier
31
- $
32
- """,
33
+ (?P<spec>(?:===|==|~=|!=|<=|>=|<|>).*?) # PEP 440 version specifier, lazy so the modifiers can follow it
34
+ """
35
+ + _MODIFIERS,
33
36
  re.VERBOSE,
34
37
  )
35
38
 
@@ -69,51 +72,60 @@ def _parse_version_parts(version: str) -> tuple[int | None, int | None, int | No
69
72
  def _parse_spec_pattern(string_spec: str) -> PythonSpec | None:
70
73
  if not (match := re.match(PATTERN, string_spec)):
71
74
  return None
72
- groups = match.groupdict()
73
- version = groups["version"]
74
- major, minor, micro, threaded = None, None, None, None
75
- debug = True if groups["debug"] else None # unconstrained unless an explicit d/-dbg/-debug marker is present
76
- if version is not None:
75
+ release, threaded = (None, None, None), None
76
+ if (version := match["version"]) is not None:
77
77
  try:
78
- major, minor, micro = _parse_version_parts(version)
78
+ release = _parse_version_parts(version)
79
79
  except ValueError:
80
80
  return None
81
- threaded = bool(groups["threaded"])
82
- impl = groups["impl"]
81
+ threaded = bool(match["threaded"])
82
+ return _spec_from_match(string_spec, match, release, threaded=threaded, version_specifier=None)
83
+
84
+
85
+ def _spec_from_match(
86
+ string_spec: str,
87
+ match: re.Match[str],
88
+ release: tuple[int | None, int | None, int | None],
89
+ *,
90
+ threaded: bool | None,
91
+ version_specifier: SpecifierSet | None,
92
+ ) -> PythonSpec:
93
+ impl = match["impl"]
83
94
  if impl in {"py", "python"}:
84
95
  impl = None
85
96
  if impl == "graalvm":
86
97
  impl = "graalpy"
87
- arch = _int_or_none(groups["arch"])
88
- machine = groups.get("machine")
89
- if machine is not None:
90
- machine = normalize_isa(machine)
91
98
  return PythonSpec(
92
- string_spec, impl, major, minor, micro, arch, None, free_threaded=threaded, machine=machine, debug=debug
99
+ string_spec,
100
+ impl,
101
+ *release,
102
+ _int_or_none(match["arch"]),
103
+ None,
104
+ free_threaded=threaded,
105
+ machine=None if (machine := match["machine"]) is None else normalize_isa(machine),
106
+ debug=True if match["debug"] else None, # unconstrained unless an explicit d/-dbg/-debug marker is present
107
+ version_specifier=version_specifier,
93
108
  )
94
109
 
95
110
 
96
111
  def _parse_specifier(string_spec: str) -> PythonSpec | None:
97
- if not (specifier_match := SPECIFIER_PATTERN.match(string_spec.strip())):
98
- return None
99
- if SpecifierSet is None: # pragma: no cover
112
+ if not (match := SPECIFIER_PATTERN.match(string_spec.strip())):
100
113
  return None
101
- impl = specifier_match.group("impl")
102
- spec_text = specifier_match.group("spec").strip()
103
- try:
104
- version_specifier = SpecifierSet.from_string(spec_text)
105
- except InvalidSpecifier: # pragma: no cover
106
- return None
107
- # the set never raises: it keeps an unparsable version as None and drops items without an operator, so fall back
108
- # to a literal path, like any other unrecognized spec, unless every item parsed into a usable version
114
+ spec_text = match["spec"].strip()
115
+ version_specifier = SpecifierSet.from_string(spec_text)
116
+ # the set never raises: it keeps an unparsable version as None and drops items without an operator
109
117
  items = [item for item in spec_text.split(",") if item.strip()]
110
118
  if len(items) != len(version_specifier.specifiers) or any(spec.version is None for spec in version_specifier):
111
- return None
112
- if impl in {"py", "python"}:
113
- impl = None
114
- if impl == "graalvm":
115
- impl = "graalpy"
116
- return PythonSpec(string_spec, impl, None, None, None, None, None, version_specifier=version_specifier)
119
+ msg = f"invalid version specifier {string_spec!r}"
120
+ raise ValueError(msg)
121
+ return _spec_from_match(
122
+ string_spec,
123
+ match,
124
+ (None, None, None),
125
+ # the version bounds say nothing about the build, so without a trailing t any build qualifies
126
+ threaded=True if match["threaded"] else None,
127
+ version_specifier=version_specifier,
128
+ )
117
129
 
118
130
 
119
131
  class PythonSpec:
@@ -169,6 +181,7 @@ class PythonSpec:
169
181
 
170
182
  :param string_spec: an interpreter spec — an absolute path, a version string, an implementation prefix,
171
183
  or a `version specifier <https://packaging.python.org/en/latest/specifications/version-specifiers/>`_.
184
+ :raises ValueError: if the spec starts with a version specifier operator but does not parse as one.
172
185
  """
173
186
  if pathlib.Path(string_spec).is_absolute():
174
187
  return cls(string_spec, None, None, None, None, None, string_spec)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-discovery
3
- Version: 1.6.7
3
+ Version: 1.7.0
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
@@ -2,16 +2,16 @@ python_discovery/__init__.py,sha256=Hi4j3pMvC8Fo8KoNL-wOHsPSCUHwY4c0qvv5sjnFzME,
2
2
  python_discovery/_cache.py,sha256=svmG2GzUQzd2xTjAbm2eX1CKxxGAglfv4kxaPuxwPIA,5147
3
3
  python_discovery/_cached_py_info.py,sha256=15P5_TODxkZCkNfD2Uk5dtPxGf0Nea_aWm2N7uY9zbY,12059
4
4
  python_discovery/_compat.py,sha256=iqxGuhK9rPaiui80G1H1KrweoMJ16gyLYEmUrSXrp5c,709
5
- python_discovery/_discovery.py,sha256=mV1dp7fkyAhXAKjlykRGCQcszQkNQ3UJ-VrDCB0LOSI,22722
5
+ python_discovery/_discovery.py,sha256=Ulq3gbOrCvVULengzFF20yrDwGYCj3xs86CGqF2LThc,23212
6
6
  python_discovery/_py_info.py,sha256=fAhFj5swYdGvhJpAXR6b6O1yT2MSbRXTDMrdu15e6q8,26965
7
7
  python_discovery/_py_info_collect.py,sha256=3PNlySbxNSHDjBw8KJ_olVpOXww1qhGdY28YoLfF0Z8,16160
8
- python_discovery/_py_spec.py,sha256=uN6PftKwk8cJnnbav5kXpbnQQYQ56LZ828KLp04YGKU,11499
8
+ python_discovery/_py_spec.py,sha256=14WRyDOLEQt7COevcUU5Rb6sgy99l9XI5XEQRUtCHf8,11800
9
9
  python_discovery/_specifier.py,sha256=9sMnJs7FTfIIduTCKKTA7Lk7onARqwkG_zzFIqgj0xs,10364
10
10
  python_discovery/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  python_discovery/_windows/__init__.py,sha256=qthcyGh36b3WkeSbZ4ECRRYLbCOCaQu5X_DRZ98tXfk,315
12
12
  python_discovery/_windows/_pep514.py,sha256=Vhxe-3hETFIXQrekLqOHmzG5eq5moeR8b0dCS80zbBg,8026
13
13
  python_discovery/_windows/_propose.py,sha256=EE-N-BiNgrFU9mQ34SkoF5gRaNjtspghssQxT9uRQi0,1756
14
- python_discovery-1.6.7.dist-info/METADATA,sha256=lnUw1wj_IpXgeaROo05hjAbznwmCY_9DaEOjRkjSYVw,3937
15
- python_discovery-1.6.7.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
16
- python_discovery-1.6.7.dist-info/licenses/LICENSE,sha256=kOJeH68qSq6o2V7o5_VziwUqGswMnFgGgQH5Mahr1Yg,1023
17
- python_discovery-1.6.7.dist-info/RECORD,,
14
+ python_discovery-1.7.0.dist-info/METADATA,sha256=FOu5Fx2hWx_B48bPGJK6vX-1lRqt3ahuJfbn86jW1tw,3937
15
+ python_discovery-1.7.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
16
+ python_discovery-1.7.0.dist-info/licenses/LICENSE,sha256=kOJeH68qSq6o2V7o5_VziwUqGswMnFgGgQH5Mahr1Yg,1023
17
+ python_discovery-1.7.0.dist-info/RECORD,,