gh-90005: Port _ctypes to PY_STDLIB_MOD (GH-32229)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>

Automerge-Triggered-By: GH:tiran
This commit is contained in:
Christian Heimes 2022-06-26 13:04:43 +02:00 committed by GitHub
parent cc0d5e568e
commit bb8b931385
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 660 additions and 147 deletions

115
setup.py
View file

@ -395,11 +395,6 @@ class PyBuildExt(build_ext):
# Remove modules that are present on the disabled list
extensions = [ext for ext in self.extensions
if ext.name not in DISABLED_MODULE_LIST]
# move ctypes to the end, it depends on other modules
ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
if "_ctypes" in ext_map:
ctypes = extensions.pop(ext_map["_ctypes"])
extensions.append(ctypes)
self.extensions = extensions
def update_sources_depends(self):
@ -600,12 +595,6 @@ class PyBuildExt(build_ext):
raise RuntimeError("Failed to build some stdlib modules")
def build_extension(self, ext):
if ext.name == '_ctypes':
if not self.configure_ctypes(ext):
self.failed.append(ext.name)
return
try:
build_ext.build_extension(self, ext)
except (CCompilerError, DistutilsError) as why:
@ -1370,102 +1359,24 @@ class PyBuildExt(build_ext):
def detect_tkinter(self):
self.addext(Extension('_tkinter', ['_tkinter.c', 'tkappinit.c']))
def configure_ctypes(self, ext):
return True
def detect_ctypes(self):
# Thomas Heller's _ctypes module
src = [
'_ctypes/_ctypes.c',
'_ctypes/callbacks.c',
'_ctypes/callproc.c',
'_ctypes/stgdict.c',
'_ctypes/cfield.c',
]
malloc_closure = sysconfig.get_config_var(
"MODULE__CTYPES_MALLOC_CLOSURE"
)
if malloc_closure:
src.append(malloc_closure)
if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
self.use_system_libffi = True
else:
self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
include_dirs = []
extra_compile_args = []
extra_link_args = []
sources = ['_ctypes/_ctypes.c',
'_ctypes/callbacks.c',
'_ctypes/callproc.c',
'_ctypes/stgdict.c',
'_ctypes/cfield.c']
if MACOS:
sources.append('_ctypes/malloc_closure.c')
extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
extra_compile_args.append('-DMACOSX')
include_dirs.append('_ctypes/darwin')
elif HOST_PLATFORM == 'sunos5':
# XXX This shouldn't be necessary; it appears that some
# of the assembler code is non-PIC (i.e. it has relocations
# when it shouldn't. The proper fix would be to rewrite
# the assembler code to be PIC.
# This only works with GCC; the Sun compiler likely refuses
# this option. If you want to compile ctypes with the Sun
# compiler, please research a proper solution, instead of
# finding some -z option for the Sun compiler.
extra_link_args.append('-mimpure-text')
ext = Extension('_ctypes',
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
libraries=[],
sources=sources)
self.add(ext)
# function my_sqrt() needs libm for sqrt()
self.addext(Extension('_ctypes', src))
self.addext(Extension('_ctypes_test', ['_ctypes/_ctypes_test.c']))
ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
ffi_lib = None
ffi_inc_dirs = self.inc_dirs.copy()
if MACOS:
ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
if not ffi_inc:
if os.path.exists(ffi_in_sdk):
ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
ffi_inc = ffi_in_sdk
ffi_lib = 'ffi'
else:
# OS X 10.5 comes with libffi.dylib; the include files are
# in /usr/include/ffi
ffi_inc_dirs.append('/usr/include/ffi')
if not ffi_inc:
found = find_file('ffi.h', [], ffi_inc_dirs)
if found:
ffi_inc = found[0]
if ffi_inc:
ffi_h = ffi_inc + '/ffi.h'
if not os.path.exists(ffi_h):
ffi_inc = None
print('Header file {} does not exist'.format(ffi_h))
if ffi_lib is None and ffi_inc:
for lib_name in ('ffi', 'ffi_pic'):
if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
ffi_lib = lib_name
break
if ffi_inc and ffi_lib:
ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
if grep_headers_for('ffi_prep_cif_var', ffi_headers):
ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
if grep_headers_for('ffi_closure_alloc', ffi_headers):
ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
ext.include_dirs.append(ffi_inc)
ext.libraries.append(ffi_lib)
self.use_system_libffi = True
if sysconfig.get_config_var('HAVE_LIBDL'):
# for dlopen, see bpo-32647
ext.libraries.append('dl')
def detect_decimal(self):
# Stefan Krah's _decimal module
self.addext(