Skip to content

Commit

Permalink
chore(setup-py): Enable ABI3 for Python 3.11+ builds
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jul 16, 2024
1 parent 37acc12 commit ff91a11
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ def run(self):
try: os.remove(g)
except OSError: pass

command_classes = {
'clean' : Clean,
}

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

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

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

# compile flags
include_dirs = ['indexed_gzip']
Expand All @@ -128,6 +137,11 @@ def run(self):
define_macros = [
('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION'),
]
if stable_abi:
define_macros += [
('CYTHON_LIMITED_API', '1'),
('Py_LIMITED_API', '0x030b0000'),
]

if ZLIB_HOME is not None:
include_dirs.append(ZLIB_HOME)
Expand Down Expand Up @@ -174,7 +188,9 @@ def run(self):
library_dirs=lib_dirs,
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
define_macros=define_macros)
define_macros=define_macros,
py_limited_api=stable_abi,
)

# Optional test modules
test_exts = []
Expand All @@ -190,7 +206,9 @@ def run(self):
library_dirs=lib_dirs,
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
define_macros=define_macros))
define_macros=define_macros,
py_limited_api=stable_abi,
))

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

if stable_abi:
from wheel.bdist_wheel import bdist_wheel

class bdist_wheel_abi3(bdist_wheel):
def get_tag(self):
python, abi, plat = super().get_tag()
if python.startswith('cp3'):
python, abi = 'cp311', 'abi3'
return python, abi, plat

command_classes['bdist_wheel'] = bdist_wheel_abi3

setup(
name='indexed_gzip',
cmdclass={'clean' : Clean},
cmdclass=command_classes,
ext_modules=extensions,
)

0 comments on commit ff91a11

Please sign in to comment.