Skip to content

Commit 58ca33b

Browse files
authored
bpo-1635741: Fix ref leak in _PyWarnings_Init() error path (GH-23151)
Replace PyModule_AddObject() with PyModule_AddObjectRef() in the _warnings module to fix a reference leak on error. Use also PyModule_AddObjectRef() in importdl.c.
1 parent 18ce7f1 commit 58ca33b

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

Python/_warnings.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -1395,18 +1395,13 @@ _PyWarnings_Init(void)
13951395
goto error;
13961396
}
13971397

1398-
Py_INCREF(st->filters);
1399-
if (PyModule_AddObject(m, "filters", st->filters) < 0) {
1398+
if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) {
14001399
goto error;
14011400
}
1402-
1403-
Py_INCREF(st->once_registry);
1404-
if (PyModule_AddObject(m, "_onceregistry", st->once_registry) < 0) {
1401+
if (PyModule_AddObjectRef(m, "_onceregistry", st->once_registry) < 0) {
14051402
goto error;
14061403
}
1407-
1408-
Py_INCREF(st->default_action);
1409-
if (PyModule_AddObject(m, "_defaultaction", st->default_action) < 0) {
1404+
if (PyModule_AddObjectRef(m, "_defaultaction", st->default_action) < 0) {
14101405
goto error;
14111406
}
14121407

Python/importdl.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
220220
def->m_base.m_init = p0;
221221

222222
/* Remember the filename as the __file__ attribute */
223-
if (PyModule_AddObject(m, "__file__", path) < 0)
223+
if (PyModule_AddObjectRef(m, "__file__", path) < 0) {
224224
PyErr_Clear(); /* Not important enough to report */
225-
else
226-
Py_INCREF(path);
225+
}
227226

228227
PyObject *modules = PyImport_GetModuleDict();
229228
if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)

0 commit comments

Comments
 (0)