Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf081b7e

Browse files
committed
Convert extbuild to multi-phase init (PEP 489)
1 parentb2b54a9 commitf081b7e

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

‎numpy/testing/_private/extbuild.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,15 @@ def build_and_import_extension(
5656
ifinclude_dirsisNone:
5757
include_dirs= []
5858
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=""
6560
ifnotbuild_dir:
6661
build_dir=pathlib.Path('.')
6762
ifmore_init:
6863
init+="""#define INITERROR return NULL
6964
"""
7065
init+=more_init
71-
init+="\nreturnmod;"
72-
source_string=_make_source(modname,init,body)
66+
init+="\nreturn0;"
67+
source_string=_make_source(modname,init,body,modname)
7368
mod_so=compile_extension_module(
7469
modname,build_dir,include_dirs,source_string)
7570
importimportlib.util
@@ -154,31 +149,45 @@ def _make_methods(functions, modname):
154149
%(methods)s
155150
{ NULL }
156151
};
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)}
165153
returnbody
166154

167155

168-
def_make_source(name,init,body):
156+
def_make_source(name,init,body,modname):
169157
""" Combines the code fragments into source code ready to be compiled
170158
"""
171159
code="""
172160
#include <Python.h>
173161
174162
%(body)s
175163
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+
176185
PyMODINIT_FUNC
177186
PyInit_%(name)s(void) {
178-
%(init)s
187+
return PyModuleDef_Init(&moduledef);
179188
}
180189
"""% {
181-
'name':name,'init':init,'body':body,
190+
'name':name,'init':init,'body':body,'modname':modname,
182191
}
183192
returncode
184193

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp