Skip to content

Commit 4f77e65

Browse files
committed
[GR-70824] Remove intrinsified _ctypes.
PullRequest: graalpython/4055
2 parents 21fbfeb + dcd8ffa commit 4f77e65

File tree

99 files changed

+12355
-15275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+12355
-15275
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ language runtime. The main focus is on user-observable behavior of the engine.
77
* Intern string literals in source files
88
* Allocation reporting via Truffle has been removed. Python object sizes were never reported correctly, so the data was misleading and there was a non-neglible overhead for object allocations even when reporting was inactive.
99
* Better `readline` support via JLine. Autocompletion and history now works in `pdb`
10+
* Remove the intrinsified _ctypes module in favor of the native CPython version. This makes GraalPy's ctypes implementation more compatible and reduces the memory footprint of using ctypes.
1011

1112
## Version 25.0.1
1213
* Allow users to keep going on unsupported JDK/OS/ARCH combinations at their own risk by opting out of early failure using `-Dtruffle.UseFallbackRuntime=true`, `-Dpolyglot.engine.userResourceCache=/set/to/a/writeable/dir`, `-Dpolyglot.engine.allowUnsupportedPlatform=true`, and `-Dpolyglot.python.UnsupportedPlatformEmulates=[linux|macos|windows]` and `-Dorg.graalvm.python.resources.exclude=native.files`.

graalpython/com.oracle.graal.python.cext/CMakeLists.txt

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ if(WIN32)
323323
endif()
324324
simple_native_module("_testmultiphase")
325325
simple_native_module("_testsinglephase")
326-
simple_native_module("_ctypes_test")
327326

328327
if(NOT WIN32)
329328
###################### BZIP2 ########################
@@ -358,6 +357,76 @@ target_compile_definitions(${TARGET_PYEXPAT} PRIVATE
358357
XML_DTD=1
359358
)
360359

360+
###################### CTYPES ######################
361+
native_module("_ctypes_test" TRUE "${SRC_DIR}/modules/_ctypes/_ctypes_test.c")
362+
set(CTYPES_SRC
363+
"${SRC_DIR}/modules/_ctypes/_ctypes.c"
364+
"${SRC_DIR}/modules/_ctypes/callbacks.c"
365+
"${SRC_DIR}/modules/_ctypes/callproc.c"
366+
"${SRC_DIR}/modules/_ctypes/cfield.c"
367+
"${SRC_DIR}/modules/_ctypes/ctypes.h"
368+
"${SRC_DIR}/modules/_ctypes/stgdict.c"
369+
)
370+
if(APPLE)
371+
set(CTYPES_SRC ${CTYPES_SRC} "${SRC_DIR}/modules/_ctypes/malloc_closure.c")
372+
endif()
373+
native_module("_ctypes" TRUE "${CTYPES_SRC}")
374+
target_include_directories("_ctypes" PUBLIC "${SRC_DIR}/modules/_ctypes")
375+
find_library(FFI_LIBRARY
376+
NAMES "ffi.lib" "libffi.a"
377+
PATHS "${LIBFFI_DIST}"
378+
REQUIRED
379+
NO_DEFAULT_PATH
380+
NO_PACKAGE_ROOT_PATH
381+
NO_CMAKE_PATH
382+
NO_CMAKE_ENVIRONMENT_PATH
383+
NO_SYSTEM_ENVIRONMENT_PATH
384+
NO_CMAKE_SYSTEM_PATH
385+
NO_CMAKE_INSTALL_PREFIX
386+
)
387+
find_path(FFI_INCLUDE
388+
NAMES "ffi.h"
389+
PATHS "${LIBFFI_DIST}"
390+
PATH_SUFFIXES "include"
391+
REQUIRED
392+
NO_DEFAULT_PATH
393+
NO_PACKAGE_ROOT_PATH
394+
NO_CMAKE_PATH
395+
NO_CMAKE_ENVIRONMENT_PATH
396+
NO_SYSTEM_ENVIRONMENT_PATH
397+
NO_CMAKE_SYSTEM_PATH
398+
NO_CMAKE_INSTALL_PREFIX
399+
)
400+
target_link_libraries("_ctypes" PRIVATE ${FFI_LIBRARY})
401+
402+
target_include_directories("_ctypes" BEFORE PRIVATE ${FFI_INCLUDE})
403+
404+
if(WIN32)
405+
target_compile_options("_ctypes" PRIVATE
406+
/wd4201 # ffi.h(332): warning C4201: nonstandard extension used: nameless struct/union
407+
/wd4457 # _ctypes.c(4783): warning C4457: declaration of 'item' hides function parameter
408+
)
409+
target_compile_definitions("_ctypes" PRIVATE
410+
MS_WIN32
411+
MS_WIN64
412+
_WIN64
413+
FFI_STATIC_BUILD
414+
)
415+
elseif(APPLE)
416+
target_compile_definitions("_ctypes" PRIVATE
417+
HAVE_DECL_RTLD_LOCAL
418+
HAVE_DECL_RTLD_GLOBAL
419+
HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
420+
USING_MALLOC_CLOSURE_DOT_C
421+
)
422+
else()
423+
target_compile_definitions("_ctypes" PRIVATE
424+
HAVE_DECL_RTLD_LOCAL
425+
HAVE_DECL_RTLD_GLOBAL
426+
)
427+
endif()
428+
429+
###################### LIBPYTHON ######################
361430
target_sources(${TARGET_LIBPYTHON} PRIVATE ${SRC_FILES})
362431
target_include_directories(${TARGET_LIBPYTHON} PRIVATE
363432
"${SRC_DIR}/include/internal"

graalpython/com.oracle.graal.python.cext/include/cpython/dictobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2

0 commit comments

Comments
 (0)