From cd29a883b1a63683082088de7360f083be8dac8e Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Fri, 3 Jul 2020 23:10:05 +0900 Subject: [PATCH 1/2] bpo-1635741: Port faulthandler module to multiphase initialization --- ...2020-07-03-23-10-02.bpo-1635741.F5coWe.rst | 1 + Modules/faulthandler.c | 67 +++++++++---------- 2 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-07-03-23-10-02.bpo-1635741.F5coWe.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-07-03-23-10-02.bpo-1635741.F5coWe.rst b/Misc/NEWS.d/next/Core and Builtins/2020-07-03-23-10-02.bpo-1635741.F5coWe.rst new file mode 100644 index 00000000000000..927c8e5b7083fa --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-07-03-23-10-02.bpo-1635741.F5coWe.rst @@ -0,0 +1 @@ +Port :mod:`faulthandler` to multiphase initialization. diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index e7a285033051df..e7e4666a1c6016 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -1291,59 +1291,56 @@ static PyMethodDef module_methods[] = { {NULL, NULL} /* sentinel */ }; -static struct PyModuleDef module_def = { - PyModuleDef_HEAD_INIT, - "faulthandler", - module_doc, - 0, /* non-negative size to be able to unload the module */ - module_methods, - NULL, - faulthandler_traverse, - NULL, - NULL -}; - -PyMODINIT_FUNC -PyInit_faulthandler(void) -{ - PyObject *m = PyModule_Create(&module_def); - if (m == NULL) - return NULL; - +static int +PyExec_faulthandler(PyObject *module) { /* Add constants for unit tests */ #ifdef MS_WINDOWS /* RaiseException() codes (prefixed by an underscore) */ - if (PyModule_AddIntConstant(m, "_EXCEPTION_ACCESS_VIOLATION", + if (PyModule_AddIntConstant(module, "_EXCEPTION_ACCESS_VIOLATION", EXCEPTION_ACCESS_VIOLATION)) { - goto error; + return -1; } - if (PyModule_AddIntConstant(m, "_EXCEPTION_INT_DIVIDE_BY_ZERO", + if (PyModule_AddIntConstant(module, "_EXCEPTION_INT_DIVIDE_BY_ZERO", EXCEPTION_INT_DIVIDE_BY_ZERO)) { - goto error; + return -1; } - if (PyModule_AddIntConstant(m, "_EXCEPTION_STACK_OVERFLOW", + if (PyModule_AddIntConstant(module, "_EXCEPTION_STACK_OVERFLOW", EXCEPTION_STACK_OVERFLOW)) { - goto error; + return -1; } /* RaiseException() flags (prefixed by an underscore) */ - if (PyModule_AddIntConstant(m, "_EXCEPTION_NONCONTINUABLE", + if (PyModule_AddIntConstant(module, "_EXCEPTION_NONCONTINUABLE", EXCEPTION_NONCONTINUABLE)) { - goto error; + return -1; } - if (PyModule_AddIntConstant(m, "_EXCEPTION_NONCONTINUABLE_EXCEPTION", + if (PyModule_AddIntConstant(module, "_EXCEPTION_NONCONTINUABLE_EXCEPTION", EXCEPTION_NONCONTINUABLE_EXCEPTION)) { - goto error; + return -1; } #endif + return 0; +} - return m; +static PyType_Slot faulthandler_slots[] = { + {Py_mod_exec, PyExec_faulthandler}, + {0, NULL} +}; -#ifdef MS_WINDOWS -error: - Py_DECREF(m); - return NULL; -#endif +static struct PyModuleDef module_def = { + PyModuleDef_HEAD_INIT, + .m_name = "faulthandler", + .m_doc = module_doc, + .m_size = 0, /* non-negative size to be able to unload the module */ + .m_methods = module_methods, + .m_traverse = faulthandler_traverse, + .m_slots = faulthandler_slots +}; + +PyMODINIT_FUNC +PyInit_faulthandler(void) +{ + return PyModuleDef_Init(&module_def); } static int From fa8b4de588af61bc7b68a27b6d0624a651447d51 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Fri, 3 Jul 2020 23:40:34 +0900 Subject: [PATCH 2/2] bpo-1635741: fix --- Modules/faulthandler.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index e7e4666a1c6016..67fe1ca9ffffd2 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -1322,7 +1322,7 @@ PyExec_faulthandler(PyObject *module) { return 0; } -static PyType_Slot faulthandler_slots[] = { +static PyModuleDef_Slot faulthandler_slots[] = { {Py_mod_exec, PyExec_faulthandler}, {0, NULL} }; @@ -1331,7 +1331,6 @@ static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, .m_name = "faulthandler", .m_doc = module_doc, - .m_size = 0, /* non-negative size to be able to unload the module */ .m_methods = module_methods, .m_traverse = faulthandler_traverse, .m_slots = faulthandler_slots