Skip to content

Commit 87d23a0

Browse files
authored
bpo-36724: Add _PyWarnings_Fini() (#12963)
Py_FinalizeEx() now clears _PyRuntime.warnings variables and _PyRuntime.exitfuncs. Changes: * Add _PyWarnings_Fini(): called by Py_FinalizeEx() * call_ll_exitfuncs() now clears _PyRuntime.exitfuncs while iterating on it (on backward order).
1 parent 99e69d4 commit 87d23a0

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extern void PyLong_Fini(void);
7676
extern void _PyFaulthandler_Fini(void);
7777
extern void _PyHash_Fini(void);
7878
extern int _PyTraceMalloc_Fini(void);
79+
extern void _PyWarnings_Fini(_PyRuntimeState *runtime);
7980

8081
extern void _PyGILState_Init(
8182
_PyRuntimeState *runtime,

Python/_warnings.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,35 +1259,46 @@ _PyWarnings_Init(void)
12591259
if (m == NULL)
12601260
return NULL;
12611261

1262-
if (_PyRuntime.warnings.filters == NULL) {
1263-
_PyRuntime.warnings.filters = init_filters();
1264-
if (_PyRuntime.warnings.filters == NULL)
1262+
struct _warnings_runtime_state *state = &_PyRuntime.warnings;
1263+
if (state->filters == NULL) {
1264+
state->filters = init_filters();
1265+
if (state->filters == NULL)
12651266
return NULL;
12661267
}
1267-
Py_INCREF(_PyRuntime.warnings.filters);
1268-
if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
1268+
Py_INCREF(state->filters);
1269+
if (PyModule_AddObject(m, "filters", state->filters) < 0)
12691270
return NULL;
12701271

1271-
if (_PyRuntime.warnings.once_registry == NULL) {
1272-
_PyRuntime.warnings.once_registry = PyDict_New();
1273-
if (_PyRuntime.warnings.once_registry == NULL)
1272+
if (state->once_registry == NULL) {
1273+
state->once_registry = PyDict_New();
1274+
if (state->once_registry == NULL)
12741275
return NULL;
12751276
}
1276-
Py_INCREF(_PyRuntime.warnings.once_registry);
1277+
Py_INCREF(state->once_registry);
12771278
if (PyModule_AddObject(m, "_onceregistry",
1278-
_PyRuntime.warnings.once_registry) < 0)
1279+
state->once_registry) < 0)
12791280
return NULL;
12801281

1281-
if (_PyRuntime.warnings.default_action == NULL) {
1282-
_PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1283-
if (_PyRuntime.warnings.default_action == NULL)
1282+
if (state->default_action == NULL) {
1283+
state->default_action = PyUnicode_FromString("default");
1284+
if (state->default_action == NULL)
12841285
return NULL;
12851286
}
1286-
Py_INCREF(_PyRuntime.warnings.default_action);
1287+
Py_INCREF(state->default_action);
12871288
if (PyModule_AddObject(m, "_defaultaction",
1288-
_PyRuntime.warnings.default_action) < 0)
1289+
state->default_action) < 0)
12891290
return NULL;
12901291

1291-
_PyRuntime.warnings.filters_version = 0;
1292+
state->filters_version = 0;
12921293
return m;
12931294
}
1295+
1296+
1297+
void
1298+
_PyWarnings_Fini(_PyRuntimeState *runtime)
1299+
{
1300+
struct _warnings_runtime_state *state = &runtime->warnings;
1301+
Py_CLEAR(state->filters);
1302+
Py_CLEAR(state->once_registry);
1303+
Py_CLEAR(state->default_action);
1304+
}

Python/pylifecycle.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ Py_FinalizeEx(void)
13131313
PyDict_Fini();
13141314
PySlice_Fini();
13151315
_PyGC_Fini(runtime);
1316+
_PyWarnings_Fini(runtime);
13161317
_Py_HashRandomization_Fini();
13171318
_PyArg_Fini();
13181319
PyAsyncGen_Fini();
@@ -2248,7 +2249,12 @@ static void
22482249
call_ll_exitfuncs(_PyRuntimeState *runtime)
22492250
{
22502251
while (runtime->nexitfuncs > 0) {
2251-
(*runtime->exitfuncs[--runtime->nexitfuncs])();
2252+
/* pop last function from the list */
2253+
runtime->nexitfuncs--;
2254+
void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
2255+
runtime->exitfuncs[runtime->nexitfuncs] = NULL;
2256+
2257+
exitfunc();
22522258
}
22532259

22542260
fflush(stdout);

0 commit comments

Comments
 (0)