[Python-checkins] cpython: Issue #18520: Fix _PySys_Init(), handle PyDict_SetItemString() errors

victor.stinner python-checkins at python.org
Mon Jul 22 23:59:09 CEST 2013


http://hg.python.org/cpython/rev/31796b188bec
changeset:   84795:31796b188bec
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Jul 22 22:40:00 2013 +0200
summary:
  Issue #18520: Fix _PySys_Init(), handle PyDict_SetItemString() errors

files:
  Python/sysmodule.c |  38 ++++++++++++++++++---------------
  1 files changed, 21 insertions(+), 17 deletions(-)


diff --git a/Python/sysmodule.c b/Python/sysmodule.c
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1565,17 +1565,24 @@
 PyObject *
 _PySys_Init(void)
 {
-    PyObject *m, *v, *sysdict, *version_info;
+    PyObject *m, *sysdict, *version_info;
 
     m = PyModule_Create(&sysmodule);
     if (m == NULL)
         return NULL;
     sysdict = PyModule_GetDict(m);
-#define SET_SYS_FROM_STRING(key, value)                 \
-    v = value;                                          \
-    if (v != NULL)                                      \
-        PyDict_SetItemString(sysdict, key, v);          \
-    Py_XDECREF(v)
+#define SET_SYS_FROM_STRING(key, value)                    \
+    do {                                                   \
+        int res;                                           \
+        PyObject *v = (value);                             \
+        if (v == NULL)                                     \
+            return NULL;                                   \
+        res = PyDict_SetItemString(sysdict, key, v);       \
+        if (res < 0) {                                     \
+            Py_DECREF(v);                                  \
+            return NULL;                                   \
+        }                                                  \
+    } while (0)
 
     /* Check that stdin is not a directory
     Using shell redirection, you can redirect stdin to a directory,
@@ -1597,10 +1604,10 @@
 
     /* stdin/stdout/stderr are now set by pythonrun.c */
 
-    PyDict_SetItemString(sysdict, "__displayhook__",
-                         PyDict_GetItemString(sysdict, "displayhook"));
-    PyDict_SetItemString(sysdict, "__excepthook__",
-                         PyDict_GetItemString(sysdict, "excepthook"));
+    SET_SYS_FROM_STRING("__displayhook__",
+                        PyDict_GetItemString(sysdict, "displayhook"));
+    SET_SYS_FROM_STRING("__excepthook__",
+                        PyDict_GetItemString(sysdict, "excepthook"));
     SET_SYS_FROM_STRING("version",
                          PyUnicode_FromString(Py_GetVersion()));
     SET_SYS_FROM_STRING("hexversion",
@@ -1664,18 +1671,15 @@
 #endif
     if (warnoptions == NULL) {
         warnoptions = PyList_New(0);
+        if (warnoptions == NULL)
+            return NULL;
     }
     else {
         Py_INCREF(warnoptions);
     }
-    if (warnoptions != NULL) {
-        PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
-    }
+    SET_SYS_FROM_STRING("warnoptions", warnoptions);
 
-    v = get_xoptions();
-    if (v != NULL) {
-        PyDict_SetItemString(sysdict, "_xoptions", v);
-    }
+    SET_SYS_FROM_STRING("_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