Skip to content

Commit 5be8241

Browse files
authored
bpo-1635741: Port math module to multiphase initialization (GH-19243)
1 parent 1cb763b commit 5be8241

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port :mod:`math` to multiphase initialization (:pep:`489`).

Modules/mathmodule.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,6 +3421,29 @@ math_ulp_impl(PyObject *module, double x)
34213421
return x2 - x;
34223422
}
34233423

3424+
static int
3425+
math_exec(PyObject *module)
3426+
{
3427+
if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
3428+
return -1;
3429+
}
3430+
if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
3431+
return -1;
3432+
}
3433+
// 2pi
3434+
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
3435+
return -1;
3436+
}
3437+
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
3438+
return -1;
3439+
}
3440+
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
3441+
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
3442+
return -1;
3443+
}
3444+
#endif
3445+
return 0;
3446+
}
34243447

34253448
static PyMethodDef math_methods[] = {
34263449
{"acos", math_acos, METH_O, math_acos_doc},
@@ -3479,41 +3502,26 @@ static PyMethodDef math_methods[] = {
34793502
{NULL, NULL} /* sentinel */
34803503
};
34813504

3505+
static PyModuleDef_Slot math_slots[] = {
3506+
{Py_mod_exec, math_exec},
3507+
{0, NULL}
3508+
};
34823509

34833510
PyDoc_STRVAR(module_doc,
34843511
"This module provides access to the mathematical functions\n"
34853512
"defined by the C standard.");
34863513

3487-
34883514
static struct PyModuleDef mathmodule = {
34893515
PyModuleDef_HEAD_INIT,
3490-
"math",
3491-
module_doc,
3492-
-1,
3493-
math_methods,
3494-
NULL,
3495-
NULL,
3496-
NULL,
3497-
NULL
3516+
.m_name = "math",
3517+
.m_doc = module_doc,
3518+
.m_size = 0,
3519+
.m_methods = math_methods,
3520+
.m_slots = math_slots,
34983521
};
34993522

35003523
PyMODINIT_FUNC
35013524
PyInit_math(void)
35023525
{
3503-
PyObject *m;
3504-
3505-
m = PyModule_Create(&mathmodule);
3506-
if (m == NULL)
3507-
goto finally;
3508-
3509-
PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
3510-
PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
3511-
PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */
3512-
PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf()));
3513-
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
3514-
PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
3515-
#endif
3516-
3517-
finally:
3518-
return m;
3526+
return PyModuleDef_Init(&mathmodule);
35193527
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy