Skip to content

Commit ec3c86e

Browse files
committed
port _testbuffer to multi-phase
1 parent a02efe4 commit ec3c86e

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

Modules/_testbuffer.c

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,48 +2813,29 @@ static struct PyMethodDef _testbuffer_functions[] = {
28132813
{NULL, NULL}
28142814
};
28152815

2816-
static struct PyModuleDef _testbuffermodule = {
2817-
PyModuleDef_HEAD_INIT,
2818-
"_testbuffer",
2819-
NULL,
2820-
-1,
2821-
_testbuffer_functions,
2822-
NULL,
2823-
NULL,
2824-
NULL,
2825-
NULL
2826-
};
2827-
2828-
2829-
PyMODINIT_FUNC
2830-
PyInit__testbuffer(void)
2816+
static int
2817+
_testbuffer_exec(PyObject *m)
28312818
{
2832-
PyObject *m;
2833-
2834-
m = PyModule_Create(&_testbuffermodule);
2835-
if (m == NULL)
2836-
return NULL;
2837-
2838-
Py_SET_TYPE(&NDArray_Type, &PyType_Type);
2839-
Py_INCREF(&NDArray_Type);
2840-
PyModule_AddObject(m, "ndarray", (PyObject *)&NDArray_Type);
2819+
if (PyModule_AddType(m, &NDArray_Type) < 0) {
2820+
return -1;
2821+
}
28412822

2842-
Py_SET_TYPE(&StaticArray_Type, &PyType_Type);
2843-
Py_INCREF(&StaticArray_Type);
2844-
PyModule_AddObject(m, "staticarray", (PyObject *)&StaticArray_Type);
2823+
if (PyModule_AddType(m, &StaticArray_Type) < 0) {
2824+
return -1;
2825+
}
28452826

28462827
structmodule = PyImport_ImportModule("struct");
28472828
if (structmodule == NULL)
2848-
return NULL;
2829+
return -1;
28492830

28502831
Struct = PyObject_GetAttrString(structmodule, "Struct");
28512832
calcsize = PyObject_GetAttrString(structmodule, "calcsize");
28522833
if (Struct == NULL || calcsize == NULL)
2853-
return NULL;
2834+
return -1;
28542835

28552836
simple_format = PyUnicode_FromString(simple_fmt);
28562837
if (simple_format == NULL)
2857-
return NULL;
2838+
return -1;
28582839

28592840
PyModule_AddIntMacro(m, ND_MAX_NDIM);
28602841
PyModule_AddIntMacro(m, ND_VAREXPORT);
@@ -2887,8 +2868,24 @@ PyInit__testbuffer(void)
28872868
PyModule_AddIntMacro(m, PyBUF_READ);
28882869
PyModule_AddIntMacro(m, PyBUF_WRITE);
28892870

2890-
return m;
2871+
return 0;
28912872
}
28922873

2874+
static PyModuleDef_Slot _testbuffer_slots[] = {
2875+
{Py_mod_exec, _testbuffer_exec},
2876+
{0, NULL}
2877+
};
28932878

2879+
static struct PyModuleDef _testbuffermodule = {
2880+
PyModuleDef_HEAD_INIT,
2881+
.m_name = "_testbuffer",
2882+
.m_size = 0,
2883+
.m_methods = _testbuffer_functions,
2884+
.m_slots = _testbuffer_slots
2885+
};
28942886

2887+
PyMODINIT_FUNC
2888+
PyInit__testbuffer(void)
2889+
{
2890+
return PyModuleDef_Init(&_testbuffermodule);
2891+
}

0 commit comments

Comments
 (0)