]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-94382: port multiprocessing static types to heap types (#94336)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Wed, 20 Jul 2022 19:26:01 +0000 (00:56 +0530)
committerGitHub <noreply@github.com>
Wed, 20 Jul 2022 19:26:01 +0000 (21:26 +0200)
Misc/NEWS.d/next/Library/2022-07-03-16-41-03.gh-issue-94382.zuVZeM.rst [new file with mode: 0644]
Modules/_multiprocessing/multiprocessing.c
Modules/_multiprocessing/multiprocessing.h
Modules/_multiprocessing/semaphore.c

diff --git a/Misc/NEWS.d/next/Library/2022-07-03-16-41-03.gh-issue-94382.zuVZeM.rst b/Misc/NEWS.d/next/Library/2022-07-03-16-41-03.gh-issue-94382.zuVZeM.rst
new file mode 100644 (file)
index 0000000..d793007
--- /dev/null
@@ -0,0 +1 @@
+Port static types of ``_multiprocessing`` module to heap types. Patch by Kumar Aditya.
index ed89a1e29a433fd8644ca6e221455eaf798dd399..2463e1e1a8bf7efd371ab19d40f1c6ac40e9ef08 100644 (file)
@@ -196,33 +196,39 @@ multiprocessing_exec(PyObject *module)
 {
 #ifdef HAVE_MP_SEMAPHORE
 
-    /* Add _PyMp_SemLock type to module */
-    if (PyModule_AddType(module, &_PyMp_SemLockType) < 0) {
+    PyTypeObject *semlock_type = (PyTypeObject *)PyType_FromModuleAndSpec(
+                module, &_PyMp_SemLockType_spec, NULL);
+
+    if (semlock_type == NULL) {
+        return -1;
+    }
+    int rc = PyModule_AddType(module, semlock_type);
+    Py_DECREF(semlock_type);
+    if (rc < 0) {
         return -1;
     }
 
-    {
-        PyObject *py_sem_value_max;
-        /* Some systems define SEM_VALUE_MAX as an unsigned value that
-         * causes it to be negative when used as an int (NetBSD).
-         *
-         * Issue #28152: Use (0) instead of 0 to fix a warning on dead code
-         * when using clang -Wunreachable-code. */
-        if ((int)(SEM_VALUE_MAX) < (0))
-            py_sem_value_max = PyLong_FromLong(INT_MAX);
-        else
-            py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
-
-        if (py_sem_value_max == NULL) {
-            return -1;
-        }
-        if (PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
-                             py_sem_value_max) < 0) {
-            Py_DECREF(py_sem_value_max);
-            return -1;
-        }
+    PyObject *py_sem_value_max;
+    /* Some systems define SEM_VALUE_MAX as an unsigned value that
+     * causes it to be negative when used as an int (NetBSD).
+     *
+     * Issue #28152: Use (0) instead of 0 to fix a warning on dead code
+     * when using clang -Wunreachable-code. */
+    if ((int)(SEM_VALUE_MAX) < (0)) {
+        py_sem_value_max = PyLong_FromLong(INT_MAX);
+    }
+    else {
+        py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
+    }
+    if (py_sem_value_max == NULL) {
+        return -1;
+    }
+    if (PyDict_SetItemString(semlock_type->tp_dict, "SEM_VALUE_MAX",
+                         py_sem_value_max) < 0) {
         Py_DECREF(py_sem_value_max);
+        return -1;
     }
+    Py_DECREF(py_sem_value_max);
 
 #endif
 
@@ -276,6 +282,7 @@ static PyModuleDef_Slot multiprocessing_slots[] = {
 static struct PyModuleDef multiprocessing_module = {
     PyModuleDef_HEAD_INIT,
     .m_name = "_multiprocessing",
+    .m_size = 0,
     .m_methods = module_methods,
     .m_slots = multiprocessing_slots,
 };
index 3a8314b1db8331d0adbfbb5d163af495dec307a0..b595e5a8dd18de4522d036d6868d999194e02e37 100644 (file)
@@ -89,7 +89,7 @@ PyObject *_PyMp_SetError(PyObject *Type, int num);
  * Externs - not all will really exist on all platforms
  */
 
-extern PyTypeObject _PyMp_SemLockType;
+extern PyType_Spec _PyMp_SemLockType_spec;
 extern PyObject *_PyMp_sem_unlink(const char *name);
 
 #endif /* MULTIPROCESSING_H */
index f5fd3257f066a37675e4daa8ab7d2769110dde16..58fb0eb96aeeed7c0f6851b850e3064a83af9e07 100644 (file)
@@ -568,10 +568,13 @@ _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
 static void
 semlock_dealloc(SemLockObject* self)
 {
+    PyTypeObject *tp = Py_TYPE(self);
+    PyObject_GC_UnTrack(self);
     if (self->handle != SEM_FAILED)
         SEM_CLOSE(self->handle);
     PyMem_Free(self->name);
-    Py_TYPE(self)->tp_free((PyObject*)self);
+    tp->tp_free(self);
+    Py_DECREF(tp);
 }
 
 /*[clinic input]
@@ -701,6 +704,13 @@ _multiprocessing_SemLock___exit___impl(SemLockObject *self,
     return _multiprocessing_SemLock_release_impl(self);
 }
 
+static int
+semlock_traverse(SemLockObject *s, visitproc visit, void *arg)
+{
+    Py_VISIT(Py_TYPE(s));
+    return 0;
+}
+
 /*
  * Semaphore methods
  */
@@ -739,45 +749,26 @@ static PyMemberDef semlock_members[] = {
  * Semaphore type
  */
 
-PyTypeObject _PyMp_SemLockType = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    /* tp_name           */ "_multiprocessing.SemLock",
-    /* tp_basicsize      */ sizeof(SemLockObject),
-    /* tp_itemsize       */ 0,
-    /* tp_dealloc        */ (destructor)semlock_dealloc,
-    /* tp_vectorcall_offset */ 0,
-    /* tp_getattr        */ 0,
-    /* tp_setattr        */ 0,
-    /* tp_as_async       */ 0,
-    /* tp_repr           */ 0,
-    /* tp_as_number      */ 0,
-    /* tp_as_sequence    */ 0,
-    /* tp_as_mapping     */ 0,
-    /* tp_hash           */ 0,
-    /* tp_call           */ 0,
-    /* tp_str            */ 0,
-    /* tp_getattro       */ 0,
-    /* tp_setattro       */ 0,
-    /* tp_as_buffer      */ 0,
-    /* tp_flags          */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
-    /* tp_doc            */ "Semaphore/Mutex type",
-    /* tp_traverse       */ 0,
-    /* tp_clear          */ 0,
-    /* tp_richcompare    */ 0,
-    /* tp_weaklistoffset */ 0,
-    /* tp_iter           */ 0,
-    /* tp_iternext       */ 0,
-    /* tp_methods        */ semlock_methods,
-    /* tp_members        */ semlock_members,
-    /* tp_getset         */ 0,
-    /* tp_base           */ 0,
-    /* tp_dict           */ 0,
-    /* tp_descr_get      */ 0,
-    /* tp_descr_set      */ 0,
-    /* tp_dictoffset     */ 0,
-    /* tp_init           */ 0,
-    /* tp_alloc          */ 0,
-    /* tp_new            */ _multiprocessing_SemLock,
+static PyType_Slot _PyMp_SemLockType_slots[] = {
+    {Py_tp_dealloc, semlock_dealloc},
+    {Py_tp_getattro, PyObject_GenericGetAttr},
+    {Py_tp_setattro, PyObject_GenericSetAttr},
+    {Py_tp_methods, semlock_methods},
+    {Py_tp_members, semlock_members},
+    {Py_tp_alloc, PyType_GenericAlloc},
+    {Py_tp_new, _multiprocessing_SemLock},
+    {Py_tp_traverse, semlock_traverse},
+    {Py_tp_free, PyObject_GC_Del},
+    {Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")},
+    {0, 0},
+};
+
+PyType_Spec _PyMp_SemLockType_spec = {
+    .name = "_multiprocessing.SemLock",
+    .basicsize = sizeof(SemLockObject),
+    .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+              Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
+    .slots = _PyMp_SemLockType_slots,
 };
 
 /*