8000 GH-94382: port multiprocessing static types to heap types (#94336) · python/cpython@000a4ee · GitHub
[go: up one dir, main page]

Skip to content
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

Appearance settings

Commit 000a4ee

Browse files
GH-94382: port multiprocessing static types to heap types (#94336)
1 parent 8ede67c commit 000a4ee

File tree

4 files changed

+62
-63
lines changed

4 files changed

+62
-63
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port static types of ``_multiprocessing`` module to heap types. Patch by Kumar Aditya.

Modules/_multiprocessing/multiprocessing.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,39 @@ multiprocessing_exec(PyObject *module)
196196
{
197197
#ifdef HAVE_MP_SEMAPHORE
198198

199-
/* Add _PyMp_SemLock type to module */
200-
if (PyModule_AddType(module, &_PyMp_SemLockType) < 0) {
199+
PyTypeObject *semlock_type = (PyTypeObject *)PyType_FromModuleAndSpec(
200+
module, &_PyMp_SemLockType_spec, NULL);
201+
202+
if (semlock_type == NULL) {
203+
return -1;
204+
}
205+
int rc = PyModule_AddType(module, semlock_type);
206+
Py_DECREF(semlock_type);
207+
if (rc < 0) {
201208
return -1;
202209
}
203210

204-
{
205-
PyObject *py_sem_value_max;
206-
/* Some systems define SEM_VALUE_MAX as an unsigned value that
207-
* causes it to be negative when used as an int (NetBSD).
208-
*
209-
* Issue #28152: Use (0) instead of 0 to fix a warning on dead code
210-
* when using clang -Wunreachable-code. */
211-
if ((int)(SEM_VALUE_MAX) < (0))
212-
py_sem_value_max = PyLong_FromLong(INT_MAX);
213-
else
214-
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
215-
216-
if (py_sem_value_max == NULL) {
217-
return -1;
218-
}
219-
if (PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
220-
py_sem_value_max) < 0) {
221-
Py_DECREF(py_sem_value_max);
222-
return -1;
223-
}
211+
PyObject *py_sem_value_max;
212+
/* Some systems define SEM_VALUE_MAX as an unsigned value that
213+
* causes it to be negative when used as an int (NetBSD).
214+
*
215+
* Issue #28152: Use (0) instead of 0 to fix a warning on dead code
216+
* when using clang -Wunreachable-code. */
217+
if ((int)(SEM_VALUE_MAX) < (0)) {
218+
py_sem_value_max = PyLong_FromLong(INT_MAX);
219+
}
220+
else {
221+
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
222+
}
223+
if (py_sem_value_max == NULL) {
224+
return -1;
225+
}
226+
if (PyDict_SetItemString(semlock_type->tp_dict, "SEM_VALUE_MAX",
227+
py_sem_value_max) < 0) {
224228
Py_DECREF(py_sem_value_max);
229+
return -1;
225230
}
231+
Py_DECREF(py_sem_value_max);
226232

227233
#endif
228234

@@ -276,6 +282,7 @@ static PyModuleDef_Slot multiprocessing_slots[] = {
276282
static struct PyModuleDef multiprocessing_module = {
277283
PyModuleDef_HEAD_INIT,
278284
.m_name = "_multiprocessing",
285+
.m_size = 0,
279286
.m_methods = module_methods,
280287
.m_slots = multiprocessing_slots,
281288
};

Modules/_multiprocessing/multiprocessing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ PyObject *_PyMp_SetError(PyObject *Type, int num);
8989
* Externs - not all will really exist on all platforms
9090
*/
9191

92-
extern PyTypeObject _PyMp_SemLockType;
92+
extern PyType_Spec _PyMp_SemLockType_spec;
9393
extern PyObject *_PyMp_sem_unlink(const char *name);
9494

9595
#endif /* MULTIPROCESSING_H */

Modules/_multiprocessing/semaphore.c

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,13 @@ _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
568568
static void
569569
semlock_dealloc(SemLockObject* self)
570570
{
571+
PyTypeObject *tp = Py_TYPE(self);
572+
PyObject_GC_UnTrack(self);
571573
if (self->handle != SEM_FAILED)
572574
SEM_CLOSE(self->handle);
573575
PyMem_Free(self->name);
574-
Py_TYPE(self)->tp_free((PyObject*)self);
576+
tp->tp_free(self);
577+
Py_DECREF(tp);
575578
}
576579

577580
/*[clinic input]
@@ -701,6 +704,13 @@ _multiprocessing_SemLock___exit___impl(SemLockObject *self,
701704
return _multiprocessing_SemLock_release_impl(self);
702705
}
703706

707+
static int
708+
semlock_traverse(SemLockObject *s, visitproc visit, void *arg)
709+
{
710+
Py_VISIT(Py_TYPE(s));
711+
return 0;
712+
}
713+
704714
/*
705715
* Semaphore methods
706716
*/
@@ -739,45 +749,26 @@ static PyMemberDef semlock_members[] = {
739749
* Semaphore type
740750
*/
741751

742-
PyTypeObject _PyMp_SemLockType = {
743-
PyVarObject_HEAD_INIT(NULL, 0)
744-
/* tp_name */ "_multiprocessing.SemLock",
745-
/* tp_basicsize */ sizeof(SemLockObject),
746-
/* tp_itemsize */ 0,
747-
/* tp_dealloc */ (destructor)semlock_dealloc,
748-
/* tp_vectorcall_offset */ 0,
749-
/* tp_getattr */ 0,
750-
/* tp_setattr */ 0,
751-
/* tp_as_async */ 0,
752-
/* tp_repr */ 0,
753-
/* tp_as_number */ 0,
754-
/* tp_as_sequence */ 0,
755-
/* tp_as_mapping */ 0,
756-
/* tp_hash */ 0,
757-
/* tp_call */ 0,
758-
/* tp_str */ 0,
759-
/* tp_getattro */ 0,
760-
/* tp_setattro */ 0,
761-
/* tp_as_buffer */ 0,
762-
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
763-
/* tp_doc */ "Semaphore/Mutex type",
764-
/* tp_traverse */ 0,
765-
/* tp_clear */ 0,
766-
/* tp_richcompare */ 0,
767-
/* tp_weaklistoffset */ 0,
768-
/* tp_iter */ 0,
769-
/* tp_iternext */ 0,
770-
/* tp_methods */ semlock_methods,
771-
/* tp_members */ semlock_members,
772-
/* tp_getset */ 0,
773-
/* tp_base */ 0,
774-
/* tp_dict */ 0,
775-
/* tp_descr_get */ 0,
776-
/* tp_descr_set */ 0,
777-
/* tp_dictoffset */ 0,
778-
/* tp_init */ 0,
779-
/* tp_alloc */ 0,
780-
/* tp_new */ _multiprocessing_SemLock,
752+
static PyType_Slot _PyMp_SemLockType_slots[] = {
753+
{Py_tp_dealloc, semlock_dealloc},
754+
{Py_tp_getattro, PyObject_GenericGetAttr},
755+
{Py_tp_setattro, PyObject_GenericSetAttr},
756+
{Py_tp_methods, semlock_methods},
757+
{Py_tp_members, semlock_members},
758+
{Py_tp_alloc, PyType_GenericAlloc},
759+
{Py_tp_new, _multiprocessing_SemLock},
760+
{Py_tp_traverse, semlock_traverse},
761+
{Py_tp_free, PyObject_GC_Del},
762+
{Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")},
763+
{0, 0},
764+
};
765+
766+
PyType_Spec _PyMp_SemLockType_spec = {
767+
.name = "_multiprocessing.SemLock",
768+
.basicsize = sizeof(SemLockObject),
769+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
770+
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
771+
.slots = _PyMp_SemLockType_slots,
781772
};
782773

783774
/*

0 commit comments

Comments
 (0)
0