@@ -56,20 +56,15 @@ def build_and_import_extension(
56
56
if include_dirs is None :
57
57
include_dirs = []
58
58
body = prologue + _make_methods (functions ,modname )
59
- init = """
60
- PyObject *mod = PyModule_Create(&moduledef);
61
- #ifdef Py_GIL_DISABLED
62
- PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
63
- #endif
64
- """
59
+ init = ""
65
60
if not build_dir :
66
61
build_dir = pathlib .Path ('.' )
67
62
if more_init :
68
63
init += """#define INITERROR return NULL
69
64
"""
70
65
init += more_init
71
- init += "\n returnmod ;"
72
- source_string = _make_source (modname ,init ,body )
66
+ init += "\n return0 ;"
67
+ source_string = _make_source (modname ,init ,body , modname )
73
68
mod_so = compile_extension_module (
74
69
modname ,build_dir ,include_dirs ,source_string )
75
70
import importlib .util
@@ -154,31 +149,45 @@ def _make_methods(functions, modname):
154
149
%(methods)s
155
150
{ NULL }
156
151
};
157
- static struct PyModuleDef moduledef = {
158
- PyModuleDef_HEAD_INIT,
159
- "%(modname)s", /* m_name */
160
- NULL, /* m_doc */
161
- -1, /* m_size */
162
- methods, /* m_methods */
163
- };
164
- """ % {'methods' :'\n ' .join (methods_table ),'modname' :modname }
152
+ """ % {'methods' :'\n ' .join (methods_table )}
165
153
return body
166
154
167
155
168
- def _make_source (name ,init ,body ):
156
+ def _make_source (name ,init ,body , modname ):
169
157
""" Combines the code fragments into source code ready to be compiled
170
158
"""
171
159
code = """
172
160
#include <Python.h>
173
161
174
162
%(body)s
175
163
164
+ static int
165
+ %(name)s_exec(PyObject *m) {
166
+ %(init)s
167
+ }
168
+
169
+ static struct PyModuleDef_Slot %(name)s_slots[] = {
170
+ {Py_mod_exec, %(name)s_exec},
171
+ {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
172
+ // signal this module supports running with the GIL disabled
173
+ {Py_mod_gil, Py_MOD_GIL_NOT_USED},
174
+ {0, NULL},
175
+ };
176
+
177
+ static struct PyModuleDef moduledef = {
178
+ .m_base = PyModuleDef_HEAD_INIT,
179
+ .m_name = "%(modname)s",
180
+ .m_size = 0,
181
+ .m_methods = methods,
182
+ .m_slots = %(name)s_slots,
183
+ };
184
+
176
185
PyMODINIT_FUNC
177
186
PyInit_%(name)s(void) {
178
- %(init)s
187
+ return PyModuleDef_Init(&moduledef);
179
188
}
180
189
""" % {
181
- 'name' :name ,'init' :init ,'body' :body ,
190
+ 'name' :name ,'init' :init ,'body' :body ,'modname' : modname ,
182
191
}
183
192
return code
184
193