Skip to content

Commit 12d3186

Browse files
committed
Target Stable ABI on free-threaded Python 3.15+
Building for the new Stable ABI creates a single wheel for both GILful and free-threaded Python 3.15+ (cp315-abi3.abi3t) but must happen in a free-threaded interpreter until setuptools drops that requirement. Fixes: #340 Signed-off-by: Benjamin Gilbert <bgilbert@cs.cmu.edu>
1 parent 0dfd2d3 commit 12d3186

3 files changed

Lines changed: 52 additions & 10 deletions

File tree

‎.github/workflows/python.yml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ jobs:
135135
fi
136136
mkdir -p "artifacts/whl/${{ needs.pre-commit.outputs.dist-base }}"
137137
mv dist/* "artifacts/whl/${{ needs.pre-commit.outputs.dist-base }}"
138-
# from system builds, save version-specific wheels and oldest abi3 wheel
138+
# from system builds, save version-specific wheels, oldest abi3 wheel,
139+
# and oldest abi3.abi3t wheel
139140
python -c 'import sys, sysconfig
140-
if "${{ matrix.openslide }}" == "system" and (sys.version_info < (3, 12) or sysconfig.get_config_var("Py_GIL_DISABLED")):
141+
if "${{ matrix.openslide }}" == "system" and (sys.version_info < (3, 12) or (sys.version_info < (3, 16) and sysconfig.get_config_var("Py_GIL_DISABLED"))):
141142
print("archive_wheel=1")' >> $GITHUB_ENV
142143
- name: Install
143144
run: pip install artifacts/whl/${{ needs.pre-commit.outputs.dist-base }}/*.whl
@@ -210,9 +211,10 @@ jobs:
210211
python -m build -w
211212
mkdir -p "artifacts/whl/${{ needs.pre-commit.outputs.dist-base }}"
212213
mv dist/*.whl "artifacts/whl/${{ needs.pre-commit.outputs.dist-base }}"
213-
# from zip builds, save version-specific wheels and oldest abi3 wheel
214+
# from zip builds, save version-specific wheels, oldest abi3 wheel,
215+
# and oldest abi3.abi3t wheel
214216
python -c 'import sys, sysconfig
215-
if "${{ matrix.openslide }}" == "zip" and (sys.version_info < (3, 12) or sysconfig.get_config_var("Py_GIL_DISABLED")):
217+
if "${{ matrix.openslide }}" == "zip" and (sys.version_info < (3, 12) or (sys.version_info < (3, 16) and sysconfig.get_config_var("Py_GIL_DISABLED"))):
216218
print("archive_wheel=1")' >> $GITHUB_ENV
217219
- name: Install
218220
run: pip install artifacts/whl/${{ needs.pre-commit.outputs.dist-base }}/*.whl

‎openslide/_convert.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ static PyMethodDef _convert_methods[] = {
102102
{NULL, NULL, 0, NULL}
103103
};
104104

105+
#ifdef Py_TARGET_ABI3T
106+
PyABIInfo_VAR(abi_info);
107+
108+
static PySlot _convert_slot_array[] = {
109+
PySlot_STATIC_DATA(Py_mod_abi, &abi_info),
110+
PySlot_STATIC_DATA(Py_mod_name, "_convert"),
111+
PySlot_STATIC_DATA(Py_mod_methods, _convert_methods),
112+
PySlot_DATA(Py_mod_gil, Py_MOD_GIL_NOT_USED),
113+
PySlot_DATA(Py_mod_multiple_interpreters,
114+
Py_MOD_PER_INTERPRETER_GIL_SUPPORTED),
115+
PySlot_END
116+
};
117+
118+
PyMODEXPORT_FUNC
119+
PyModExport__convert(void)
120+
{
121+
return _convert_slot_array;
122+
}
123+
124+
#else
125+
105126
static PyModuleDef_Slot _convert_slots[] = {
106127
#if PY_VERSION_HEX >= 0x030D0000 && !defined(Py_LIMITED_API)
107128
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
@@ -124,3 +145,4 @@ PyInit__convert(void)
124145
{
125146
return PyModuleDef_Init(&_convert_module);
126147
}
148+
#endif

‎setup.py‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,47 @@
33
import sysconfig
44

55
from setuptools import Extension, setup
6+
from setuptools import __version__ as _setuptools_ver
67

78
# Load version string
89
with open(Path(__file__).parent / 'openslide/_version.py') as _fh:
910
exec(_fh.read()) # instantiates __version__ # noqa: S102
1011

11-
# use the Stable ABI on Python 3.11+ on GIL builds; build release-specific
12-
# wheels on older or free-threaded Python
13-
_abi3 = sys.version_info >= (3, 11) and not sysconfig.get_config_var('Py_GIL_DISABLED')
12+
# Use the free-threaded Stable ABI (abi3t) only on free-threaded Python
13+
# 3.15+ with setuptools 84.2+; Python supports building abi3t from
14+
# GIL-enabled interpreters but setuptools doesn't yet. Use the
15+
# non-free-threaded Stable API (abi3) on Python 3.11+ with GIL enabled, and
16+
# release-specific wheels in all other cases.
17+
_freethread = sysconfig.get_config_var('Py_GIL_DISABLED')
18+
_abi3t = (
19+
sys.version_info >= (3, 15)
20+
and _freethread
21+
and tuple(int(c) for c in _setuptools_ver.split('.')) >= (84, 2)
22+
)
23+
_abi3 = sys.version_info >= (3, 11) and not _freethread
1424

1525
setup(
1626
ext_modules=[
1727
Extension(
1828
'openslide._convert',
1929
['openslide/_convert.c'],
2030
# hide symbols that aren't in the Limited API
21-
define_macros=[('Py_LIMITED_API', '0x030b0000')] if _abi3 else [],
31+
define_macros=[('Py_TARGET_ABI3T', '0x030f0000')]
32+
if _abi3t
33+
else [('Py_LIMITED_API', '0x030b0000')]
34+
if _abi3
35+
else [],
2236
# tag extension module for Stable ABI
23-
py_limited_api=_abi3,
37+
py_limited_api=_abi3 or _abi3t,
2438
),
2539
],
2640
options={
2741
# tag wheel for Stable ABI
28-
'bdist_wheel': {'py_limited_api': 'cp311'} if _abi3 else {},
42+
'bdist_wheel': {'py_limited_api': 'cp315'}
43+
if _abi3t
44+
else {'py_limited_api': 'cp311'}
45+
if _abi3
46+
else {},
2947
},
3048
#
3149
# setuptools < 61 compatibility for distro packages building from source

0 commit comments

Comments
 (0)