Skip to content

Commit f76d894

Browse files
authored
bpo-1635741: Port cmath to multi-phase init (PEP 489) (GH-22165)
1 parent 1e874d5 commit f76d894

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Port the :mod:`cmath` extension module to multi-phase initialization
2+
(:pep:`489`).

Modules/cmathmodule.c

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,37 +1254,35 @@ static PyMethodDef cmath_methods[] = {
12541254
{NULL, NULL} /* sentinel */
12551255
};
12561256

1257-
1258-
static struct PyModuleDef cmathmodule = {
1259-
PyModuleDef_HEAD_INIT,
1260-
"cmath",
1261-
module_doc,
1262-
-1,
1263-
cmath_methods,
1264-
NULL,
1265-
NULL,
1266-
NULL,
1267-
NULL
1268-
};
1269-
1270-
PyMODINIT_FUNC
1271-
PyInit_cmath(void)
1257+
static int
1258+
cmath_exec(PyObject *mod)
12721259
{
1273-
PyObject *m;
1274-
1275-
m = PyModule_Create(&cmathmodule);
1276-
if (m == NULL)
1277-
return NULL;
1278-
1279-
PyModule_AddObject(m, "pi",
1280-
PyFloat_FromDouble(Py_MATH_PI));
1281-
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
1282-
PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
1283-
PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
1284-
PyModule_AddObject(m, "infj", PyComplex_FromCComplex(c_infj()));
1260+
if (PyModule_AddObject(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
1261+
return -1;
1262+
}
1263+
if (PyModule_AddObject(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
1264+
return -1;
1265+
}
1266+
// 2pi
1267+
if (PyModule_AddObject(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
1268+
return -1;
1269+
}
1270+
if (PyModule_AddObject(mod, "inf", PyFloat_FromDouble(m_inf())) < 0) {
1271+
return -1;
1272+
}
1273+
1274+
if (PyModule_AddObject(mod, "infj",
1275+
PyComplex_FromCComplex(c_infj())) < 0) {
1276+
return -1;
1277+
}
12851278
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
1286-
PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
1287-
PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj()));
1279+
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(m_nan())) < 0) {
1280+
return -1;
1281+
}
1282+
if (PyModule_AddObject(mod, "nanj",
1283+
PyComplex_FromCComplex(c_nanj())) < 0) {
1284+
return -1;
1285+
}
12881286
#endif
12891287

12901288
/* initialize special value tables */
@@ -1401,5 +1399,25 @@ PyInit_cmath(void)
14011399
C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
14021400
C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
14031401
})
1404-
return m;
1402+
return 0;
14051403
}
1404+
1405+
static PyModuleDef_Slot cmath_slots[] = {
1406+
{Py_mod_exec, cmath_exec},
1407+
{0, NULL}
1408+
};
1409+
1410+
static struct PyModuleDef cmathmodule = {
1411+
PyModuleDef_HEAD_INIT,
1412+
.m_name = "cmath",
1413+
.m_doc = module_doc,
1414+
.m_size = 0,
1415+
.m_methods = cmath_methods,
1416+
.m_slots = cmath_slots
1417+
};
1418+
1419+
PyMODINIT_FUNC
1420+
PyInit_cmath(void)
1421+
{
1422+
return PyModuleDef_Init(&cmathmodule);
1423+
}

0 commit comments

Comments
 (0)