Skip to content

Commit 6f4635f

Browse files
authored
bpo-1635741: Port _warnings to the multi-phase init (GH-23379)
Port the _warnings extension module to the multi-phase initialization API (PEP 489).
1 parent 829b177 commit 6f4635f

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Port the ``_warnings`` extension module to the multi-phase initialization
2+
API (:pep:`489`). Patch by Victor Stinner.

Python/_warnings.c

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ _Py_IDENTIFIER(ignore);
2424

2525
typedef struct _warnings_runtime_state WarningsState;
2626

27-
/* Forward declaration of the _warnings module definition. */
28-
static struct PyModuleDef warningsmodule;
29-
3027
_Py_IDENTIFIER(__name__);
3128

3229
/* Given a module object, get its per-module state. */
@@ -1353,52 +1350,45 @@ static PyMethodDef warnings_functions[] = {
13531350
};
13541351

13551352

1356-
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-
13791356
WarningsState *st = warnings_get_state();
13801357
if (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+
}
13931371

1394-
return m;
13951372

1396-
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
}
14031393

14041394
// We need this to ensure that warnings still work until late in finalization.

0 commit comments

Comments
 (0)