Skip to content

Commit ae2526c

Browse files
committed
Support building free-threaded interrogate wheel
For interrogatedb, we just wrap all calls with a mutex for now
1 parent c343350 commit ae2526c

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

cmake/macros/Python.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function(add_python_target target)
6060
add_library(${target} ${MODULE_TYPE} ${sources})
6161
target_link_libraries(${target} PKG::PYTHON)
6262
else()
63-
if(MODULE_TYPE STREQUAL "MODULE")
63+
if(MODULE_TYPE STREQUAL "MODULE" AND NOT PYTHON_FREETHREADED)
6464
Python_add_library(${target} MODULE USE_SABI 3.3 ${sources})
6565
else()
6666
Python_add_library(${target} ${MODULE_TYPE} ${sources})

src/Config.cmake

+18-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ if(Python_FOUND)
8383
set(PYTHON_INCLUDE_DIRS ${Python_INCLUDE_DIRS})
8484
set(PYTHON_LIBRARY_DIRS ${Python_LIBRARY_DIRS})
8585
set(PYTHON_VERSION_STRING ${Python_VERSION})
86+
87+
execute_process(COMMAND ${Python_EXECUTABLE}
88+
-c "import sysconfig;print(sysconfig.get_config_var('Py_GIL_DISABLED') or 0)"
89+
OUTPUT_VARIABLE PYTHON_FREETHREADED
90+
OUTPUT_STRIP_TRAILING_WHITESPACE)
91+
if(PYTHON_FREETHREADED)
92+
# The free-threaded build has no stable ABI (yet).
93+
set(_IMPORTED_AS Python::Module)
94+
endif()
8695
endif()
8796

8897
if(CMAKE_VERSION VERSION_LESS "3.15")
@@ -162,7 +171,15 @@ if(HAVE_PYTHON)
162171

163172
endif()
164173

165-
if(CYGWIN)
174+
if(PYTHON_FREETHREADED)
175+
if(CYGWIN)
176+
set(_EXT_SUFFIX ".${Python_SOABI}.dll")
177+
elseif(WIN32)
178+
set(_EXT_SUFFIX ".${Python_SOABI}.pyd")
179+
else()
180+
set(_EXT_SUFFIX ".${Python_SOABI}.so")
181+
endif()
182+
elseif(CYGWIN)
166183
set(_EXT_SUFFIX ".dll")
167184
elseif(WIN32)
168185
set(_EXT_SUFFIX ".pyd")

src/interrogate/interfaceMakerPythonSimple.cxx

+21-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ InterfaceMakerPythonSimple::
4848
*/
4949
void InterfaceMakerPythonSimple::
5050
write_includes(std::ostream &out) {
51-
out << "#if PY_VERSION_HEX >= 0x30300000\n";
51+
out << "#if PY_VERSION_HEX >= 0x30300000 && !defined(Py_GIL_DISABLED)\n";
5252
out << "#define Py_LIMITED_API 0x30300000\n";
5353
out << "#endif\n\n";
5454
InterfaceMakerPython::write_includes(out);
55+
out << "\n#ifdef Py_GIL_DISABLED\n";
56+
out << "static PyMutex mutex;\n";
57+
out << "#endif\n\n";
5558
}
5659

5760
/**
@@ -132,7 +135,11 @@ write_module(ostream &out,ostream *out_h, InterrogateModuleDef *def) {
132135

133136
<< "INIT_FUNC() {\n"
134137
<< "#if PY_MAJOR_VERSION >= 3\n"
135-
<< " return PyModule_Create(&python_simple_module);\n"
138+
<< " PyObject *module = PyModule_Create(&python_simple_module);\n"
139+
<< "#ifdef Py_GIL_DISABLED\n"
140+
<< " PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);\n"
141+
<< "#endif\n"
142+
<< " return module;\n"
136143
<< "#else\n"
137144
<< " Py_InitModule(\"" << def->library_name << "\", python_simple_funcs);\n"
138145
<< "#endif\n"
@@ -380,6 +387,9 @@ void InterfaceMakerPythonSimple::write_function_instance(ostream &out, Interface
380387
<< " }\n";
381388
}
382389

390+
out << "#ifdef Py_GIL_DISABLED\n";
391+
out << " PyMutex_Lock(&mutex);\n";
392+
out << "#endif\n";
383393
if (track_interpreter) {
384394
out << " in_interpreter = 0;\n";
385395
}
@@ -397,6 +407,9 @@ void InterfaceMakerPythonSimple::write_function_instance(ostream &out, Interface
397407
if (track_interpreter) {
398408
out << " in_interpreter = 1;\n";
399409
}
410+
out << "#ifdef Py_GIL_DISABLED\n";
411+
out << " PyMutex_Unlock(&mutex);\n";
412+
out << "#endif\n";
400413
if (!extra_cleanup.empty()) {
401414
out << " " << extra_cleanup << "\n";
402415
}
@@ -411,6 +424,9 @@ void InterfaceMakerPythonSimple::write_function_instance(ostream &out, Interface
411424
if (track_interpreter) {
412425
out << " in_interpreter = 1;\n";
413426
}
427+
out << "#ifdef Py_GIL_DISABLED\n";
428+
out << " PyMutex_Unlock(&mutex);\n";
429+
out << "#endif\n";
414430
if (!extra_cleanup.empty()) {
415431
out << " " << extra_cleanup << "\n";
416432
}
@@ -425,6 +441,9 @@ void InterfaceMakerPythonSimple::write_function_instance(ostream &out, Interface
425441
if (track_interpreter) {
426442
out << " in_interpreter = 1;\n";
427443
}
444+
out << "#ifdef Py_GIL_DISABLED\n";
445+
out << " PyMutex_Unlock(&mutex);\n";
446+
out << "#endif\n";
428447
if (!extra_cleanup.empty()) {
429448
out << " " << extra_cleanup << "\n";
430449
}

0 commit comments

Comments
 (0)