Skip to content

Commit

Permalink
Merge pull request #495 from isuruf/freethreading
Browse files Browse the repository at this point in the history
Freethreading support
  • Loading branch information
isuruf authored Oct 1, 2024
2 parents 562ff08 + 170e7de commit bf8f69a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
32 changes: 23 additions & 9 deletions cmake/FindPython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,36 @@ execute_process(
string(STRIP ${PYTHON_LIB_PATH} PYTHON_LIB_PATH)

execute_process(
COMMAND ${PYTHON_BIN} -c "import sys; print(sys.prefix)"
OUTPUT_VARIABLE PYTHON_PREFIX_PATH
)
COMMAND ${PYTHON_BIN} -c "import sys; print(sys.prefix)"
OUTPUT_VARIABLE PYTHON_PREFIX_PATH
)

string(STRIP ${PYTHON_PREFIX_PATH} PYTHON_PREFIX_PATH)

execute_process(
COMMAND ${PYTHON_BIN} -c "import sys; print('%s.%s' % sys.version_info[:2])"
COMMAND ${PYTHON_BIN} -c "import sys; print('%s.%s' % sys.version_info[:2])"
OUTPUT_VARIABLE PYTHON_VERSION
)
)
string(STRIP ${PYTHON_VERSION} PYTHON_VERSION)
message(STATUS "Python version: ${PYTHON_VERSION}")

string(REPLACE "." "" PYTHON_VERSION_WITHOUT_DOTS ${PYTHON_VERSION})

execute_process(
COMMAND ${PYTHON_BIN} -c "import sysconfig;print(bool(sysconfig.get_config_var('Py_GIL_DISABLED')))"
OUTPUT_VARIABLE PY_GIL_DISABLED
)
string(STRIP ${PY_GIL_DISABLED} PY_GIL_DISABLED)

if ("${PY_GIL_DISABLED}" STREQUAL "True")
set (PY_THREAD "t")
endif()

if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
FIND_LIBRARY(PYTHON_LIBRARY NAMES
python${PYTHON_VERSION}
python${PYTHON_VERSION}${PY_THREAD}
python${PYTHON_VERSION}m
python${PYTHON_VERSION_WITHOUT_DOTS}
python${PYTHON_VERSION_WITHOUT_DOTS}${PY_THREAD}
PATHS ${PYTHON_LIB_PATH} ${PYTHON_PREFIX_PATH}/lib ${PYTHON_PREFIX_PATH}/libs
PATH_SUFFIXES ${CMAKE_LIBRARY_ARCHITECTURE}
NO_DEFAULT_PATH
Expand All @@ -51,8 +61,8 @@ endif()

execute_process(
COMMAND ${PYTHON_BIN} -c "from sysconfig import get_paths; print(get_paths()['purelib'])"
OUTPUT_VARIABLE PYTHON_INSTALL_PATH_tmp
)
OUTPUT_VARIABLE PYTHON_INSTALL_PATH_tmp
)
string(STRIP ${PYTHON_INSTALL_PATH_tmp} PYTHON_INSTALL_PATH_tmp)
set(PYTHON_INSTALL_PATH ${PYTHON_INSTALL_PATH_tmp}
CACHE BOOL "Python install path")
Expand Down Expand Up @@ -129,5 +139,9 @@ macro(ADD_PYTHON_LIBRARY name)
IF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(${name} ${PYTHON_LIBRARY})
set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
IF("${PY_GIL_DISABLED}" STREQUAL "True")
target_compile_definitions(${name} PRIVATE Py_GIL_DISABLED=1)
ENDIF()
ENDIF()

endmacro(ADD_PYTHON_LIBRARY)
4 changes: 2 additions & 2 deletions cmake/cython_test.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ cdef extern from "<symengine/symbol.h>" namespace "SymEngine":
string get_name() nogil

cdef extern from "<symengine/add.h>" namespace "SymEngine":
cdef RCP[Basic] add(RCP[Basic] &a, RCP[Basic] &b) nogil except+
cdef RCP[Basic] sub(RCP[Basic] &a, RCP[Basic] &b) nogil except+
cdef RCP[Basic] add(RCP[Basic] &a, RCP[Basic] &b) except+ nogil
cdef RCP[Basic] sub(RCP[Basic] &a, RCP[Basic] &b) except+ nogil

cdef cppclass Add(Basic):
void as_two_terms(const Ptr[RCP[Basic]] &a, const Ptr[RCP[Basic]] &b)

0 comments on commit bf8f69a

Please sign in to comment.