Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions news/abi3t-whl-selection.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(pypi) The default `_freethreaded` {obj}`pip.default` platforms now select the
`abi3t` wheels that use the stable ABI for the free-threaded builds as specified
in [PEP 803](https://peps.python.org/pep-0803/). The version-specific `cp3xt`
wheels are still preferred over `abi3t` wheels.
18 changes: 14 additions & 4 deletions python/private/pypi/default_platforms.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ def _platform(
os_constraint = None,
config_settings = [],
marker = ""):
# The free-threaded builds can use the `abi3t` stable ABI wheels, see
# https://peps.python.org/pep-0803/. The python tag of the `abi3t`
# wheels is the minimum supported version, which is handled in the same
# way as for `abi3` when selecting the wheels. The GIL-enabled builds
# cannot use `abi3t` wheels unless they are also tagged with `abi3`,
# e.g. `cp315-abi3.abi3t`.
#
# The last item has the highest priority.
if freethreaded:
whl_abi_tags = ["abi3t", "cp{major}{minor}t"]
else:
whl_abi_tags = ["abi3", "cp{major}{minor}"]

return {
"arch_name": arch_name,
"config_settings": [
Expand All @@ -26,10 +39,7 @@ def _platform(
"marker": "python_version >= '3.13'" if freethreaded else marker,
"name": name,
"os_name": os_name,
"whl_abi_tags": ["cp{major}{minor}t"] if freethreaded else [
"abi3",
"cp{major}{minor}",
],
"whl_abi_tags": whl_abi_tags,
"whl_platform_tags": whl_platform_tags,
}

Expand Down
16 changes: 16 additions & 0 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,29 @@ parameters like `{major}` and `{minor}` which will be replaced with python versi
`cp{major}{minor}` will result in `cp313` given the full python version is `3.13.5`.
Will always include `"none"` even if it is not specified.

The python tag of the wheels using the stable ABI (`abi3` and `abi3t`) is
treated as the minimum supported Python version, e.g. a `cp315-abi3t` wheel is
selected for Python `3.15` and above.

The default platforms use the following values:
* `["abi3", "cp{major}{minor}"]` for the GIL-enabled builds.
* `["abi3t", "cp{major}{minor}t"]` for the free-threaded builds, where `abi3t`
is the stable ABI for the free-threaded builds as specified in
[PEP 803](https://peps.python.org/pep-0803/). The GIL-enabled builds only
select `abi3t` wheels if they are also tagged as `abi3` (e.g.
`cp315-abi3.abi3t`).

:::{note}
We select a single wheel and the last match will take precedence.
:::

:::{seealso}
See official [docs](https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#abi-tag) for more information.
:::

:::{versionchanged} VERSION_NEXT_FEATURE
The default free-threaded platforms select the `abi3t` wheels.
:::
""",
),
"whl_platform_tags": attr.string_list(
Expand Down
10 changes: 8 additions & 2 deletions python/private/pypi/whl_target_platforms.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ def whl_target_platforms(platform_tag, abi_tag = ""):
cpus = _cpu_from_tag(platform_tag)

abi = None
if abi_tag not in ["", "none", "abi3"]:
abi = abi_tag

# Stable ABI tags (`abi3`, `abi3t`, and `abi3.abi3t`; see
# https://peps.python.org/pep-0803/) do not identify a single
# interpreter ABI.
for tag in abi_tag.split("."):
if tag not in ["", "none", "abi3", "abi3t"]:
abi = abi_tag
break

# TODO @aignas 2024-05-29: this code is present in many places, I think
_, _, tail = platform_tag.partition("_")
Expand Down
6 changes: 5 additions & 1 deletion tests/pypi/extension/extension_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def _default(
whl_platform_tags = whl_platform_tags or [],
)

def _default_whl_abi_tags(os, cpu, freethreaded):
platform = "{}_{}{}".format(os, cpu, freethreaded)
return default_platforms()[platform]["whl_abi_tags"]

# The default value for the default platforms tags use in `_mod`.
_default_tags_default = [
_default(
Expand All @@ -85,7 +89,7 @@ _default_tags_default = [
"@platforms//os:{}".format(os),
"@platforms//cpu:{}".format(cpu),
],
whl_abi_tags = ["cp{major}{minor}t"] if freethreaded else ["abi3", "cp{major}{minor}"],
whl_abi_tags = _default_whl_abi_tags(os, cpu, freethreaded),
whl_platform_tags = whl_platform_tags,
)
for (os, cpu, freethreaded), whl_platform_tags in {
Expand Down
7 changes: 6 additions & 1 deletion tests/pypi/hub_builder/hub_builder_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("@rules_testing//lib:truth.bzl", "subjects")
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "REPO_VERBOSITY_ENV_VAR", "repo_utils") # buildifier: disable=bzl-visibility
load("//python/private/pypi:default_platforms.bzl", "default_platforms") # buildifier: disable=bzl-visibility
load("//python/private/pypi:hub_builder.bzl", _hub_builder = "hub_builder") # buildifier: disable=bzl-visibility
load("//python/private/pypi:parse_simpleapi_html.bzl", "parse_simpleapi_html") # buildifier: disable=bzl-visibility
load("//python/private/pypi:platform.bzl", _plat = "platform") # buildifier: disable=bzl-visibility
Expand All @@ -40,6 +41,10 @@ simple==0.0.1 \
},
)

def _default_whl_abi_tags(os, cpu, freethreaded):
platform = "{}_{}{}".format(os, cpu, freethreaded)
return default_platforms()[platform]["whl_abi_tags"]

def hub_builder(
env,
enable_pipstar_extract = True,
Expand All @@ -66,7 +71,7 @@ def hub_builder(
"@platforms//os:{}".format(os),
"@platforms//cpu:{}".format(cpu),
],
whl_abi_tags = ["cp{major}{minor}t"] if freethreaded else ["abi3", "cp{major}{minor}"],
whl_abi_tags = _default_whl_abi_tags(os, cpu, freethreaded),
whl_platform_tags = whl_platform_tags,
)
for (os, cpu, freethreaded), whl_platform_tags in {
Expand Down
164 changes: 164 additions & 0 deletions tests/pypi/select_whl/select_whl_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "REPO_VERBOSITY_ENV_VAR", "repo_utils") # buildifier: disable=bzl-visibility
load("//python/private/pypi:default_platforms.bzl", "default_platforms") # buildifier: disable=bzl-visibility
load("//python/private/pypi:select_whl.bzl", "select_whl") # buildifier: disable=bzl-visibility

WHL_LIST = [
Expand Down Expand Up @@ -61,6 +62,17 @@ WHL_LIST = [
"pkg-0.0.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
]

# See https://peps.python.org/pep-0803/
ABI3T_WHL_LIST = [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-cp315-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-cp315t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-cp316t-manylinux_2_17_x86_64.whl",
]

def _match(env, got, *want_filenames):
if not want_filenames:
env.expect.that_collection(got).has_size(len(want_filenames))
Expand Down Expand Up @@ -92,6 +104,13 @@ def _select_whl(whls, debug = False, **kwargs):
**kwargs
)

def _default_abi_tags(platform, python_version):
major, _, minor = python_version.partition(".")
return ["none"] + [
tag.format(major = major, minor = minor)
for tag in default_platforms()[platform]["whl_abi_tags"]
]

_tests = []

def _test_not_select_py2(env):
Expand Down Expand Up @@ -547,6 +566,151 @@ def _test_android(env):

_tests.append(_test_android)

def _test_abi3t_freethreaded(env):
got = _select_whl(
whls = [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-cp315-manylinux_2_17_x86_64.whl",
],
whl_platform_tags = ["any", "manylinux_*_x86_64"],
whl_abi_tags = _default_abi_tags("linux_x86_64_freethreaded", "3.15"),
python_version = "3.15",
limit = 3,
)
_match(
env,
got,
# The free-threaded builds cannot use `abi3` or `cp315` wheels.
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
)

_tests.append(_test_abi3t_freethreaded)

def _test_abi3t_prefer_version_specific(env):
got = _select_whl(
whls = ABI3T_WHL_LIST,
whl_platform_tags = ["any", "manylinux_*_x86_64"],
whl_abi_tags = _default_abi_tags("linux_x86_64_freethreaded", "3.15"),
python_version = "3.15",
limit = 3,
)
_match(
env,
got,
# The last item has the most priority
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-cp315t-manylinux_2_17_x86_64.whl",
)

_tests.append(_test_abi3t_prefer_version_specific)

def _test_abi3t_python_tag_is_minimum_version(env):
# The `cp315-abi3t` wheel can be used on 3.15 and above, whilst the
# `cp316-abi3t` wheel cannot be used on 3.15.
for python_version, want in {
"3.14": [
"pkg-0.0.1-py3-none-any.whl",
],
"3.15": [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
],
"3.16": [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
],
"3.17": [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
],
}.items():
got = _select_whl(
whls = [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
],
whl_platform_tags = ["any", "manylinux_*_x86_64"],
whl_abi_tags = _default_abi_tags(
"linux_x86_64_freethreaded",
python_version,
),
python_version = python_version,
limit = 3,
)
_match(env, got, *want)

_tests.append(_test_abi3t_python_tag_is_minimum_version)

def _test_abi3t_newer_cpxt_preferred(env):
got = _select_whl(
whls = ABI3T_WHL_LIST,
whl_platform_tags = ["any", "manylinux_*_x86_64"],
whl_abi_tags = _default_abi_tags("linux_x86_64_freethreaded", "3.16"),
python_version = "3.16",
limit = 4,
)
_match(
env,
got,
# The last item has the most priority
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp316-cp316t-manylinux_2_17_x86_64.whl",
)

_tests.append(_test_abi3t_newer_cpxt_preferred)

def _test_abi3t_not_selected_on_gil_builds(env):
got = _select_whl(
whls = [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3t-manylinux_2_17_x86_64.whl",
"pkg-0.0.1-cp315-cp315t-manylinux_2_17_x86_64.whl",
],
whl_platform_tags = ["any", "manylinux_*_x86_64"],
whl_abi_tags = _default_abi_tags("linux_x86_64", "3.15"),
python_version = "3.15",
limit = 3,
)
_match(
env,
got,
# The GIL-enabled builds cannot use `abi3t` only wheels.
"pkg-0.0.1-py3-none-any.whl",
)

_tests.append(_test_abi3t_not_selected_on_gil_builds)

def _test_abi3_abi3t_compressed_tag(env):
whls = [
"pkg-0.0.1-py3-none-any.whl",
"pkg-0.0.1-cp315-abi3.abi3t-manylinux_2_17_x86_64.whl",
]
for platform in ["linux_x86_64", "linux_x86_64_freethreaded"]:
got = _select_whl(
whls = whls,
whl_platform_tags = ["any", "manylinux_*_x86_64"],
whl_abi_tags = _default_abi_tags(platform, "3.15"),
python_version = "3.15",
)

# The wheels compatible with both ABIs can be used on both builds.
_match(
env,
[got],
"pkg-0.0.1-cp315-abi3.abi3t-manylinux_2_17_x86_64.whl",
)

_tests.append(_test_abi3_abi3t_compressed_tag)

def select_whl_test_suite(name):
"""Create the test suite.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _test_simple(env):
}

for give, want in tests.items():
for abi in ["", "abi3", "none"]:
for abi in ["", "abi3", "abi3t", "abi3.abi3t", "none"]:
got = whl_target_platforms(give, abi)
env.expect.that_collection(got).contains_exactly(want)

Expand Down
Loading