]> granicus.if.org Git - python/commitdiff
Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 16 Jul 2013 20:26:05 +0000 (22:26 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 16 Jul 2013 20:26:05 +0000 (22:26 +0200)
Modules/_pickle.c
Modules/main.c
Python/bltinmodule.c
Python/import.c

index e7d7dd95ac06ca5d9d2dd8363027994511aeff16..4ba185d4b476794e318c0f7829d3f3ddcc37b9f6 100644 (file)
@@ -1361,8 +1361,10 @@ whichmodule(PyObject *global, PyObject *global_name)
 
   search:
     modules_dict = PySys_GetObject("modules");
-    if (modules_dict == NULL)
+    if (modules_dict == NULL) {
+        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
         return NULL;
+    }
 
     i = 0;
     module_name = NULL;
@@ -5542,8 +5544,10 @@ Unpickler_find_class(UnpicklerObject *self, PyObject *args)
     }
 
     modules_dict = PySys_GetObject("modules");
-    if (modules_dict == NULL)
+    if (modules_dict == NULL) {
+        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
         return NULL;
+    }
 
     module = PyDict_GetItemWithError(modules_dict, module_name);
     if (module == NULL) {
index 0343ddab6e9133d69738e6bdee6e3d34c714c97e..e592d8bd2fb1b73dc58a969e4604796e0142148a 100644 (file)
@@ -260,8 +260,10 @@ RunMainFromImporter(wchar_t *filename)
     /* argv0 is usable as an import source, so put it in sys.path[0]
        and import __main__ */
     sys_path = PySys_GetObject("path");
-    if (sys_path == NULL)
+    if (sys_path == NULL) {
+        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
         goto error;
+    }
     if (PyList_SetItem(sys_path, 0, argv0)) {
         argv0 = NULL;
         goto error;
index 949f0294460b45fe3c745c0ecd75dce67c58652f..06d71f726ca5a408cb9ee896628d40aecf6db741 100644 (file)
@@ -1550,6 +1550,11 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
         return NULL;
     if (file == NULL || file == Py_None) {
         file = PySys_GetObject("stdout");
+        if (file == NULL) {
+            PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
+            return NULL;
+        }
+
         /* sys.stdout may be None when FILE* stdout isn't connected */
         if (file == Py_None)
             Py_RETURN_NONE;
index 2e5d2059731e1d07497bdd4efe1ec98af5183474..a1c0aee54034c337d263128a9ae4420167daadb9 100644 (file)
@@ -85,8 +85,10 @@ _PyImportZip_Init(void)
     int err = 0;
 
     path_hooks = PySys_GetObject("path_hooks");
-    if (path_hooks == NULL)
+    if (path_hooks == NULL) {
+        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks");
         goto error;
+    }
 
     if (Py_VerboseFlag)
         PySys_WriteStderr("# installing zipimport hook\n");
@@ -944,11 +946,11 @@ PyAPI_FUNC(PyObject *)
 PyImport_GetImporter(PyObject *path) {
     PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
 
-    if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
-        if ((path_hooks = PySys_GetObject("path_hooks"))) {
-            importer = get_path_importer(path_importer_cache,
-                                         path_hooks, path);
-        }
+    path_importer_cache = PySys_GetObject("path_importer_cache");
+    path_hooks = PySys_GetObject("path_hooks");
+    if (path_importer_cache != NULL && path_hooks != NULL) {
+        importer = get_path_importer(path_importer_cache,
+                                     path_hooks, path);
     }
     Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
     return importer;