Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add free-threaded Python support #2809

Merged
merged 25 commits into from
Jan 15, 2025
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4feb6d3
Add free-threaded Python support to PythonSpec
robsdedude Nov 29, 2024
a1f0562
Merge branch 'main' into feat/free-threaded-python
gaborbernat Dec 20, 2024
e22db2d
Add changelog entry
robsdedude Nov 29, 2024
f003c3d
Fix spec: None value for threading flag on empty spec
robsdedude Jan 8, 2025
f2633d2
Version py info cache files
robsdedude Jan 8, 2025
ffe7798
Add threaded flag to PythonInfo
robsdedude Jan 8, 2025
debd89c
CI: add python 3.13t to matrix
robsdedude Jan 8, 2025
bc0b3b7
Update docs
robsdedude Jan 8, 2025
9ed16d2
Add more tests
robsdedude Jan 8, 2025
0c59eab
Merge branch 'main' into feat/free-threaded-python
gaborbernat Jan 10, 2025
5eafd42
Fix windows registry discovery (PEP 514)
robsdedude Jan 10, 2025
c3c8291
GitHub actions formatting
robsdedude Jan 10, 2025
27aeab2
Improve docs: expected spec format
robsdedude Jan 10, 2025
41539c6
GitHub actions: tox with py3.12 also for host py3.13t
robsdedude Jan 10, 2025
cadb3bc
CI: hack to get tox to pick up python3.13t
robsdedude Jan 10, 2025
0668de0
Win: more lenient free-threading detection
robsdedude Jan 12, 2025
1697ae1
Fix discovery + tests
robsdedude Jan 12, 2025
b68ef18
Fix 3.13t interpreter selection in tox.ini
robsdedude Jan 14, 2025
710be8f
CI: increase test timeout
robsdedude Jan 14, 2025
76dcb36
Adjust windows tests to new registry mock values
robsdedude Jan 15, 2025
743922f
pip514: fix registry value type warning
robsdedude Jan 15, 2025
6b11264
Revert CI timeout change
robsdedude Jan 15, 2025
c3dc4fd
More windows discory test fixes
robsdedude Jan 15, 2025
9c37467
CI: fix windows env substitution
robsdedude Jan 15, 2025
c8fc645
Merge branch 'main' into feat/free-threaded-python
gaborbernat Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix discovery + tests
  • Loading branch information
robsdedude committed Jan 12, 2025
commit 1697ae1d6a1d84e66d22de9b4b1d336f7d978fbf
17 changes: 12 additions & 5 deletions src/virtualenv/discovery/py_info.py
Original file line number Diff line number Diff line change
@@ -306,7 +306,7 @@ def clear_cache(cls, app_data):
clear(app_data)
cls._cache_exe_discovery.clear()

def satisfies(self, spec, impl_must_match): # noqa: C901
def satisfies(self, spec, impl_must_match): # noqa: C901, PLR0911
"""Check if a given specification can be satisfied by the this python interpreter instance."""
if spec.path:
if self.executable == os.path.abspath(spec.path):
@@ -332,6 +332,9 @@ def satisfies(self, spec, impl_must_match): # noqa: C901
if spec.architecture is not None and spec.architecture != self.architecture:
return False

if spec.free_threaded is not None and spec.free_threaded != self.free_threaded:
return False

for our, req in zip(self.version_info[0:3], (spec.major, spec.minor, spec.micro)):
if req is not None and our is not None and our != req:
return False
@@ -528,10 +531,14 @@ def _find_possible_exe_names(self):
for name in self._possible_base():
for at in (3, 2, 1, 0):
version = ".".join(str(i) for i in self.version_info[:at])
for arch in [f"-{self.architecture}", ""]:
for ext in EXTENSIONS:
candidate = f"{name}{version}{arch}{ext}"
name_candidate[candidate] = None
mods = [""]
if self.free_threaded:
mods.append("t")
for mod in mods:
for arch in [f"-{self.architecture}", ""]:
for ext in EXTENSIONS:
candidate = f"{name}{version}{mod}{arch}{ext}"
name_candidate[candidate] = None
return list(name_candidate.keys())

