[Python-checkins] bpo-1635741: Port cmath to multi-phase init (PEP 489) (GH-22165)

Mohamed Koubaa webhook-mailer at python.org
Tue Sep 15 02:20:25 EDT 2020


https://github.com/python/cpython/commit/f76d894dc5d5facce1a6c1b71637f6a2b3f9fd2b
commit: f76d894dc5d5facce1a6c1b71637f6a2b3f9fd2b
branch: master
author: Mohamed Koubaa <koubaa.m at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-09-10T16:09:04+02:00
summary:

bpo-1635741: Port cmath to multi-phase init (PEP 489) (GH-22165)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-09-08-21-58-47.bpo-1635741.vdjSLH.rst
M Modules/cmathmodule.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-09-08-21-58-47.bpo-1635741.vdjSLH.rst b/Misc/NEWS.d/next/Core and Builtins/2020-09-08-21-58-47.bpo-1635741.vdjSLH.rst
new file mode 100644
index 0000000000000..bc1a6c888e33b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-09-08-21-58-47.bpo-1635741.vdjSLH.rst	
@@ -0,0 +1,2 @@
+Port the :mod:`cmath` extension module to multi-phase initialization
+(:pep:`489`).
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 5eac4b4940bea..0f22049a17084 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -1254,37 +1254,35 @@ static PyMethodDef cmath_methods[] = {
     {NULL, NULL}  /* sentinel */
 };
 
-
-static struct PyModuleDef cmathmodule = {
-    PyModuleDef_HEAD_INIT,
-    "cmath",
-    module_doc,
-    -1,
-    cmath_methods,
-    NULL,
-    NULL,
-    NULL,
-    NULL
-};
-
-PyMODINIT_FUNC
-PyInit_cmath(void)
+static int
+cmath_exec(PyObject *mod)
 {
-    PyObject *m;
-
-    m = PyModule_Create(&cmathmodule);
-    if (m == NULL)
-        return NULL;
-
-    PyModule_AddObject(m, "pi",
-                       PyFloat_FromDouble(Py_MATH_PI));
-    PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
-    PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
-    PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
-    PyModule_AddObject(m, "infj", PyComplex_FromCComplex(c_infj()));
+    if (PyModule_AddObject(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
+        return -1;
+    }
+    if (PyModule_AddObject(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
+        return -1;
+    }
+    // 2pi
+    if (PyModule_AddObject(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
+        return -1;
+    }
+    if (PyModule_AddObject(mod, "inf", PyFloat_FromDouble(m_inf())) < 0) {
+        return -1;
+    }
+
+    if (PyModule_AddObject(mod, "infj",
+                           PyComplex_FromCComplex(c_infj())) < 0) {
+        return -1;
+    }
 #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
-    PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
-    PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj()));
+    if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(m_nan())) < 0) {
+        return -1;
+    }
+    if (PyModule_AddObject(mod, "nanj",
+                           PyComplex_FromCComplex(c_nanj())) < 0) {
+        return -1;
+    }
 #endif
 
     /* initialize special value tables */
@@ -1401,5 +1399,25 @@ PyInit_cmath(void)
       C(INF,N) C(U,U) C(INF,-0.) C(INF,0.)   C(U,U) C(INF,N) C(INF,N)
       C(N,N)   C(N,N) C(N,0.)    C(N,0.)     C(N,N) C(N,N)   C(N,N)
     })
-    return m;
+    return 0;
 }
+
+static PyModuleDef_Slot cmath_slots[] = {
+    {Py_mod_exec, cmath_exec},
+    {0, NULL}
+};
+
+static struct PyModuleDef cmathmodule = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "cmath",
+    .m_doc = module_doc,
+    .m_size = 0,
+    .m_methods = cmath_methods,
+    .m_slots = cmath_slots
+};
+
+PyMODINIT_FUNC
+PyInit_cmath(void)
+{
+    return PyModuleDef_Init(&cmathmodule);
+}
\ No newline at end of file



More information about the Python-checkins mailing list