[Python-checkins] bpo-1635741: Port operator module to multiphase initialization (PEP 489) (GH-19150)

Paulo Henrique Silva webhook-mailer at python.org
Tue Mar 24 22:18:55 EDT 2020


https://github.com/python/cpython/commit/f3d5ac47720045a72f7ef5af13046d9531e6007b
commit: f3d5ac47720045a72f7ef5af13046d9531e6007b
branch: master
author: Paulo Henrique Silva <ph.silva at carta.com>
committer: GitHub <noreply at github.com>
date: 2020-03-25T03:18:47+01:00
summary:

bpo-1635741: Port operator module to multiphase initialization (PEP 489) (GH-19150)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-03-24-22-17-12.bpo-1635741.jWaMRV.rst
M Modules/_operator.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-24-22-17-12.bpo-1635741.jWaMRV.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-24-22-17-12.bpo-1635741.jWaMRV.rst
new file mode 100644
index 0000000000000..d84626af5b131
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-24-22-17-12.bpo-1635741.jWaMRV.rst	
@@ -0,0 +1,2 @@
+Port operator module to multiphase initialization (PEP 489). Patch by Paulo
+Henrique Silva.
diff --git a/Modules/_operator.c b/Modules/_operator.c
index 9c54aac0583f4..007c21b1bda37 100644
--- a/Modules/_operator.c
+++ b/Modules/_operator.c
@@ -1746,16 +1746,38 @@ static PyTypeObject methodcaller_type = {
 };
 
 
-/* Initialization function for the module (*must* be called PyInit__operator) */
+static int
+operator_exec(PyObject *module)
+{
+    PyTypeObject *types[] = {
+        &itemgetter_type,
+        &attrgetter_type,
+        &methodcaller_type
+    };
+
+    for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
+        if (PyModule_AddType(module, types[i]) < 0) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+
+static struct PyModuleDef_Slot operator_slots[] = {
+    {Py_mod_exec, operator_exec},
+    {0, NULL}
+};
 
 
 static struct PyModuleDef operatormodule = {
     PyModuleDef_HEAD_INIT,
     "_operator",
     operator_doc,
-    -1,
+    0,
     operator_methods,
-    NULL,
+    operator_slots,
     NULL,
     NULL,
     NULL
@@ -1764,24 +1786,5 @@ static struct PyModuleDef operatormodule = {
 PyMODINIT_FUNC
 PyInit__operator(void)
 {
-    PyObject *m;
-
-    /* Create the module and add the functions */
-    m = PyModule_Create(&operatormodule);
-    if (m == NULL)
-        return NULL;
-
-    PyTypeObject *types[] = {
-        &itemgetter_type,
-        &attrgetter_type,
-        &methodcaller_type
-    };
-
-    for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
-        if (PyModule_AddType(m, types[i]) < 0) {
-            return NULL;
-        }
-    }
-
-    return m;
+    return PyModuleDef_Init(&operatormodule);
 }



More information about the Python-checkins mailing list