From f76d894dc5d5facce1a6c1b71637f6a2b3f9fd2b Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 10 Sep 2020 09:09:04 -0500 Subject: [PATCH] bpo-1635741: Port cmath to multi-phase init (PEP 489) (GH-22165) --- ...2020-09-08-21-58-47.bpo-1635741.vdjSLH.rst | 2 + Modules/cmathmodule.c | 76 ++++++++++++------- 2 files changed, 49 insertions(+), 29 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-09-08-21-58-47.bpo-1635741.vdjSLH.rst 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 00000000000..bc1a6c888e3 --- /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 5eac4b4940b..0f22049a170 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; + 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; + } - 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, "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