Skip to content

Commit 512781a

Browse files
committed
BLD/RFC: cleanup limited_api build logic
1 parent 0333509 commit 512781a

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

setupext.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import subprocess
88
import sys
99
import tempfile
10+
from copy import deepcopy
1011
from textwrap import dedent
1112
from concurrent.futures import ThreadPoolExecutor
1213
from distutils import sysconfig
1314
from distutils.ccompiler import CCompiler, new_compiler
1415
from distutils.sysconfig import customize_compiler
1516
from subprocess import PIPE, Popen
1617
from sys import platform as _platform
18+
import numpy
1719
import ewah_bool_utils
20+
from Cython.Build import cythonize
1821
from setuptools.command.build_ext import build_ext as _build_ext
1922
from setuptools.command.sdist import sdist as _sdist
2023
from setuptools.errors import CompileError, LinkError
@@ -403,12 +406,18 @@ def get_python_include_dirs():
403406
return include_dirs
404407

405408

409+
include_dirs = [numpy.get_include(), ewah_bool_utils.get_include()]
410+
406411
NUMPY_MACROS = [
407412
("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION"),
408413
# keep in sync with runtime requirements (pyproject.toml)
409414
("NPY_TARGET_VERSION", "NPY_1_21_API_VERSION"),
410415
]
411416

417+
define_macros = deepcopy(NUMPY_MACROS)
418+
if USE_PY_LIMITED_API:
419+
define_macros.append(("Py_LIMITED_API", ABI3_TARGET_HEX))
420+
412421

413422
def create_build_ext(lib_exts, cythonize_aliases):
414423
class build_ext(_build_ext):
@@ -418,40 +427,26 @@ class build_ext(_build_ext):
418427
# pyproject.toml was introduced in the project
419428

420429
def finalize_options(self):
421-
from Cython.Build import cythonize
422-
423430
# Override the list of extension modules
424-
self.distribution.ext_modules[:] = cythonize(
431+
self.extensions = self.distribution.ext_modules = cythonize(
425432
lib_exts,
426433
aliases=cythonize_aliases,
427434
compiler_directives={"language_level": 3},
428435
nthreads=get_cpu_count(),
429436
)
430-
_build_ext.finalize_options(self)
431-
# Prevent numpy from thinking it is still in its setup process
432-
# see http://stackoverflow.com/a/21621493/1382869
433-
if isinstance(__builtins__, dict):
434-
# sometimes this is a dict so we need to check for that
435-
# https://docs.python.org/3/library/builtins.html
436-
__builtins__["__NUMPY_SETUP__"] = False
437-
else:
438-
__builtins__.__NUMPY_SETUP__ = False
439-
import numpy
440-
441-
self.include_dirs.append(numpy.get_include())
442-
self.include_dirs.append(ewah_bool_utils.get_include())
443437

444-
define_macros = NUMPY_MACROS
445-
if USE_PY_LIMITED_API:
446-
define_macros.append(("Py_LIMITED_API", ABI3_TARGET_HEX))
447-
for ext in self.extensions:
448-
ext.py_limited_api = True
438+
if self.include_dirs is None:
439+
self.include_dirs = include_dirs
440+
else:
441+
self.include_dirs.extend(include_dirs)
449442

450443
if self.define is None:
451444
self.define = define_macros
452445
else:
453446
self.define.extend(define_macros)
454447

448+
super().finalize_options()
449+
455450
def build_extensions(self):
456451
self.check_extensions_list(self.extensions)
457452

0 commit comments

Comments
 (0)