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

This commit is contained in:
Mohamed Koubaa 2020-09-10 09:09:04 -05:00 committed by GitHub
parent 1e874d5bec
commit f76d894dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 29 deletions

View File

@ -0,0 +1,2 @@
Port the :mod:`cmath` extension module to multi-phase initialization
(:pep:`489`).

View File

@ -1254,37 +1254,35 @@ static PyMethodDef cmath_methods[] = {
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static int
static struct PyModuleDef cmathmodule = { cmath_exec(PyObject *mod)
PyModuleDef_HEAD_INIT,
"cmath",
module_doc,
-1,
cmath_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit_cmath(void)
{ {
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 (PyModule_AddObject(mod, "infj",
if (m == NULL) PyComplex_FromCComplex(c_infj())) < 0) {
return NULL; return -1;
}
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 !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN) #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan())); if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(m_nan())) < 0) {
PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj())); return -1;
}
if (PyModule_AddObject(mod, "nanj",
PyComplex_FromCComplex(c_nanj())) < 0) {
return -1;
}
#endif #endif
/* initialize special value tables */ /* 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(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) 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);
} }