bpo-1635741: Port _warnings to the multi-phase init (GH-23379) · python/cpython@6f4635f
@@ -24,9 +24,6 @@ _Py_IDENTIFIER(ignore);
24242525typedef struct _warnings_runtime_state WarningsState;
262627-/* Forward declaration of the _warnings module definition. */
28-static struct PyModuleDef warningsmodule;
29-3027_Py_IDENTIFIER(__name__);
31283229/* Given a module object, get its per-module state. */
@@ -1353,52 +1350,45 @@ static PyMethodDef warnings_functions[] = {
13531350};
13541351135513521356-static struct PyModuleDef warningsmodule = {
1357-PyModuleDef_HEAD_INIT,
1358-MODULE_NAME, /* m_name */
1359-warnings__doc__, /* m_doc */
1360-0, /* m_size */
1361-warnings_functions, /* m_methods */
1362-NULL, /* m_reload */
1363-NULL, /* m_traverse */
1364-NULL, /* m_clear */
1365-NULL /* m_free */
1366-};
1367-1368-1369-PyMODINIT_FUNC
1370-_PyWarnings_Init(void)
1353+static int
1354+warnings_module_exec(PyObject *module)
13711355{
1372-PyObject *m;
1373-1374-m = PyModule_Create(&warningsmodule);
1375-if (m == NULL) {
1376-return NULL;
1377- }
1378-13791356WarningsState *st = warnings_get_state();
13801357if (st == NULL) {
1381-goto error;
1358+return -1;
13821359 }
1383-1384-if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) {
1385- goto error;
1360+if (PyModule_AddObjectRef(module, "filters", st->filters) < 0) {
1361+return -1;
13861362 }
1387-if (PyModule_AddObjectRef(m, "_onceregistry", st->once_registry) < 0) {
1388-goto error;
1363+if (PyModule_AddObjectRef(module, "_onceregistry", st->once_registry) < 0) {
1364+return -1;
13891365 }
1390-if (PyModule_AddObjectRef(m, "_defaultaction", st->default_action) < 0) {
1391-goto error;
1366+if (PyModule_AddObjectRef(module, "_defaultaction", st->default_action) < 0) {
1367+return -1;
13921368 }
1369+return 0;
1370+}
139313711394-return m;
139513721396-error:
1397-if (st != NULL) {
1398-warnings_clear_state(st);
1399- }
1400-Py_DECREF(m);
1401-return NULL;
1373+static PyModuleDef_Slot warnings_slots[] = {
1374+ {Py_mod_exec, warnings_module_exec},
1375+ {0, NULL}
1376+};
1377+1378+static struct PyModuleDef warnings_module = {
1379+PyModuleDef_HEAD_INIT,
1380+ .m_name = MODULE_NAME,
1381+ .m_doc = warnings__doc__,
1382+ .m_size = 0,
1383+ .m_methods = warnings_functions,
1384+ .m_slots = warnings_slots,
1385+};
1386+1387+1388+PyMODINIT_FUNC
1389+_PyWarnings_Init(void)
1390+{
1391+return PyModuleDef_Init(&warnings_module);
14021392}
1403139314041394// We need this to ensure that warnings still work until late in finalization.