Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-1635741 port cmath to multi-phase init (pep 489) #22165

Merged
merged 3 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port the :mod:`cmath` extension module to multi-phase initialization
(:pep:`489`).
78 changes: 48 additions & 30 deletions Modules/cmathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}