]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-1635741: Fix ref leak in _PyWarnings_Init() error path (GH-23151)
authorVictor Stinner <vstinner@python.org>
Wed, 4 Nov 2020 16:33:06 +0000 (17:33 +0100)
committerGitHub <noreply@github.com>
Wed, 4 Nov 2020 16:33:06 +0000 (17:33 +0100)
Replace PyModule_AddObject() with PyModule_AddObjectRef() in the
_warnings module to fix a reference leak on error.

Use also PyModule_AddObjectRef() in importdl.c.

Python/_warnings.c
Python/importdl.c

index 3c048af4193a00d87608fd65aa36087965faf171..e42b7c3be3db9afbd06a2b69e8eca43f03a5aca6 100644 (file)
@@ -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;
     }
 
index fbeb9fb75403e608d01de02ad6e7c8df389043bb..1847eba74aef45ce241bd13b2b262e45898fc9c3 100644 (file)
@@ -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)