[Python-checkins] bpo-1635741: Fix ref leak in _PyWarnings_Init() error path (GH-23151)

vstinner webhook-mailer at python.org
Wed Nov 4 11:33:17 EST 2020


https://github.com/python/cpython/commit/58ca33b4674f39189b03c9a39fa7b676b43b3d08
commit: 58ca33b4674f39189b03c9a39fa7b676b43b3d08
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2020-11-04T17:33:06+01:00
summary:

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.

files:
M Python/_warnings.c
M Python/importdl.c

diff --git a/Python/_warnings.c b/Python/_warnings.c
index 3c048af4193a0..e42b7c3be3db9 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -1395,18 +1395,13 @@ _PyWarnings_Init(void)
         goto error;
     }
 
-    Py_INCREF(st->filters);
-    if (PyModule_AddObject(m, "filters", st->filters) < 0) {
+    if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) {
         goto error;
     }
-
-    Py_INCREF(st->once_registry);
-    if (PyModule_AddObject(m, "_onceregistry", st->once_registry) < 0) {
+    if (PyModule_AddObjectRef(m, "_onceregistry", st->once_registry) < 0) {
         goto error;
     }
-
-    Py_INCREF(st->default_action);
-    if (PyModule_AddObject(m, "_defaultaction", st->default_action) < 0) {
+    if (PyModule_AddObjectRef(m, "_defaultaction", st->default_action) < 0) {
         goto error;
     }
 
diff --git a/Python/importdl.c b/Python/importdl.c
index fbeb9fb75403e..1847eba74aef4 100644
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -220,10 +220,9 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
     def->m_base.m_init = p0;
 
     /* Remember the filename as the __file__ attribute */
-    if (PyModule_AddObject(m, "__file__", path) < 0)
+    if (PyModule_AddObjectRef(m, "__file__", path) < 0) {
         PyErr_Clear(); /* Not important enough to report */
-    else
-        Py_INCREF(path);
+    }
 
     PyObject *modules = PyImport_GetModuleDict();
     if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)



More information about the Python-checkins mailing list