Skip to content

Commit ff91a11

Browse files
committed
chore(setup-py): Enable ABI3 for Python 3.11+ builds
1 parent 37acc12 commit ff91a11

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

setup.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ def run(self):
7474
try: os.remove(g)
7575
except OSError: pass
7676

77+
command_classes = {
78+
'clean' : Clean,
79+
}
7780

7881
# Platform information
7982
noc99 = sys.version_info[0] == 3 and sys.version_info[1] <= 4
83+
stable_abi = sys.version_info[0] == 3 and sys.version_info[1] >= 11
8084
windows = sys.platform.startswith("win")
8185
testing = 'INDEXED_GZIP_TESTING' in os.environ
8286
thisdir = op.dirname(__file__)
@@ -96,6 +100,7 @@ def run(self):
96100
have_numpy = True
97101

98102
try:
103+
import Cython
99104
from Cython.Build import cythonize
100105
except Exception:
101106
have_cython = False
@@ -117,6 +122,10 @@ def run(self):
117122
print(' testing: {} (if True, code will be compiled with line '
118123
'tracing enabled)'.format(testing))
119124

125+
if stable_abi and have_cython:
126+
cython_version_info = tuple(map(int, Cython.__version__.split('.')[:2]))
127+
# Cython 3.1 is/will be the first version to support the stable ABI
128+
stable_abi = cython_version_info >= (3, 1)
120129

121130
# compile flags
122131
include_dirs = ['indexed_gzip']
@@ -128,6 +137,11 @@ def run(self):
128137
define_macros = [
129138
('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION'),
130139
]
140+
if stable_abi:
141+
define_macros += [
142+
('CYTHON_LIMITED_API', '1'),
143+
('Py_LIMITED_API', '0x030b0000'),
144+
]
131145

132146
if ZLIB_HOME is not None:
133147
include_dirs.append(ZLIB_HOME)
@@ -174,7 +188,9 @@ def run(self):
174188
library_dirs=lib_dirs,
175189
include_dirs=include_dirs,
176190
extra_compile_args=extra_compile_args,
177-
define_macros=define_macros)
191+
define_macros=define_macros,
192+
py_limited_api=stable_abi,
193+
)
178194

179195
# Optional test modules
180196
test_exts = []
@@ -190,7 +206,9 @@ def run(self):
190206
library_dirs=lib_dirs,
191207
include_dirs=include_dirs,
192208
extra_compile_args=extra_compile_args,
193-
define_macros=define_macros))
209+
define_macros=define_macros,
210+
py_limited_api=stable_abi,
211+
))
194212

195213
# If we have numpy, we can compile the tests
196214
if have_numpy: extensions = [igzip_ext] + test_exts
@@ -201,8 +219,20 @@ def run(self):
201219
if have_cython:
202220
extensions = cythonize(extensions, compiler_directives=compiler_directives)
203221

222+
if stable_abi:
223+
from wheel.bdist_wheel import bdist_wheel
224+
225+
class bdist_wheel_abi3(bdist_wheel):
226+
def get_tag(self):
227+
python, abi, plat = super().get_tag()
228+
if python.startswith('cp3'):
229+
python, abi = 'cp311', 'abi3'
230+
return python, abi, plat
231+
232+
command_classes['bdist_wheel'] = bdist_wheel_abi3
233+
204234
setup(
205235
name='indexed_gzip',
206-
cmdclass={'clean' : Clean},
236+
cmdclass=command_classes,
207237
ext_modules=extensions,
208238
)

0 commit comments

Comments
 (0)