[Python-checkins] cpython: Issue #18520: fix reference leak in _PySys_Init()

victor.stinner python-checkins at python.org
Sun Oct 27 17:15:54 CET 2013


http://hg.python.org/cpython/rev/5eb00460e6e8
changeset:   86693:5eb00460e6e8
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sun Oct 27 17:15:42 2013 +0100
summary:
  Issue #18520: fix reference leak in _PySys_Init()

files:
  Python/sysmodule.c |  25 ++++++++++++++++++-------
  1 files changed, 18 insertions(+), 7 deletions(-)


diff --git a/Python/sysmodule.c b/Python/sysmodule.c
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1573,6 +1573,17 @@
     if (m == NULL)
         return NULL;
     sysdict = PyModule_GetDict(m);
+#define SET_SYS_FROM_STRING_BORROW(key, value)             \
+    do {                                                   \
+        int res;                                           \
+        PyObject *v = (value);                             \
+        if (v == NULL)                                     \
+            return NULL;                                   \
+        res = PyDict_SetItemString(sysdict, key, v);       \
+        if (res < 0) {                                     \
+            return NULL;                                   \
+        }                                                  \
+    } while (0)
 #define SET_SYS_FROM_STRING(key, value)                    \
     do {                                                   \
         int res;                                           \
@@ -1580,8 +1591,8 @@
         if (v == NULL)                                     \
             return NULL;                                   \
         res = PyDict_SetItemString(sysdict, key, v);       \
+        Py_DECREF(v);                                      \
         if (res < 0) {                                     \
-            Py_DECREF(v);                                  \
             return NULL;                                   \
         }                                                  \
     } while (0)
@@ -1606,10 +1617,10 @@
 
     /* stdin/stdout/stderr are now set by pythonrun.c */
 
-    SET_SYS_FROM_STRING("__displayhook__",
-                        PyDict_GetItemString(sysdict, "displayhook"));
-    SET_SYS_FROM_STRING("__excepthook__",
-                        PyDict_GetItemString(sysdict, "excepthook"));
+    SET_SYS_FROM_STRING_BORROW("__displayhook__",
+                               PyDict_GetItemString(sysdict, "displayhook"));
+    SET_SYS_FROM_STRING_BORROW("__excepthook__",
+                               PyDict_GetItemString(sysdict, "excepthook"));
     SET_SYS_FROM_STRING("version",
                          PyUnicode_FromString(Py_GetVersion()));
     SET_SYS_FROM_STRING("hexversion",
@@ -1679,9 +1690,9 @@
     else {
         Py_INCREF(warnoptions);
     }
-    SET_SYS_FROM_STRING("warnoptions", warnoptions);
+    SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions);
 
-    SET_SYS_FROM_STRING("_xoptions", get_xoptions());
+    SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions());
 
     /* version_info */
     if (VersionInfoType.tp_name == NULL) {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list