Skip to content

Commit 1d541c2

Browse files
authored
bpo-1635741: Port multiprocessing ext to multiphase init (GH-21378)
Port the _multiprocessing extension module to multiphase initialization (PEP 489).
1 parent 490c542 commit 1d541c2

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port :mod:`multiprocessing` to multi-phase initialization

Modules/_multiprocessing/multiprocessing.c

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,17 @@ static PyMethodDef module_methods[] = {
183183
* Initialize
184184
*/
185185

186-
static struct PyModuleDef multiprocessing_module = {
187-
PyModuleDef_HEAD_INIT,
188-
"_multiprocessing",
189-
NULL,
190-
-1,
191-
module_methods,
192-
NULL,
193-
NULL,
194-
NULL,
195-
NULL
196-
};
197-
198-
199-
PyMODINIT_FUNC
200-
PyInit__multiprocessing(void)
186+
static int
187+
multiprocessing_exec(PyObject *module)
201188
{
202-
PyObject *module, *temp, *value = NULL;
203-
204-
/* Initialize module */
205-
module = PyModule_Create(&multiprocessing_module);
206-
if (!module)
207-
return NULL;
208-
209189
#if defined(MS_WINDOWS) || \
210190
(defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
191+
211192
/* Add _PyMp_SemLock type to module */
212-
if (PyType_Ready(&_PyMp_SemLockType) < 0)
213-
return NULL;
214-
Py_INCREF(&_PyMp_SemLockType);
193+
if (PyModule_AddType(module, &_PyMp_SemLockType) < 0) {
194+
return -1;
195+
}
196+
215197
{
216198
PyObject *py_sem_value_max;
217199
/* Some systems define SEM_VALUE_MAX as an unsigned value that
@@ -223,25 +205,41 @@ PyInit__multiprocessing(void)
223205
py_sem_value_max = PyLong_FromLong(INT_MAX);
224206
else
225207
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
226-
if (py_sem_value_max == NULL)
227-
return NULL;
228-
PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
229-
py_sem_value_max);
208+
209+
if (py_sem_value_max == NULL) {
210+
Py_DECREF(py_sem_value_max);
211+
return -1;
212+
}
213+
if (PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
214+
py_sem_value_max) < 0) {
215+
Py_DECREF(py_sem_value_max);
216+
return -1;
217+
}
218+
Py_DECREF(py_sem_value_max);
230219
}
231-
PyModule_AddObject(module, "SemLock", (PyObject*)&_PyMp_SemLockType);
220+
232221
#endif
233222

234223
/* Add configuration macros */
235-
temp = PyDict_New();
236-
if (!temp)
237-
return NULL;
224+
PyObject *flags = PyDict_New();
225+
if (!flags) {
226+
return -1;
227+
}
238228

239-
#define ADD_FLAG(name) \
240-
value = Py_BuildValue("i", name); \
241-
if (value == NULL) { Py_DECREF(temp); return NULL; } \
242-
if (PyDict_SetItemString(temp, #name, value) < 0) { \
243-
Py_DECREF(temp); Py_DECREF(value); return NULL; } \
244-
Py_DECREF(value)
229+
#define ADD_FLAG(name) \
230+
do { \
231+
PyObject *value = PyLong_FromLong(name); \
232+
if (value == NULL) { \
233+
Py_DECREF(flags); \
234+
return -1; \
235+
} \
236+
if (PyDict_SetItemString(flags, #name, value) < 0) { \
237+
Py_DECREF(flags); \
238+
Py_DECREF(value); \
239+
return -1; \
240+
} \
241+
Py_DECREF(value); \
242+
} while (0)
245243

246244
#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
247245
ADD_FLAG(HAVE_SEM_OPEN);
@@ -256,8 +254,28 @@ PyInit__multiprocessing(void)
256254
ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
257255
#endif
258256

259-
if (PyModule_AddObject(module, "flags", temp) < 0)
260-
return NULL;
257+
if (PyModule_AddObject(module, "flags", flags) < 0) {
258+
Py_DECREF(flags);
259+
return -1;
260+
}
261+
262+
return 0;
263+
}
264+
265+
static PyModuleDef_Slot multiprocessing_slots[] = {
266+
{Py_mod_exec, multiprocessing_exec},
267+
{0, NULL}
268+
};
261269

262-
return module;
270+
static struct PyModuleDef multiprocessing_module = {
271+
PyModuleDef_HEAD_INIT,
272+
.m_name = "_multiprocessing",
273+
.m_methods = module_methods,
274+
.m_slots = multiprocessing_slots,
275+
};
276+
277+
PyMODINIT_FUNC
278+
PyInit__multiprocessing(void)
279+
{
280+
return PyModuleDef_Init(&multiprocessing_module);
263281
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy