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 math module to multiphase initialization #19243

Merged
merged 2 commits into from
Mar 31, 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 @@
Port :mod:`math` to multiphase initialization (:pep:`489`).
58 changes: 33 additions & 25 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,29 @@ math_ulp_impl(PyObject *module, double x)
return x2 - x;
}

static int
math_exec(PyObject *module)
{
if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
return -1;
}
// 2pi
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
return -1;
}
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
return -1;
}
#endif
return 0;
}

static PyMethodDef math_methods[] = {
{"acos", math_acos, METH_O, math_acos_doc},
Expand Down Expand Up @@ -3479,41 +3502,26 @@ static PyMethodDef math_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyModuleDef_Slot math_slots[] = {
{Py_mod_exec, math_exec},
{0, NULL}
};

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


static struct PyModuleDef mathmodule = {
PyModuleDef_HEAD_INIT,
"math",
module_doc,
-1,
math_methods,
NULL,
NULL,
NULL,
NULL
.m_name = "math",
.m_doc = module_doc,
.m_size = 0,
.m_methods = math_methods,
.m_slots = math_slots,
};

PyMODINIT_FUNC
PyInit_math(void)
{
PyObject *m;

m = PyModule_Create(&mathmodule);
if (m == NULL)
goto finally;

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()));
#if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN)
PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan()));
#endif

finally:
return m;
return PyModuleDef_Init(&mathmodule);
}