-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-1635741 port _testbuffer to multi-phase init #22003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Port the :mod:`_testbuffer` extension module to multi-phase initialization | ||
(:pep:`489`). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2813,48 +2813,29 @@ static struct PyMethodDef _testbuffer_functions[] = { | |
{NULL, NULL} | ||
}; | ||
|
||
static struct PyModuleDef _testbuffermodule = { | ||
PyModuleDef_HEAD_INIT, | ||
"_testbuffer", | ||
NULL, | ||
-1, | ||
_testbuffer_functions, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL | ||
}; | ||
|
||
|
||
PyMODINIT_FUNC | ||
PyInit__testbuffer(void) | ||
static int | ||
_testbuffer_exec(PyObject *m) | ||
{ | ||
PyObject *m; | ||
|
||
m = PyModule_Create(&_testbuffermodule); | ||
if (m == NULL) | ||
return NULL; | ||
|
||
Py_SET_TYPE(&NDArray_Type, &PyType_Type); | ||
Py_INCREF(&NDArray_Type); | ||
PyModule_AddObject(m, "ndarray", (PyObject *)&NDArray_Type); | ||
if (PyModule_AddType(m, &NDArray_Type) < 0) { | ||
return -1; | ||
} | ||
|
||
Py_SET_TYPE(&StaticArray_Type, &PyType_Type); | ||
Py_INCREF(&StaticArray_Type); | ||
PyModule_AddObject(m, "staticarray", (PyObject *)&StaticArray_Type); | ||
if (PyModule_AddType(m, &StaticArray_Type) < 0) { | ||
return -1; | ||
} | ||
|
||
structmodule = PyImport_ImportModule("struct"); | ||
if (structmodule == NULL) | ||
return NULL; | ||
return -1; | ||
|
||
Struct = PyObject_GetAttrString(structmodule, "Struct"); | ||
calcsize = PyObject_GetAttrString(structmodule, "calcsize"); | ||
if (Struct == NULL || calcsize == NULL) | ||
return NULL; | ||
return -1; | ||
|
||
simple_format = PyUnicode_FromString(simple_fmt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum, would it be possible to move these static variables to be able to clear them when the extension module is unloaded? It would require to pass the module to NDArray_Type creation, so be able to get the module state from a ndarray instance.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vstinner This seems tricky. How do you recommend passing the module to a static type like NDArray_Type. I can't put the module instance on the type or it will be overridden each time we do _testbuffer_exec. Is there a way to do this in tp_new? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vstinner What if I just call PyUnicode_FromString every time it is used, since as you say this is just a test module. Struct and calcsize should be handled properly in any case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually to convert an extension module to multiphase init, you must convert static types to heap types first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vstinner see my comment in response to @shihai1991 about this - I was not able to convert these to heap types |
||
if (simple_format == NULL) | ||
return NULL; | ||
return -1; | ||
|
||
PyModule_AddIntMacro(m, ND_MAX_NDIM); | ||
PyModule_AddIntMacro(m, ND_VAREXPORT); | ||
|
@@ -2887,8 +2868,24 @@ PyInit__testbuffer(void) | |
PyModule_AddIntMacro(m, PyBUF_READ); | ||
PyModule_AddIntMacro(m, PyBUF_WRITE); | ||
|
||
return m; | ||
return 0; | ||
} | ||
|
||
static PyModuleDef_Slot _testbuffer_slots[] = { | ||
{Py_mod_exec, _testbuffer_exec}, | ||
{0, NULL} | ||
}; | ||
|
||
static struct PyModuleDef _testbuffermodule = { | ||
PyModuleDef_HEAD_INIT, | ||
.m_name = "_testbuffer", | ||
.m_size = 0, | ||
.m_methods = _testbuffer_functions, | ||
.m_slots = _testbuffer_slots | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
PyInit__testbuffer(void) | ||
{ | ||
return PyModuleDef_Init(&_testbuffermodule); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting _testbuffer to multiphase init allows to create two instances of the module, but the C implementation uses global variables which is incompatible with that:
You must first avoid these global variables. I suggest you to attempt to pass the module to functions using Struct and calcsize, and then get Struct and calcsize from the module. Either use PyObject_GetAttrString() which can slow down the module (which is ok, the module is only used for testing purpose), or use a module state (see my other comment about that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vstinner I am more comfortable with module state, anyways eventually it will need it if we migrate to heap types.