Skip to content

Commit 3608d6c

Browse files
committed
Support Python 3.15 Free Threading
1 parent baf7f53 commit 3608d6c

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎tests/extension.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,44 @@
1212
#define _CONCAT(a, b) a ## b
1313
#define CONCAT(a, b) _CONCAT(a, b)
1414

15+
// Use PySlot API on Python 3.15 and newer and on limited C API 3.15 and newer
16+
#if (PY_VERSION_HEX >= _Py_PACK_VERSION(3, 15) \
17+
&& (!defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)))
18+
19+
PyABIInfo_VAR(abi_info);
20+
21+
static PySlot module_slots[] = {
22+
PySlot_PTR_STATIC(Py_mod_abi, &abi_info),
23+
PySlot_PTR_STATIC(Py_mod_name, (void*)STR(MODULE_NAME)),
24+
PySlot_PTR_STATIC(Py_mod_state_size, 0),
25+
PySlot_PTR_STATIC(Py_mod_gil, Py_MOD_GIL_NOT_USED),
26+
{0, 0, {0}, {0}}
27+
};
28+
29+
#define EXPORT_FUNC CONCAT(PyModExport_, MODULE_NAME)
30+
31+
PyMODEXPORT_FUNC
32+
EXPORT_FUNC(void)
33+
{
34+
return module_slots;
35+
}
36+
37+
#ifdef MS_WINDOWS
38+
# define INIT_FUNC CONCAT(PyInit_, MODULE_NAME)
39+
40+
// Workaround for https://github.com/pypa/distutils/issues/387
41+
// https://docs.python.org/3.16/howto/abi3t-migration.html#module-export-hook
42+
PyMODINIT_FUNC
43+
INIT_FUNC(void)
44+
{
45+
PyErr_SetString(PyExc_SystemError,
46+
"PyInit_* called for module with PyModExport_*");
47+
return NULL;
48+
}
49+
#endif
50+
51+
#else
52+
1553
// No method and no module exec method needed, most basic C extension
1654

1755
static struct PyModuleDef module_def = {
@@ -34,3 +72,4 @@ INIT_FUNC(void)
3472
{
3573
return PyModule_Create(&module_def);
3674
}
75+
#endif

0 commit comments

Comments
 (0)