From 942bb7677a5fead5e461baed852949af55382276 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 31 Mar 2020 22:15:32 +0900 Subject: [PATCH 1/2] bpo-1635741: Port math module to multiphase initialization --- ...2020-03-31-22-15-04.bpo-1635741.8Ir1a0.rst | 1 + Modules/mathmodule.c | 57 +++++++++++-------- 2 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-31-22-15-04.bpo-1635741.8Ir1a0.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-31-22-15-04.bpo-1635741.8Ir1a0.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-31-22-15-04.bpo-1635741.8Ir1a0.rst new file mode 100644 index 00000000000000..e1c5a29916b1cf --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-31-22-15-04.bpo-1635741.8Ir1a0.rst @@ -0,0 +1 @@ +Port :mod:`math` to multiphase initialization (:pep:`489`). diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2a73a983f56d2d..a75b0039a1f00b 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -3421,6 +3421,28 @@ 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}, @@ -3479,41 +3501,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); } From 0789fd022efad55d7ada2aad204303166b13c971 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 31 Mar 2020 15:23:00 +0200 Subject: [PATCH 2/2] Update Modules/mathmodule.c --- Modules/mathmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index a75b0039a1f00b..f1d59c09e6272c 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -3422,7 +3422,8 @@ math_ulp_impl(PyObject *module, double x) } static int -math_exec(PyObject *module) { +math_exec(PyObject *module) +{ if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) { return -1; }