def _possible_base(self):
8 changes: 4 additions & 4 deletions src/virtualenv/discovery/py_spec.py
Original file line number Diff line number Diff line change
@@ -61,14 +61,14 @@ def _int_or_none(val):
major = int(str(version_data)[0]) # first digit major
if version_data > 9: # noqa: PLR2004
minor = int(str(version_data)[1:])
threaded = bool(groups["threaded"])
ok = True
except ValueError:
pass
else:
impl = groups["impl"]
if impl in {"py", "python"}:
impl = None
threaded = bool(groups["threaded"])
arch = _int_or_none(groups["arch"])

if not ok:
@@ -82,7 +82,7 @@ def generate_re(self, *, windows: bool) -> re.Pattern:
*(r"\d+" if v is None else v for v in (self.major, self.minor, self.micro))
)
impl = "python" if self.implementation is None else f"python|{re.escape(self.implementation)}"
mod = "t" if self.free_threaded else ""
mod = "t?" if self.free_threaded else ""
suffix = r"\.exe" if windows else ""
version_conditional = (
"?"
@@ -94,7 +94,7 @@ def generate_re(self, *, windows: bool) -> re.Pattern:
)
# Try matching `direct` first, so the `direct` group is filled when possible.
return re.compile(
rf"(?P<impl>{impl})(?P<v>{version}){version_conditional}{mod}{suffix}$",
rf"(?P<impl>{impl})(?P<v>{version}{mod}){version_conditional}{suffix}$",
flags=re.IGNORECASE,
)

@@ -110,7 +110,7 @@ def satisfies(self, spec):
return False
if spec.architecture is not None and spec.architecture != self.architecture:
return False
if spec.free_threaded != self.free_threaded:
if spec.free_threaded is not None and spec.free_threaded != self.free_threaded:
return False

for our, req in zip((self.major, self.minor, self.micro), (spec.major, spec.minor, spec.micro)):
2 changes: 1 addition & 1 deletion tests/unit/discovery/py_info/test_py_info.py
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ def test_satisfy_not_arch():

def test_satisfy_not_threaded():
parsed_spec = PythonSpec.from_string_spec(
f"{CURRENT.implementation}{'' if CURRENT.free_threaded else 't'}",
f"{CURRENT.implementation}{CURRENT.version_info.major}{'' if CURRENT.free_threaded else 't'}",
)
matches = CURRENT.satisfies(parsed_spec, True)
assert matches is False
4 changes: 2 additions & 2 deletions tests/unit/discovery/test_discovery.py
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, se
if specificity == "more":
# e.g. spec: python3, exe: /bin/python3.12
core_ver = current.version_info.major
exe_ver = ".".join(str(i) for i in current.version_info[0:2])
exe_ver = ".".join(str(i) for i in current.version_info[0:2]) + threaded
elif specificity == "less":
# e.g. spec: python3.12.1, exe: /bin/python3
core_ver = ".".join(str(i) for i in current.version_info[0:3])
@@ -38,7 +38,7 @@ def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, se
# e.g. spec: python3.12.1, exe: /bin/python
core_ver = ".".join(str(i) for i in current.version_info[0:3])
exe_ver = ""
core = threaded if specificity == "none" else f"{name}{threaded}{core_ver}"
core = "" if specificity == "none" else f"{name}{core_ver}{threaded}"
exe_name = f"{name}{exe_ver}{'.exe' if sys.platform == 'win32' else ''}"
target = tmp_path / current.install_path("scripts")
target.mkdir(parents=True)
23 changes: 16 additions & 7 deletions tests/unit/discovery/test_py_spec.py
Original file line number Diff line number Diff line change
@@ -76,13 +76,22 @@ def test_spec_satisfies_implementation_nok():
def _version_satisfies_pairs():
target = set()
version = tuple(str(i) for i in sys.version_info[0:3])
for i in range(len(version) + 1):
req = ".".join(version[0:i])
for j in range(i + 1):
sat = ".".join(version[0:j])
# can be satisfied in both directions
target.add((req, sat))
target.add((sat, req))
for threading in (False, True):
for i in range(len(version) + 1):
req = ".".join(version[0:i])
for j in range(i + 1):
sat = ".".join(version[0:j])
# can be satisfied in both directions
if sat:
target.add((req, sat))
# else: no version => no free-threading info
target.add((sat, req))
if not threading or not sat or not req:
# free-threading info requires a version
continue
target.add((f"{req}t", f"{sat}t"))
target.add((f"{sat}t", f"{req}t"))

return sorted(target)


Loading