bpo-1635741: Port _warnings to the multi-phase init (GH-23379)

Port the _warnings extension module to the multi-phase initialization
API (PEP 489).
This commit is contained in:
Victor Stinner 2020-11-19 00:19:06 +01:00 committed by GitHub
parent 829b177436
commit 6f4635fe20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 43 deletions

View File

@ -0,0 +1,2 @@
Port the ``_warnings`` extension module to the multi-phase initialization
API (:pep:`489`). Patch by Victor Stinner.

View File

@ -24,9 +24,6 @@ _Py_IDENTIFIER(ignore);
typedef struct _warnings_runtime_state WarningsState; typedef struct _warnings_runtime_state WarningsState;
/* Forward declaration of the _warnings module definition. */
static struct PyModuleDef warningsmodule;
_Py_IDENTIFIER(__name__); _Py_IDENTIFIER(__name__);
/* Given a module object, get its per-module state. */ /* Given a module object, get its per-module state. */
@ -1353,52 +1350,45 @@ static PyMethodDef warnings_functions[] = {
}; };
static struct PyModuleDef warningsmodule = { static int
PyModuleDef_HEAD_INIT, warnings_module_exec(PyObject *module)
MODULE_NAME, /* m_name */ {
warnings__doc__, /* m_doc */ WarningsState *st = warnings_get_state();
0, /* m_size */ if (st == NULL) {
warnings_functions, /* m_methods */ return -1;
NULL, /* m_reload */ }
NULL, /* m_traverse */ if (PyModule_AddObjectRef(module, "filters", st->filters) < 0) {
NULL, /* m_clear */ return -1;
NULL /* m_free */ }
if (PyModule_AddObjectRef(module, "_onceregistry", st->once_registry) < 0) {
return -1;
}
if (PyModule_AddObjectRef(module, "_defaultaction", st->default_action) < 0) {
return -1;
}
return 0;
}
static PyModuleDef_Slot warnings_slots[] = {
{Py_mod_exec, warnings_module_exec},
{0, NULL}
};
static struct PyModuleDef warnings_module = {
PyModuleDef_HEAD_INIT,
.m_name = MODULE_NAME,
.m_doc = warnings__doc__,
.m_size = 0,
.m_methods = warnings_functions,
.m_slots = warnings_slots,
}; };
PyMODINIT_FUNC PyMODINIT_FUNC
_PyWarnings_Init(void) _PyWarnings_Init(void)
{ {
PyObject *m; return PyModuleDef_Init(&warnings_module);
m = PyModule_Create(&warningsmodule);
if (m == NULL) {
return NULL;
}
WarningsState *st = warnings_get_state();
if (st == NULL) {
goto error;
}
if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) {
goto error;
}
if (PyModule_AddObjectRef(m, "_onceregistry", st->once_registry) < 0) {
goto error;
}
if (PyModule_AddObjectRef(m, "_defaultaction", st->default_action) < 0) {
goto error;
}
return m;
error:
if (st != NULL) {
warnings_clear_state(st);
}
Py_DECREF(m);
return NULL;
} }
// We need this to ensure that warnings still work until late in finalization. // We need this to ensure that warnings still work until late in finalization.